0

I want to iterate through an ArrayList containing ArrayLists of different object type and write the data to a console.

I tried to use IEnumerable and foreach-loop.

//-------------------- Custom class Point --------------------
class Point
{
    public double X { get; set; }
    public double Y { get; set; }
    public double Z { get; set; }
    public Point(double x, double y, double z) { this.X = x; this.Y = y; this.Z = z; }
}
//-------------------- Main program --------------------
class Program
{
    static void Main(string[] args)
    {
        //ArrayList of different objects
        ArrayList arrlist = new ArrayList{
            new ArrayList { 1, "one" ,new Point(1.0,1.0,1.0)},
            new ArrayList { "two", 2,new Point(2.0,2.0,2.0) },
            new ArrayList { new Point(3.0,3.0,3.0), "three",3}
        };
        readData(arrlist);
        Console.ReadLine();
    }
    //-------------------- readData() function definition --------------------
    public static void readData(ArrayList arlst)
    {
        foreach (object obj in arlst)
        {
            foreach (object item in (IEnumerable)obj)
            {
                Console.WriteLine($"... {(IEnumerable)item.ToString()} ...");
            }
        }
    }
}

I expect to write the real value of each item in ArrayList as entered.

Edit: Formatting

B0RDERS
  • 217
  • 1
  • 8
arash deilami
  • 25
  • 1
  • 1
  • 7
  • Strange that a method named "readData" actually writes data... – Rufus L Jul 13 '19 at 02:32
  • Also, `ArrayList` is [practically obsolete](https://stackoverflow.com/questions/5063156/why-isnt-arraylist-marked-obsolete). You might consider using `List` instead. – Rufus L Jul 13 '19 at 02:34

3 Answers3

0

Is this the output you are looking for?

... 1 ...
... one ...
... Point(1,1,1) ...
... two ...
... 2 ...
... Point(2,2,2) ...
... Point(3,3,3) ...
... three ...
... 3 ...

If you want your point to print, you need a toString() method like this in your Point class

override public string ToString() { return $"Point({X},{Y},{Z})"; }
B0RDERS
  • 217
  • 1
  • 8
  • 1
    Hi Andrew, I want to see the value for each Point as it's entered; I mean Point(1.0,1.0,1.0) instead of Test_App.Point – arash deilami Jul 12 '19 at 20:02
  • I understand. I added a fix to my solution. – B0RDERS Jul 12 '19 at 20:05
  • Is there another way of getting the same result without overriding the ToString() method!? – arash deilami Jul 12 '19 at 22:29
  • There is no defined ToString() method in your Point class, so the ToString() just prints TEST_APP.Point. Why do you want to avoid overriding this? – B0RDERS Jul 15 '19 at 14:27
  • What do you mean by its real type? Point is the object's "real type". Since you defined that type, there is no default ToString() method. If you want a class with a predefined ToString, use the Point3D struct: https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.media3d.point3d?redirectedfrom=MSDN&view=netframework-4.8 – B0RDERS Jul 16 '19 at 21:01
  • getting direct access to data with its real type (from foreach-loop without later casting) is due to my next step to transpose the data. That's also why I avoided generics. Because of this 2nd step, using ToString() looks like a hack to me. I'm not a software engineer or programmer and as a beginner, I might have a wrong understanding of the concept. – arash deilami Jul 16 '19 at 21:06
  • I could be wrong, but I think your ArrayList objects are still being converted to generic objects. If you want to uncast them from generic objects, you can do something like this: https://stackoverflow.com/a/106351/11736511 – B0RDERS Jul 16 '19 at 21:12
  • I mean what if I want to change ReadData() method to return object instead of void? – arash deilami Jul 16 '19 at 21:17
  • 1
    Andrew, I wanted to say that your ToString() solution is one of the easiest and best options based on the information that I had shared at the beginning and nothing is wrong with that. Frankly, I caused the issue by not describing the case completely at the beginning. Now I'm getting the point better. Thank you for your time and consideration, I really appreciate that. – arash deilami Jul 17 '19 at 14:01
0
//-------------------- Custom class Point --------------------
class Point
{
    public double X { get; set; }
    public double Y { get; set; }
    public double Z { get; set; }
    public Point(double x, double y, double z) { this.X = x; this.Y = y; this.Z = z; }
}
//-------------------- Main program --------------------
class Program
{
    static void Main(string[] args)
    {
        //ArrayList of different objects
        ArrayList arrlist = new ArrayList{
            new ArrayList { 1, "one" ,new Point(1.0,1.0,1.0)},
            new ArrayList { "two", 2,new Point(2.0,2.0,2.0) },
            new ArrayList { new Point(3.0,3.0,3.0), "three",3}
        };
        readData(arrlist);
        Console.ReadLine();
    }
    //-------------------- readData() function definition --------------------
    public static void readData(ArrayList arlst)
    {
        foreach (ArrayList l in arlst)
        {
            foreach (object item in l)
            {
                Console.WriteLine($"... {item.ToString()} ...");
            }
        }
    }
}
Stu
  • 15,675
  • 4
  • 43
  • 74
0

You could use it as an arraylist to loop on the arraylists, for sample, see the comments:

public static void readData(ArrayList arlst)
{

    foreach (object l in arlst)
    {
        // try to convert it to arrayList to keep 
        var data = l as ArrayList;safe!
        if (data != null)
           // it is an arrayList, loop on it an print values
           foreach (object item in data)
               Console.WriteLine($"... {item.ToString()} ...");
        else
           // print the value if it is not an array list
           Console.WriteLine($"... {item.ToString()} ...");
    }
}
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194