Thread: ArrayLists + Inner Structs

  1. #1
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187

    ArrayLists + Inner Structs

    I've created an ArrayList of structs of structs.

    What's the best way to access the inner structs ?
    an Enumerator is iterating through the ArrayList,
    and you can access current.

    I'm guessing you need get/set functions
    (accessors i think) for both structs to do this ?

    The Idea in C++ :
    Code:
    iter_x->struct1_NAME->part_x = "I want to Change this container"

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    I have not used structs widely in my programs, because they are value types and copy their data each time they are given to a function, but with a class with inner classes, you could make each inner class a property of the outer class with set/get accessors. Maybe only get accessors if you don't need writing access.

    PHP Code:
    using System;
    using System.Collections

    namespace 
    ConsoleApplication1
    {
        public class 
    B
        
    {
            private 
    string m_Name;
        
            public 
    Bstring aName )
            {
                
    m_Name aName;
            }

            public 
    string Name
            
    {
                
    get { return m_Name; }
                
    set m_Name value; }
            }
        }

        public class 
    A
        
    {
            private 
    B m_InnerClass;
            
            public 
    Astring BName )
            {
                
    m_InnerClass = new BBName );
            }

            public 
    B InnerClass
            
    {
                
    get { return m_InnerClass; }
                
    set m_InnerClass value; }
            }
        }

        class 
    Class1
        
    {
            [
    STAThread]
            static 
    void Main(string[] args)
            {
                
    ArrayList list = new ArrayList();

                for( 
    int i 10 i++ )
                {
                    
    // Add a new Instance of class A to the ArrayList
                    
    list.Add( new A"Class #" i.ToString() ) );
                    
    Console.WriteLine"Class #" i.ToString() + "created." ); 
                }

                
    Console.WriteLine"Elements in list:" ); 

                foreach( 
    A Element in list )
                {
                    
    // this is your C++ line:
                    // Element is the object
                    // .InnerClass calls the property and returns the Innerclass
                    // .Name returns the Name of the InnerClass
                    
    Console.WriteLineElement.InnerClass.Name );
                }

                
    Console.WriteLine"Done now, Garbage collection takes care of the rest" ); 
                
    Console.Read(); 
            }
        }

    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    I agree with nvoigt. I would use a class instead of structs. Throughout my C# programming (only a few weeks) I haven't seen much use for structs. Classes have been fine for me so far.

  4. #4
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    Isn't there a way to do the same thing but with array indexers

    //The 3rd element in the array list

    list[3].A.B-stuff

    p.s.
    Rough Idea/Guess

  5. #5
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187

    more basic...

    Having the ability to iterate easily one by one
    and being able to tell if i'm at the first/last rec is necessary.

    is an ArrayList() the best method of storage ?

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Yes, you can do it with an ArrayIndexer like in plain old C.

    An ArrayList is the best method of storage if you have a number of elements that is growing or shrinking only by manipulating the last element ( i.e. adding at the end, deleting from the end ).
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  2. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 02:46 AM
  3. Files, Structs, and Pointers
    By Mr Moe in forum C++ Programming
    Replies: 5
    Last Post: 02-15-2005, 08:33 PM
  4. packed structs
    By moi in forum C Programming
    Replies: 4
    Last Post: 08-20-2002, 01:46 PM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM