永远不会分配字段“帐户”和“卡”,并且默认为Null

时间:2017-05-24 18:38:37

标签: c#

错误表示卡和帐户字段从未分配给,但它们在默认构造函数中分配。我在这里错过了什么吗?

    private string firstName, lastName;
    private string address, number;
    private Account account;
    private Card card;

    public Person()
    {
        firstName = "John";
        lastName = "Doe";
        address = "";
        number = "02080000000";
        account.AccountNumber = "0000";
        card.CardNumber = "0000/0000/0000/0000";
        card.PinNumber = "0000";
    }

    public Person(string FName, string LName, string Address, string Number, string AccNumber, string CardNumber, string PinNumber)
    {
        firstName = FName;
        lastName = LName;
        address = Address;
        number = Number;
        account.AccountNumber = AccNumber;
        card.CardNumber = CardNumber;
        card.PinNumber = PinNumber;
    }

1 个答案:

答案 0 :(得分:6)

首先需要创建它们,然后才将值分配给它们的属性。

private Account account = new Account();
private Card card = new Card();