Saving an ArrayList to a file

Sadie

Member
Joined
Oct 31, 2005
Messages
7
Location
Cambridge
Programming Experience
1-3
Hi,

I have created an ArrayList called AL to hold the values of 5 textboxes and 2 comboboxes. These all represent one purchase object. The code is as follows:
Dim AL as ArrayList = Nothing
AL =
New ArrayList
AL.Add(objPurchase.RefNum & objPurchase.Type & objPurchase.Weight & objPurchase.Quantity & _
objPurchase.CalcMeth & objPurchase.BuyPrice & objPurchase.SalePrice)

I need to save the contents of the ArrayList to a file. How would I go about this? Hope someone can help!
 
It would be a much, much better idea to create your own class to represent a purchase, which had a property to represent each of those things. You could then save or load an instance of that class to or from an XML file in four lines of code using serialization. You're using an object-oriented language so you should be using proper objects, not loose collections like that.
 
Hi jmcilhinney,

Thank you for your help!

I have created the class:

VB.NET:
Public Class CPurchase
Public RefNum As Integer
Public Type As Integer
Public Weight As Integer
Public Quantity As Integer
Public CalcMeth As Integer
Public BuyPrice As Integer
Public SalePrice As Integer
Public Price As Integer
End Class

When you say use a property to represent the above fields, do you mean:

VB.NET:
Private variable as DataType
Public Property name() as DataType
   Get
       Return variable
   End Get
   Set
       variable = Value
   End Set
End Property
 
That's exactly what I mean. It is the .NET convention to expose values publicly through properties rather than the actual variables themselves. There is a (very, very) slight perfromance hit using properties because you are basically calling a method, but if you're interested to know what the advantages of properties are then MSDN has plenty of info.
 
Back
Top