MM92x
MM92x

Reputation: 568

C# WPF Crash in the constructor during the initialization of a TextBox element?

I've the class Person:

public class Person {

public Person(string nome) {
        this.Name.Text = nome;
        this.Name.Background = Brushes.Red;
        System.Windows.MessageBox.Show(Name.Text.ToString()); 
    }
    public TextBox Name;
}

In the main, I've

List<Person> items = new List<Person>();
Person p = new Person("Samantha");
items.Add(p);
listView.ItemsSource = items;

Now, in the xaml file, i've a GridView which contain the listView. All is ok if instead TextBox Name I put string Name. But I need the background color, so I used a TextBox.

The problem is that the constructor Person crash in "this.Name.Text = nome;" and I don't understand why. Thanks to all

Upvotes: 0

Views: 256

Answers (1)

Giangregorio
Giangregorio

Reputation: 1510

Because you have declared a TextBox called Name (what a bad name!, change it) and you have not created it! You need to create a new instance of TextBox before you can use it.

Then we can discuss why you are putting a TextBox in your class ;-)

Upvotes: 1

Related Questions