yanguya995
yanguya995

Reputation: 143

Get and Set properties' initializing

When I write the code like the methods below my fields get initialized correctly and the application works fine.

private string username;
private string password;

public string Password
{
    get { return password; }
    set { password = value; }
}

public string Username
{
    get { return username; }
    set { username = value; }
}

public Authenticate()
{
    this.username = "njabulo";
    this.password = "12345";
}

Before writing it like this I had written the code in the following fashion and the fields didn't get initialized:

private string username;
private string password;

public string Password
{
    get { return password; }
    set { password = "njabulo"; }
}

public string Username
{
    get { return username; }
    set { username = "12345"; }
}

I would like to know what exactly is causing the error in the second method. I think the value on the set property stands for anything that may be thrown at the property and I am giving it an actual value.

Upvotes: 3

Views: 23443

Answers (4)

user6522773
user6522773

Reputation:

The correct initialization is the first method, or make it shorter using automatic property. The second method, before anybody call "set", your Password or Username is still null :

public string Password { get; set; }
public string Username { get; set; }
public Authenticate()
{
    Username = "njabulo";
    Password = "12345";
}

I add more because of your comments above (compare value), you can use it like this:

public class Authenticate
{
  private string _password;
  private string _username;

  public Authenticate()
  {
    _password = "mypassword";
    _username = "myusername";
  }

  public string Password
  {
    get { return _password; }
    set
    {
      if (_password != value)  // Compare it here
        _password = value;
    }
  }

  public string Username
  {
    get { return _username; }
    set
    {
      if (_username != value)  // Compare it here
        _username = value;
    }
  }
}

Upvotes: 1

Scott Hannen
Scott Hannen

Reputation: 29222

The purpose of set is to allow setting the value of that property, like this:

var x = new WhateverYourClassIsNamed();
x.Username = "ABC";

You would normally write the property like this:

public string Username
{
    get { return username; }
    set { username = value; }
}

That way if someone calls

x.Username = "newusername";

then when the set method is called, value is "newusername"; That's how you can set a property on your class. You don't have to declare a variable named value. That automatically refers to whatever value is used when you call set.

If you do this:

set { username = "12345"; }

Then it doesn't matter what value you try to set. You could call

x.Username = "99999"

or any other value, but it's always going to set username to "12345".

Usually when we say "initialize" we mean values that are set when the class is first created. If that's what you had in mind you could do this:

private string username;
private string password = "12345"; //This set whenever you create
                                   //a new instance of the class
public string Password
{
    get { return password; }
    set { password = value; }
}

public string Username
{
    get { return username; }
    set { username = value; }
}

or do as Crowcoder suggested,

public string Password {get; set;} = "12345";

That's a newer, more convenient syntax that does the same thing.

Upvotes: 2

Toji Thomas
Toji Thomas

Reputation: 54

When you define a property, with getter or setter, it means that the code for getter or setter only execute when any of these actions occurred.

In the second example you haven't called the setter yet.and there is no reason to specify setter with a content value.

The first example is fine bcoz you have done the followings

1.Defined properties with back end fields. 2.initialised back end fields

But in the second one you haven't made initialisation.

Upvotes: 2

Crowcoder
Crowcoder

Reputation: 11514

There is no reason to Set to a literal value, you may as well do

get { return "njabulo"; }

If you are using C# 6 then you can initialize like:

public string Password {get; set;} = "njabulo";

Then it will initialize, but not always stay that value if you set it later.

Upvotes: 15

Related Questions