How to access Array elements in an object that is in a List?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sal Sal
    New Member
    • Nov 2010
    • 15

    #1

    How to access Array elements in an object that is in a List?

    Hi,

    I have an object, and inside are different variables that I populate during runtime, one of the is an array of string

    Code:
    [B]string [] array = null;[/B]
    So I populate it like this
    Code:
    [B]object.array[0] = "something";[/B]
    Then I have a List of these objects like this and add my objects to it, perform stuff on the list like sorting and search etc.
    Code:
    [B]List<object> objectList = new List<object>;
    
    objectList.Add(object1);
    objectList.Add(object2);
    ...[/B]
    At the end I wanted to iterate through the list and for a certain object I want to access the array and print out the contents. I tried the following and it does not work and I don't understand why.

    Code:
    [B]foreach(object o in objectList){
    
       foreach(string s in o.array){
    
          Console.Writeline(s);
    
       }
    }[/B]
    I get an error in the debugger that says I have to instantiate a new object using the new keyword. I don't get it, the object already exists and I can see values in the array when I watch the variable. How do I access the array values? Thanks in advance for any help
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I think you might need to instantiate your array object. You can't access element 0 (line 1 of second code box) unless you've done something like...

    Code:
    array = new string[COUNT];
    Also, as object is a reserved word in C# I assume you're just using that to mean some object you've defined, and not the actual name of your class?

    Comment

    • Sal Sal
      New Member
      • Nov 2010
      • 15

      #3
      Yes I'm only using the word object as a name here, its called something else in my program. I don't give my arrays a length at the beginning (i.e. when I instantiate the variable inside the object class) because each object will have a different array size.

      Code:
      [B]private string [] Array = null;[/B]
      
      [B]public string [] array
      {
       get { return this.Array; }
       set { this.Array = value; }
      }[/B]
      I populate the arrays using the Split() method, to split a string into pieces. I set each objects array to the result of the split. and each one has a different count.

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        Well, the thing is, according to the code you posted you aren't populating it with a string.Split call. You said...

        So I populate it like this
        Code:
        object.array[0] = "something";
        According to that you're trying to assign the string "something" to the array element 0 when array is very likely null. Unless you didn't post something...

        Anyway, using the code you posted here I worked through a quick test and don't seem to be having any trouble. Can you compare this code to yours and see if there are any discrepancies?

        Code:
            class Program
            {
                static void Main(string[] args)
                {
                    string testStr1 = "this/is/a/test/of/stuff";
                    string testStr2 = "oooh/how/about/another/test?";
        
                    TestObj obj1 = new TestObj("obj1");
                    obj1.Array = testStr1.Split(new char[] { '/' });
        
                    obj1.Array[0] = "DIFFERENT";
        
                    TestObj obj2 = new TestObj("obj2");
                    obj2.Array = testStr2.Split(new char[] { '/' });
        
                    List<TestObj> objList = new List<TestObj>();
                    objList.Add(obj1);
                    objList.Add(obj2);
        
                    foreach (TestObj o in objList)
                    {
                        Console.WriteLine("Object: " + o.Name);
                        foreach (string s in o.Array)
                        {
                            Console.WriteLine("  " + s);
                        }
                    }
                }
            }
        
            class TestObj
            {
                private string[] m_array = null;
                private string m_name;
        
                public TestObj(string name)
                {
                    m_name = name;
                }
        
                public string Name
                {
                    get { return m_name; }
                    set { m_name = value; }
                }
        
                public string[] Array
                {
                    get { return m_array; }
                    set { m_array = value; }
                }
            }

        Comment

        • Sal Sal
          New Member
          • Nov 2010
          • 15

          #5
          Sorry you can ignore that other code I posted
          Code:
          object.array[0] = "something";
          I mixed up the order in which I was doing this, I populate the array first by assigning the Split result to the array, basically copies the array into my variable. Then later on I do some searching text replacing/modification on the array elements depending on some conditions. (checking if its null first of course)

          I'll compare your code to mine, although it looks similar I'll see if there is somethingi am missing. Thanks

          Comment

          • Sal Sal
            New Member
            • Nov 2010
            • 15

            #6
            I think I know the problem, although your code works, in this case we are guaranteed that the array will be instantiated with the Split results. So it will never be null.

            In my code the location where I populate each object array is nested inside other code. I have thousands of objects and some of them are null and don't get initialized.

            I'm running another test but does the foreach loop take into account a null array or do I have to surround it with an if statement like this

            Code:
            if(object.array != null){
            
            foreach(string s in object.array){
            
              Console.Writeline(s);
            }
            
            }

            Comment

            • GaryTexmo
              Recognized Expert Top Contributor
              • Jul 2009
              • 1501

              #7
              Yea you have to check for null before trying to do a foreach on an object. When you use foreach, it gets the enumerator for an object (that inherits from IEnumerable) and it can't access that if the object is null.

              Comment

              • Sal Sal
                New Member
                • Nov 2010
                • 15

                #8
                Interesting, it works now. Thanks very much :)

                Comment

                Working...