2

I have in my program defined an object log.

LoginModel log = new LoginModel ( ) ;

I do not write values ​​into it. But why the object is not NULL when my function return it? (See picture)

[Bind(Exclude = "UserID")]
public class LoginModel
{
    [ScaffoldColumn(false)]
    public int UserID { get; set; }

    [ScaffoldColumn(false)]
    public string FirstName { get; set; }

    [Display(Name = "E-mailadresse")]
    [Required(ErrorMessage = "Skriv venligst din e-mailadresse", AllowEmptyStrings = false)]
    [EmailAddress(ErrorMessage = "Ugyldig e-mailadresse")]
    [DataType(DataType.EmailAddress)]
    public string Emailaddress { get; set; }

    [Display(Name = "Kodeord")]
    [Required(ErrorMessage = "Skriv venligst et kodeord", AllowEmptyStrings = false)]
    [DataType(DataType.Password)]
    [StringLength(8, MinimumLength = 4, ErrorMessage = "Kodeordet skal mindst bestå af 4-8 karakter.")]
    public string Password { get; set; }
}

enter image description here

ekad
  • 14,436
  • 26
  • 44
  • 46
Ahmed Sazar
  • 915
  • 1
  • 8
  • 10
  • Soo i cant do that: if(log != null) { } How can I check the log object has been loaded with values ​​from the database or not ? – Ahmed Sazar Jul 20 '16 at 08:23
  • you can use `if(log.UserID!=0) {}` to determine if it has been loaded. The point is to check if the value of the property is not `default(sometype)`, then it has been loaded with new values. – kurakura88 Jul 20 '16 at 08:52

3 Answers3

2

The code LoginModel log = new LoginModel ( ) ; will create a new instance of the class LoginModel which will contains all accessible properties and fields with default values. If you want log to be null means declare like this:

LoginModel log;

Note : You should assign an instance of LoginModel class to the log to access values from it. Else it will throws NullReferenceException

Community
  • 1
  • 1
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • Soo i cant do that: if(log != null) { } How can I check the log object has been loaded with values ​​from the database or not ? – Ahmed Sazar Jul 20 '16 at 08:40
1

When you new an object it wont be null anymore.

if you have

LoginModel log;

or

LoginModel log =null;

Then it will be null. when you write

LoginModel log = new LoginModel ( ) ;

It will new your object and assign in to the variable (log),so it won't be null anymore. it will be the object with properties as you have initialized in your constructor (in your case there is no initialization, so every nullable property will be null and integers will be 0 , etc)

Ashkan S
  • 10,464
  • 6
  • 51
  • 80
1

https://en.wikipedia.org/wiki/Default_constructor

In both Java and C#, a "default constructor" refers to a nullary constructor that is automatically generated by the compiler if no constructors have been defined for the class. The default constructor implicitly calls the superclass's nullary constructor, then executes an empty body. All fields are left at their initial value of 0 (integer types), 0.0 (floating-point types), false (boolean type), or null (reference types). A programmer-defined constructor that takes no parameters is also called a default constructor in C#, but not in Java

And as everyone says, if you call the default constructor, the value of that object won't be null.

kurakura88
  • 2,185
  • 2
  • 12
  • 18