ASP.NET MVC 3 - How does this boolean work in a controller?

I am looking at a tutorial for asp.net mvc here on asp site: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-basic-crud-functionality-with- the-entity-framework-in-asp-net-mvc-application

There is a method in the controller that confused me:

//
        // GET: /Student/Delete/5

        public ActionResult Delete(int id, bool? saveChangesError)
        {
            if (saveChangesError.GetValueOrDefault())
            {
                ViewBag.ErrorMessage = "Unable to save changes.  Try again, and if the problem persists contact your system administrator.";
            }
            return View(db.Students.Find(id));
        }

      

I can see that the bool is being instantiated named "saveChangesError" but there is a boolean method in the if statement called "GetValueOrDefault ()"

What exactly happens in this scenario? I assume GetValueOrDefault () should be a method of all boolean types? I looked at this in the .NET documentation and found this definition:

The value of the Value property if the HasValue property is true; otherwise, the default value of the current object is Nullable (Of T). The default type is the type argument of the current Nullable (Of T) Object, and the default value consists of only binary zeros.

I am having trouble connecting this definition to what is happening in a .net mvc application.

Thank.

+3


source to share


5 answers


GetValueOrDefault()

not part bool

, part of itNullable<T>

. The key here is the syntax, which is bool

declared in the function header:

public ActionResult Delete(int id, bool? saveChangesError)

      



The question mark is a C # language construct that indicates that it is not valid bool

, but it is Nullable<bool>

. Value types of which bool

one cannot be null

. But sometimes it would be helpful if they could. Thus, the structure Nullable<T>

exists to fulfill this purpose.

GetValueOrDefault()

is a method of this structure that will return a value bool

or a default value for bool

( whichfalse

) if no value is specified.

+5


source


The documentation states that GetValueOrDefault()

"Retrieves the value of the current object Nullable(Of T)

or the default of an object." Nullable(Of T)

in this case saveChangesError

.



If an error occurs while saving changes to the database (in this case, deleting the student), then it saveChangesError.GetValueOrDefault()

will return true and the if statement will process its contents.

+2


source


Look at the definition of the bool? saveChangesError

"?" means bool is null. It can be True

| False

| Null

...

Since it now has three meanings, you cannot simply check True

| False

... You should also check for null.

GetValueOrDefault()

is added to nullable types, which does exactly what the method describes. If the parameter has a value, it will return it, unless it returns a default value (in this case, it's false ).

So, to break the if statement, it would be something like

if (saveChangesError.HasValue && saveChangesError == true)
{
    //...
}

      

Using the method just makes it more concise.

+2


source


Invalid objects implement the overloaded GetValueOrDefault method: Returns the object's value with a null value if assigned a value, or a default value.

+1


source


bool? saveChangesError 

      

is a null boolean data type. It has three possible values: 1) true 2) false 3) null

GetValueOrDefault () will return either true or false if the value is set, or null if it is not set. In mention script if value is true

if (saveChangesError.GetValueOrDefault())

      

An error has occurred. Hope it helps

+1


source







All Articles