How to instantiate multiples of the same objects, such as order line item.  
Author Message
AshleyT





PostPosted: .NET Base Class Library, How to instantiate multiples of the same objects, such as order line item. Top

To create an order I have to create a OrderHeader class and a OrderLineItem class. When I read through the OrderLineItems from an XML.

So have create a OrderHeader and OrderLineItem classes. When I read the xml for the OrderHeader information I instantiate an orderheader object. And then the same for an orderlineitem. But when I go to the next line item how to do I handle that second orderlineitem object Or do I have to work with each orderlineitem completely seperate, and loss the the orderlineitem ahead of it Any suggestions for examples is greatly appreciated.

Thanks,



.NET Development21  
 
 
Sean Hederman





PostPosted: .NET Base Class Library, How to instantiate multiples of the same objects, such as order line item. Top

Usually you'd add the OrderLineItem to a collection or something, and then move on to the next line. Alternatively, if you're reading from XML, why not just use a DataSet



 
 
Indian Ocean





PostPosted: .NET Base Class Library, How to instantiate multiples of the same objects, such as order line item. Top

Hi,

You may have the following options,

  1. If you want to persist that collection of OrderLineItems then you should use collection API to store those items for each line. For each line create new OrderLineItem object and then add into the collection
  2. If you dont want to persist the OrderLineItems instances and you are doing some immediate operations on it and then you dont need it anymore before you read other item, then you should use the same object to fill up the information from XML and then do the required process on it and so on..

HTH,



 
 
AshleyT





PostPosted: .NET Base Class Library, How to instantiate multiples of the same objects, such as order line item. Top

Thank you very much.  I found the MSDN solutions where I created a class that creates a list.  Public class myList : Implements IEnumerable, IEnumerator.

I created the list, not passing any of my objects to it.  Now how do I pass my own class object that I have created and set the properties for to the list   How do I add it to the list   In my loop as I set the properties for the object, I then want to add it to the myList.

Any suggestions of examples Or how do I do this   Thanks.,

 

 


 
 
Indian Ocean





PostPosted: .NET Base Class Library, How to instantiate multiples of the same objects, such as order line item. Top

Hi,

IEnumerable and IEnumerator provides features for iterating the collection items but not add, remove, count etc.

So better you use ICollection which itself inherits from IEnumerable or IList (to access individual items by Index) which itself inherits from ICollection for more features.

ICollection and IList available under System.Collections.Generic for manipulating generic types and System.Collections for non-generic type.

As per your need you should go for System.Collections.ICollection for collection related add, remove, count, etc features and if you wish to access/manipulate collection items by Index then you should go for System.Collections.IList
Please note that you will need to inherit IEnumerator explicitely if required because above interfaces are inheriting from IEnumerable not IEnumerator

You will find good examples in MSDN document for above interfaces,
http://msdn2.microsoft.com/en-gb/library/system.collections.icollection.aspx
http://msdn2.microsoft.com/en-us/library/system.collections.ilist.aspx

For more advanced collection api, you should check System.Collections namespace and find proper APIs as per your need.

HTH,