简体   繁体   中英

How to add an item to a list of generics declared as a list of an abstract object in C#

So I have an abstract class named "Account" :

public abstract class Account
{
    private string _FinancialInstitution;

    public string FinancialInstitution
    {
        get { return _FinancialInstitution; }
        ...
    }
}

And I have two other classes that extends from those two:

public class CreditCard : Account
    {
        private DateTime _ExpirationDate;
        ...
    }

and this one:

public class CheckingSavingsAccount : Account
{
    private string _PrimaryAccountHolder;
    ...
}

Now, The whole point was to be able to store either kind of account in a generics collection list, but if I try to do this:

List<Account> lstTemp = new List<Account>();
CreditCard newCC1 = new CreditCard();
lstTemp.Add(new CreditCard());

I got an "Object reference not set to an instance of an object." error on the line that attemps to add the newly credit card object created (lstTemp.Add). What am I doing wrong?


This is the exception detail:

System.NullReferenceException was unhandled
  Message="Object reference not set to an instance of an object."
  Source="mscorlib"
  StackTrace:
       at System.Collections.Generic.List`1.Add(T item)
       at RunAsConsole.Program.Main(String[] args) in C:\Users\ortegae\Documents\Visual Studio 2008\Projects\eStocks50600\RunAsConsole\Program.cs:line 52
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Hmmm... I don't see anything wrong. I think your problem is not related to your list and must be in some other code you haven't shown. I just ran the following test without failure:

public abstract class Base {}
public class D1 : Base {}
public class D2 : Base {}

[Test]
public void Test_Generic_Lists_With_Abstract_Base()
{
    var list = new List<Base>();
    list.Add(new D1());
    list.Add(new D2());

    Assert.That(list[0] is D1);
    Assert.That(list[1] is D2);
}

EDIT Your stack trace does not line up with the code you showed. The return of new can never be null, and your stack trace shows that null was passed in. What else are we missing?

If you have a constructor for Account() or for CreditCard()

public Account()
{
...
}

public CreditCard()
{
...
}

then it may be that the exception is thrown in there. You probably want to check with the Visual Studio debugger to see what's going on.

A useful tip, BTW, is to set Visual Studio to break on all exceptions, whether handled or unhandled. Under Debug, choose Exceptions..., then next to Common Language Runtime Exceptions make sure the checkbox under Thrown is set on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM