附加信息:未将对象引用设置为对象的实例

时间:2014-03-07 05:47:07

标签: c# variables static

所以我得到这个错误,我不明白为什么我得到它。我试着在这里寻找答案,但每个问题都是如此独特,以至于它们不适用于我的问题。

这是我到目前为止所拥有的。

class customer{
    public string sFirstName { get; set; }
    public string sLastName { get; set; }
    public int iAge { get; set; }
}
class customerObject{
    public static List<customer> lstStaticUsers { get; set; }
    public customerObject(){
        lstStaticcustomer = new List<customer>();
    }
    public static void AddNewStaticUser(string FirstName, string LastName, int Age)
    {
        lstStaticcustomer.Add(new customer{sFirstName = FirstName, sLastName = LastName, iAge = Age});
    }
}

这是我的窗口

public partial class wndCustomerInfo : Window
{
    public wndCustomerInfo ()
    {
        InitializeComponent();
    }

    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        this.Hide();
        e.Cancel = true;
    }

    private void btnSubmitForm_Click(object sender, RoutedEventArgs e)
    {
        customerObject.AddNewStaticcustomer(txtFirstName.Text, txtLastName.Text, Convert.ToInt32(txtAge.Text));
        lblMessage.Content = "Created new user: " + txtFirstName.Text;
    }
}

然后这是我的主窗口打开上面的那个

public partial class MainWindow : Window
{

    wndCustomerInfo wndCustomerInfoForm;

    public MainWindow()
    {
        InitializeComponent();
        Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
        wndCustomerInfoForm= new wndCustomerInfo ();
    }

    private void btnSave_Click(object sender, RoutedEventArgs e)
    {
        this.Hide();
        wndCustomerInfoForm.ShowDialog();
        this.Show();
    }
}

如果有人能提供帮助那就太棒了。我是一种新的c#所以请你好看

1 个答案:

答案 0 :(得分:4)

制作customObject的构造函数static

static customerObject(){
    lstStaticcustomer = new List<customer>();
}