2
votes

I previously asked a question about my meeting planner app, and how to delete a specific entry in the array. Someone had the interesting suggestion of using a ArrayList, instead of a normal array. I am attempting to learn ArrayList, but to no good result. I can't even get the ToString method to work correctly to write what I want it to into a text file.

I have three important fields:

  • Day is whatever is monthcalendar1.selectionend
  • ToD is whatever is selected in a combo box of times (cmbTime.text)
  • Appointment is whatever is in the textbox field for the appointment description (txtAPPT.text)

My plan is to have each of those three fields to have their own entry in the ArrayList, but when it is saved to text file, those three fields are comma delimited to a line for each appointment. Of course when the text is loaded, it will have to be converted back. Can anyone point me in the right direction or have any other method of going about this with the ArrayList?

3

3 Answers

2
votes

I would recommend using a generic list of your class that houses the data, instead of an arraylist. Then for your comma delimited data, just override the .ToString() function of your class to return the fields with commas between them. Just build in a parse function to read that delimited string back into the class and you are all set.

Public Overrides Function ToString() As String
        Return Day & ',' & ToD & ',' & Appointment
End Function

If you don't need your file to be comma delimited, you should consider using XML serialization for saving it out to a file.

0
votes

I would create a class to encapsulate those three fields, and define a ToString method which outputs them in comma-delimited form. Then you can also give this class a Shared method to parse a comma-delimited string back into an object.

Your ArrayList would then hold objects of this class and output them one per line.

0
votes

I would recommend you use a struct of class as a holder for your three data field. You can then override its ToString() to output the data in the way you want. You can add a function that initializes data members given a string (reading a line of text).

As far as the ArrayList (or preferably List<YourHolderClass>), you can just do a simple for/foreach loop and call ToString on each of the holder elements. Write result to file one line at a time

Hopefully this will point you in the right direction.