asim.ali314
asim.ali314

Reputation: 41

Getting Value Through C# Getter Setter Property Returns Null

I have a Login.cs class and I have set properties in it as below.

private string Username_;
private string Password_; 

public string UserName
{
    get => Username_;
    set => Username_ = value; 
}
public string Password
{
    get => Password_; 
    set => Password_ = value; 
}

and setting the values in the same class for both properties as below.

UserName = "admin";
Password = "admin";

Where Username_ & Password_ are both strings. Now I have another Usercontrol which I am accessing in MainWindow on button click which is shown below.

<vm:ViewUsers x:Name="ViewAllUsers" Visibility="Hidden"/>

What I am doing in Code behind of UserControl is as below to access the values of Username and Password through Property Getter Setter as below.

string username = Login.GetInstance().UserName;
string password = Login.GetInstance().Password;

On debugging obj.UserName; property Returns Null. Why does it return null and how should I fix that?

Upvotes: 0

Views: 2787

Answers (3)

Jon Skeet
Jon Skeet

Reputation: 1500385

The problem is that you're creating a new instance of Login() before you fetch the properties:

// Code originally in the question
Login obj = new Login();

This has now been changed to Login.GetInstance(), but we don't know what that method does. If that method returns a reference to a new instance of Login each time, we're left with the same problem.

Both properties in that object will be null, because you haven't set them to anything. (The default value for all reference types is null.) It's not clear to me where you're setting the properties to any other value, but whichever object you're setting them on isn't the same one that you're then using in the final piece of code you showed us.

You need to make sure you can use the "getters" on the same object that you called the "setters" on.

As an aside, because your properties are trivial (they're just getting and setting variables, with no other behavior such as validation) you can replace all of the first piece of code you've got with automatically implemented properties:

public string UserName { get; set; }
public string Password { get; set; }

Upvotes: 3

Gauravsa
Gauravsa

Reputation: 6524

As other users have stated, you are creating a new instance of Login which automatically initializes the fields to default values (null for reference types, 0 for numbers).

Also, you can look at removing the default constructor for Login class and have the following as constructor:

public Login(string username, string password)
{
    Username = username;
    Password = password;
}

By doing this, you can then do Login login = new Login("admin", "admin").

You can also look at Singleton pattern if you only wish to have one instance of Login class.

Upvotes: 0

Michał Turczyn
Michał Turczyn

Reputation: 37367

You are misunderstanding how getters and setters work I think.

At the moment of initialization of an object, all it's uninitialized fields are set to default values (null for reference types, 0 for numbers, etc.).

Getters and setters are, as their name suggest, for setting value and getting.

What you are doing, after initialization you use private fields to supply value for respective setter. But what it does is assign value supplied to it to underlaying field.

Since you are using the same private field, you never initialize it, since it is null (as I mentioned), then you try to set its value through setter, but what you pass there is null, hence you end up with nulls as well.

And C# provide something known as properties, so you don't need private fields. Unless you want to develop some additional logic for getters and setters.

Upvotes: 2

Related Questions