0

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 an ArrayList.

  • The ArrayList items 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.

3 Answers 3

5

You can use linq to do this easily:

This will cast all items to string and return an IEnumerable<string>. It will fail if any items can't be cast to a string:

items.Cast<string>();

This will cast all items that can be to a string and skip over any that can't:

items.OfType<string>();
Sign up to request clarification or add additional context in comments.

Comments

1

You can access single elements in the ArrayList doing a cast, for example...

string s = myArrayList[100] as string;
myArrayList.Remove("hello");
myArrayList[100] = "ciao"; // you don't need a cast here.

You can also iterate through all elements without a cast...

foreach (string s in myArrayList)
    Console.WriteLine(s);

You can also use CopyTo method to copy all items in a string array...

string[] strings = new string[myArrayList.Count];
myArrayList.CopyTo(strings);

You can create another List<string> with all items in the ArrayList. Since ArrayList implements IEnumerable you can call the List<string> constructor.

List<string> mylist = new List<string>(myArrayList);

But this doesn't makes much sense... why you don't just use List<string> directly? Using directly a List<string> seems more useful to me, and is faster. ArrayList still exists mostly for compatibility purposes since generics were introduced in version 2 of the language.

I just noticed that there may be an error in your code:

    while (line != null)
    {   
        fileContent.Add (line);
        line = reader.ReadLine ();
    }

Should be instead

    for (;;)
    {   
        string line = reader.ReadLine();
        if (line == null)
            break;
        fileContent.Add(line);
    }

2 Comments

Sure, but Maybe the idea is to teach them how you can exploit the Object class for this stuff in C#/.NET. After all its a home work assignment.
In the assignment we have to use ArrayList, I did read that it might be better to use List<string> though... Thankyou for the suggestions!
0

You cast each element on its own before using it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.