4

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 ?

7
  • 3
    OT: Funny that you comment //end method at the end of the class definition. Commented Feb 1, 2013 at 14:43
  • 1
    The Account property and the account variable are unrelated. Commented Feb 1, 2013 at 14:43
  • 1
    Yes, because you don't assign Account anywhere. There is no clever mechanism that maps your variable account to the method Account all by itself. If you do want to use these names, write the get and set routines out in full. Commented Feb 1, 2013 at 14:44
  • It doesn't automatically make a private variable for you. If you create a property using the short hand with {get;set;} then just use that properties name, in your case Account and AccountType with CAPITAL a's Commented Feb 1, 2013 at 14:45
  • @LastCoder: Correct, but to be a little more precise: Using an auto property does create a private field! However, that private field will be anonymous, i.e. hidden from the programmer, so there is no way to directly refer to it. Thus the need to do all access via the property; or implement the property explicitly, which gives you the choice which field will be used as the property's backing field. Commented Feb 1, 2013 at 14:48

4 Answers 4

6

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 } 
Sign up to request clarification or add additional context in comments.

6 Comments

If no special need, using auto property will be simpler. You could also add an example using that.
Right, which in the first line of my answer, it says he should be using just Account then.
@Jeffery Khan So if I decided to go with public string Account { get; set; } how would I setup an initializing value like I do for private variable private string account = "0000" ?
By setting the Account value in your constructor. Or use the first example (not using auto properties)
That would be special implementation and in that case you would want to use a backing field. Set the backing field to whatever the you would like the default value to be. You could also use the constructor to assign a value to the auto property by default as well.
|
4

The private field needs to be used in the property otherwise you get an auto-implemented property which has a different backing store.

public class CustAccount { private string account; public string Account { get {return account;} set{account = value;} } private string accountType; public string AccountType { get{return accountType;} set{accountType = value;} } public CustSTIOrder(Account ord) { account = ord.Account; accountType = ord.AccountType; } } 

Comments

3

You have to connect property Account with field account:

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

Comments

2

Just don´t use account, use the Property directly:

public class CustAccount { public string Account { get; set; } public string AccountType { get; set; } public CustSTIOrder(Account ord) { Account = ord.Account; AccountType = ord.AccountType; } } 

Those auto-properties are internally backed with a field, so you don´t have to write that trivial code.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.