0

For example in my model class

public class Dashboard
{
    public Dashboard(Account account)
    {
        AccountListName = new List<string>();
        AccountListName.Add(account.Name);

        string AccountName = account.Name;

    }
    public List<string> AccountListName { get; set; }

    public string AccountName { get; set; }

}

When I call the model in my controller.

var model = new DashBoard(account);

the model would contain the AccountListName properly but the AccountName would return null. Why does the AccountName returns null when I bind it to account.Name? Is there any weird interaction with the string type? And how do I fix this issue?

3 Answers 3

2

Because you've redefined a more local scope of AccountName within your constructor that never assigns the value to your property.

string AccountName = account.Name;

Just remove the string and you should be fine. I'm surprised the compiler isn't warning you.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the help. But why doesn't the AccountListName also return null as well since I also redeclared it in my constructor?
You haven't declared it in your constructor. You've /instantiated/ it in your constructor.
You didn't re-declare it. You re-initialized it, since the auto property will initialize it for you, and then added your object to it.
string foo; This declared a variable foo of type string. foo = "test"; This sets the variable to a value. string foo = "test"; This declares AND sets the value. In the case of your list, you've declared AccountListName as a List<string> and AccountName as string by declaring them as properties. In your constructor you're using the AccountListName declaration by instantiating a new list and adding the name. You never use the AccountName property but rather created a new variable of the same name that immediately goes out of scope at the end of the constructor.
2

You have declared AccountName a second time in the local scope of your function. You are setting this instead of the property of dashboard.

Comments

1

Use:

this.AccountName = account.Name

instead of:

string AccountName = account.Name

You've accidentally redefined your AccountName to a local variable instead of assigning your property.

12 Comments

The this qualifier is redundant.
Lol no kidding. It makes it clearer to avoid issues such as these.
I will always choose clarity.
It is not clearer as the this is implied when using Property accessors within the scope of the class. It's bad practice to have both a property and scoped variable with the same name within the same class.
Following .Net Design Guidelines and Best Practices, inner-scoped variables should not be declared with names that begin with capital letters, thus alleviating any need for the this. msdn.microsoft.com/en-us/library/ms229043.aspx
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.