I'm taking a basic course in C# programming, and this is a part of our assignment. Very new to programming so I feel more than a bit lost with this.
The assignment is to add an ArrayList and insert strings from a file to this, which I hopefully have done with the code below:
Read ()is a method in another class (FileReader) that reads the files from"info.txt"and returns anArrayList.The
ArrayListitems are supposed to store the object items, although I'm not quite sure why I need two arrays?
The problem I have is: when you retrieve the "items" from the array, they have to be cast to a type, string, (if I understand it correctly, otherwise they are returned as objects?). How do I do that?
Can you cast the entire ArrayList?
public PriceFlux () //Constructor
{
ArrayList items;
items = new ArrayList();
FileReader infoFile = new FileReader("info.txt");
items = infoFile.Read();
}
The files from the info.txt looks approximately like this:
G&34&Kellogs K frukostflingor&Sverige&29.50&5/11/2005&29/10/2005&29/10/2006
Here is the FileReader Read() method:
public ArrayList Read ()
{
ArrayList fileContent = new ArrayList ();
try
{
while (line != null)
{
fileContent.Add (line);
line = reader.ReadLine ();
}
reader.Close ();
}
catch
{
Console.WriteLine ("Couldn´t read from file.");
}
return fileContent;
}
Very grateful for suggestions on how to solve this.