C# Dynamic array of Struct (ArrayList)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • johndale
    New Member
    • Aug 2007
    • 1

    C# Dynamic array of Struct (ArrayList)

    hi,
    I am pretty new to C#, but have some experience with other lang.(delphi,ph p,asp...). I wanted to create dynamic array of structs type, but it wont work. So I google it, and found that C#.NET CAN NOT work with dynamic arrays... (pretty weird)
    So I want to please more experienced C# programmers to help me with my elementary problem, which grows into nightmare :-)

    All I want is simple data structure:

    1. Structs type called Bar
    string a,b,c;

    2. Array of Bar(s) called Bars

    I want it to be accessible through:
    Bars[i].a
    Bars[i].b
    Bars[i].c


    when I create it using ArrayList, than it complaints about dimensions etc. and I can not figure how I can access these values, because when I use Bars.Add() it creates member of Bars but type OBJECT ?? I do not want type object, I simply want to create a set of structs records with not knowing how much of them I will have...

    Thanks very much for your help and tips how to solve this little problem !! I am looking forward to hearing from you. Have a nice day!!
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    An ArrayList will always return objects of the type Object.
    The Object class is the base class for all objects in .Net so all objects in .Net can be converted to Object and back to their original form at any time.

    This is how you can use ArrayList:
    [code=css]
    //
    class Vector3
    {
    public int X;
    public int Y;
    public int Z;

    public Vector3(int x, int y, int z)
    {
    this.X = x; this.Y = y; this.Z = z;
    }
    }
    class Program
    {
    static void Main()
    {
    // Create an ArrayList of Vector3 objects
    System.Collecti ons.ArrayList Vectors =
    new System.Collecti ons.ArrayList() ;

    Vectors.Add(new Vector3(0, 0, 0));
    Vectors.Add(new Vector3(5, 1, 2));
    Vectors.Add(new Vector3(1, 2, 3));

    // Call x in the second object
    // Note, that the object returned by the
    // ArrayList is of the type Object so it needs
    // to be identified as Vector3 before calling
    // the member x
    int x2 = (Vectors[1] as Vector3).X;

    // Get the sum of all vectors in Vectors
    Vector3 sum = new Vector3(0, 0, 0);
    foreach (Vector3 vector in Vectors)
    {
    sum.X += vector.X;
    sum.Y += vector.Y;
    sum.Z += vector.Z;
    }
    }
    }
    [/code]

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      I believe what it is really complaining about is that ArrayList was not designed to work with structs. Change your struct to a class.
      There's really not much use for a struct in .NET languages.

      Comment

      • TRScheel
        Recognized Expert Contributor
        • Apr 2007
        • 638

        #4
        ArrayList is also considered older technology. Use either the new List<Type T> or the older array declaration such as TYPE[] object = new TYPE[n];

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Originally posted by TRScheel
          ArrayList is also considered older technology. Use either the new List<Type T> or the older array declaration such as TYPE[] object = new TYPE[n];
          The biggest difference in ArrayList vs List<Type T> is that List is strongly typed so you don't have to cast it's return values yes?
          And the obvious tradeoff that you can only add the Type T to the List and not anything you want like with ArrayList?

          Comment

          • TRScheel
            Recognized Expert Contributor
            • Apr 2007
            • 638

            #6
            Originally posted by Plater
            The biggest difference in ArrayList vs List<Type T> is that List is strongly typed so you don't have to cast it's return values yes?
            And the obvious tradeoff that you can only add the Type T to the List and not anything you want like with ArrayList?
            [code=cpp]List<object> myarraylist = new List<object>();[/code]

            List<> just gives you a lot more options than ArrayList does by default.

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Originally posted by TRScheel
              [code=cpp]List<object> myarraylist = new List<object>();[/code]
              But then you would have to cast everything anyway right?

              Comment

              • TRScheel
                Recognized Expert Contributor
                • Apr 2007
                • 638

                #8
                Originally posted by Plater
                But then you would have to cast everything anyway right?
                Yes but there are a lot more inherit options available with a List than an ArrayList.

                I really shudder at casting everything anyways, and would rather have two generics than one with objects.

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by TRScheel
                  Yes but there are a lot more inherit options available with a List than an ArrayList.

                  I really shudder at casting everything anyways, and would rather have two generics than one with objects.
                  We are making everything generic in Java.
                  The compiler can really do a better job than the programmer for type checking.

                  Comment

                  • TRScheel
                    Recognized Expert Contributor
                    • Apr 2007
                    • 638

                    #10
                    Originally posted by r035198x
                    We are making everything generic in Java.
                    The compiler can really do a better job than the programmer for type cheking.
                    Agreed wholeheartedly

                    Use three, four, five different generic objects instead of one to hold them all. Casting to and from objects is not only dangerous but also slow

                    Comment

                    Working...