none
Arraylist update RRS feed

  • Question

  • I have the below sample code
    class Program 
        { 
            static void Main(string[] args) 
            { 
                testType testme = new testType(); 
     
                ArrayList ar = new ArrayList(); 
                object[] obj = new object[2]; 
                obj[0] = testme.myType; 
                obj[1] = "HelloWorld"
     
                ar.Add(obj); 
                 
                //Read back  
                object[] getObj = (object[]) ar[0]; 
                Type t = getObj[0] as Type; 
                if (t != null
                { 
                    Console.WriteLine(t.FullName); 
                    Console.WriteLine(getObj[1].ToString()); 
                } 
     
                //Set again 
                testme.setme(); 
     
                //Read back -- I want the set value, Int32 to be shown in the ArrayList 
                getObj = (object[])ar[0]; 
                t = getObj[0] as Type; 
                if (t != null
                { 
                    Console.WriteLine(t.FullName); 
                    Console.WriteLine(getObj[1].ToString()); 
                } 
                            
            } 
     
            public class testType 
            { 
                public Type myType; 
     
                public testType() 
                { 
                    myType = typeof(string); 
                } 
     
                public void setme() 
                { 
                    myType = typeof(Int32); 
                }           
            } 
        } 

    I know it is simple, and I am missing some basic thing here. I should reference the testType's myType object so that, any changes to it should be reflected in the Arraylist.

    Please let me know how to achieve this.

    Thanks,
    mecspek
    Tuesday, January 13, 2009 6:13 PM

Answers

  •  You should consider using generic collection. I don't really get the meaning of this but here's a little modified version of your code :

    class Program  
      {  
        static void Main(string[] args)  
        {  
          testType testme = new testType();  
     
          ArrayList ar = new ArrayList();  
          object[] obj = new object[2];  
          obj[0] = testme;  
          obj[1] = "HelloWorld";  
     
          ar.Add(obj);  
     
          //Read back     
          object[] getObj = (object[])ar[0];  
          testType t = getObj[0] as testType;  
     
          if (t != null && t.myType != null )  
          {  
            Console.WriteLine(t.myType.FullName);  
            Console.WriteLine(getObj[1].ToString());  
          }  
     
          //Set again    
          t.setme();  
     
          //Read back -- I want the set value, Int32 to be shown in the ArrayList    
          getObj = (object[])ar[0];  
          t = getObj[0] as testType;  
     
          if (t != null && t.myType != null)  
          {  
            Console.WriteLine(t.myType.FullName);  
            Console.WriteLine(getObj[1].ToString());  
          }  
     
          Console.ReadLine();  
        }  
      }  
     
      public class testType  
      {  
        public Type myType;  
     
        public testType()  
        {  
          myType = typeof(string);  
        }  
     
        public void setme()  
        {  
          myType = typeof(Int32);  
        }  
      }   


    • Marked as answer by mecspek Tuesday, January 13, 2009 7:54 PM
    Tuesday, January 13, 2009 6:35 PM

All replies

  •  I fail to see how this changes anything in your object obj:

    //Set again
    testme.setme ( );


    AlexB
    Tuesday, January 13, 2009 6:24 PM
  •  Also I noticed that in the code above that statement there is a vacuum also:

    obj[ 0 ] = testme.myType;

    myType is null I believe at this point.


    AlexB
    Tuesday, January 13, 2009 6:26 PM
  • The problem here is that you're not saving the actual "testType" instance in the ArrayList anywhere. 

    In other words, you have two references to typeof(string).  You have the reference in testType, and you have a second reference in the array located in ArrayList, at ((object[])ar[0])[0].  This second reference isn't updated when you update the first reference (the reference in testType, called "myType".  The only way to solve this issue is to add the actual instance of testType into the ArrayList instead of creating references within the array to the objects that are also referenced in testType.  Your whole structure here is confusing as well.  I would suggest rewriting it as something much simpler. Use generics.  Something like this might work:

    using System;

    using System.Runtime.Serialization.Formatters.Binary;

    using System.IO;

    using System.Collections;

    using System.Collections.Generic;

    using System.Linq;

     

    class Program 

        static void Main(string[] args) 

        {

            Dictionary<string, testType> listOfTestType = new Dictionary<string, testType>();

     

            testType testme = new testType();

     

            listOfTestType.Add("HelloWorld", testme);

             

            //Read back  

            KeyValuePair<string, testType> getObj = listOfTestType.ToList()[0];

            Console.WriteLine(getObj.Value.myType.FullName); 

            Console.WriteLine(getObj.Key); 

     

            //Set again 

            testme.setme(); 

     

            //Read back -- I want the set value, Int32 to be shown in the ArrayList 

            getObj = listOfTestType.ToList()[0];

            Console.WriteLine(getObj.Value.myType.FullName);

            Console.WriteLine(getObj.Key); 

     

            Console.ReadLine();

        } 

     

        public class testType 

        { 

            public Type myType; 

           

            public testType() 

            { 

                myType = typeof(string); 

            } 

     

            public void setme() 

            { 

                myType = typeof(Int32); 

            }           

        } 


    David Morton - http://blog.davemorton.net/
    Tuesday, January 13, 2009 6:28 PM
    Moderator
  •  You should consider using generic collection. I don't really get the meaning of this but here's a little modified version of your code :

    class Program  
      {  
        static void Main(string[] args)  
        {  
          testType testme = new testType();  
     
          ArrayList ar = new ArrayList();  
          object[] obj = new object[2];  
          obj[0] = testme;  
          obj[1] = "HelloWorld";  
     
          ar.Add(obj);  
     
          //Read back     
          object[] getObj = (object[])ar[0];  
          testType t = getObj[0] as testType;  
     
          if (t != null && t.myType != null )  
          {  
            Console.WriteLine(t.myType.FullName);  
            Console.WriteLine(getObj[1].ToString());  
          }  
     
          //Set again    
          t.setme();  
     
          //Read back -- I want the set value, Int32 to be shown in the ArrayList    
          getObj = (object[])ar[0];  
          t = getObj[0] as testType;  
     
          if (t != null && t.myType != null)  
          {  
            Console.WriteLine(t.myType.FullName);  
            Console.WriteLine(getObj[1].ToString());  
          }  
     
          Console.ReadLine();  
        }  
      }  
     
      public class testType  
      {  
        public Type myType;  
     
        public testType()  
        {  
          myType = typeof(string);  
        }  
     
        public void setme()  
        {  
          myType = typeof(Int32);  
        }  
      }   


    • Marked as answer by mecspek Tuesday, January 13, 2009 7:54 PM
    Tuesday, January 13, 2009 6:35 PM