开发者

adding object to list of objects

开发者 https://www.devze.com 2023-03-11 10:03 出处:网络
I\'m getting an exception and I\'m not seeing what I\'m missing. I have an object model called MyCom开发者_如何学运维posedModel and I\'m creating a list of these by adding each element as they\'re bu

I'm getting an exception and I'm not seeing what I'm missing.

I have an object model called MyCom开发者_如何学运维posedModel and I'm creating a list of these by adding each element as they're built.

I have this:

List<MyComposedModel> TheListOfModel = null;
MyComposedModel ThisObject = new MyComposedModel();

foreach (MyComposedModel in some list)
{
 ThisObject.Reset() //clears all properties
 ....
 TheListOfModel.Add(ThisObject)
}

And I'm getting Object reference not set to an instance of an object. on the Add(ThisObject) line.

What am I missing?

Thanks.


You have to initialize TheListOfModel first. Instead of:

List<MyComposedModel> TheListOfModel = null;

do this:

List<MyComposedModel> TheListOfModel = new List<MyComposedModel> ();


TheListOfModel is null, so you can't apply a method on it.

0

精彩评论

暂无评论...
验证码 换一张
取 消