Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getter and Setter not working

Tags:

c#

.net

I currently have the following class with getters and setters as such

public class CustAccount
{
    public string Account { get; set; }        private string account;
    public string AccountType { get; set; }    private string accountType;

    public CustSTIOrder(Account ord)
    {
        account = ord.Account;
        accountType = ord.AccountType;
    }
}

Now I realize that with public string Account { get; set; } I do not need to declare private string account . Anyways now my private variable account contains the value but when I use Account to obtain the value I get a null. Any suggestions on why I am getting a null ?

like image 292
Casper_2211 Avatar asked Dec 07 '22 09:12

Casper_2211


1 Answers

Since you're using an auto property, you should be using Account for all references to the property.

If you're looking to use a backing field, then you'll need to have the backing field (account) in the get and set.

Example:

public string Account 
{ 
    get { return account; }
    set { account = value; }
}        
private string account;

Example of use for the auto property:

public CustSTIOrder(Account ord)
{
    Account = ord.Account;
    // the rest
}
like image 92
Khan Avatar answered Dec 18 '22 21:12

Khan