none
how do you create and access an array of objects, not an array of handles to those objects

    Question

  • I am trying to create an array of objects of my own class.  I did the following:

    Code Block

        ref class trace_info
         {
         public:
          String^ name;
          String^ via_count;
          String^ delay;
          trace_info() //constructor
          {
            name = gcnew String("");
            via_count = gcnew String("");
            delay = gcnew String("");
          }
        };

            array<trace_info^>^ trace_list;

     

     

     

    I then instatiated trace_list by the following code:

    Code Block

       trace_list = gcnew array<trace_info^>((12000);

     

     

    This compiles.  However, this actually makes an array of handles to objects not an array of objects.  So, if I make the following temporary varrible:

    Code Block
    trace_info^ ti = gcnew trace_info;

     

     

    then fill this variable with info and then do the following in a loop:

    Code Block

    for(i = 0; i!= x; i++)

    {

    fill temporary variable ti from a file then

    trace_list->SetValue(ti, i);

    }

     

    What happens is that all the array handles are pointing to the same temporary variable's last info which is not what I want to do (i.e. if ti's handle is pointing to addres z, then the entire array is also pointing to z and if the data in z changes so does the array).  So how to you create an array of objects that contain managed code so that I can change each object?  In C I would just put and * in front of the variable and all would be good but that does not compile here.
    Thanks

     

     

     

     

    Thursday, December 20, 2007 10:20 PM

Answers

  • Hi~

    I can't understand exactly what do you want.

    Do you want access an array's item?

    It's same as C array.

    Code Snippet

     trace_list[0]->name = "name";
     trace_list[0]->via_count = "ccc";
     trace_list[0]->delay = "100";

     

     

    On your code, all array's items pointing a same object(ti).

     

    And If you going to add to array.

     

    Code Snippet

       ref class trace_info
         {
         public:
          String^ name;
          String^ via_count;
          String^ delay;
          trace_info() //constructor
          {
            name = gcnew String("");
            via_count = gcnew String("");
            delay = gcnew String("");
          }
       trace_info(String^ n, String^ c, String^d)
       {
        name = n;
        via_count= c;
        delay = d;
       }
        };

     

    .................

    ...............

    ............

     

     

     

    trace_list[1] = gcnew trace_info(gcnew String("aaa"),gcnew String("aaa"),gcnew String("aaa")) ;

     

     

    And reference below msdn pages about array and string.

    http://msdn2.microsoft.com/en-us/library/system.array_members(VS.80).aspx

    http://msdn2.microsoft.com/en-us/library/system.string(VS.80).aspx

     

     

    bye~

    Friday, December 21, 2007 8:36 AM
  •  Damon at catapult wrote:

    [...]

    I make the following temporary varrible:

    Code Block
    trace_info^ ti = gcnew trace_info;

     

     

    then fill this variable with info and then do the following in a loop:

    Code Block

    for(i = 0; i!= x; i++)

    {

    fill temporary variable ti from a file then

    trace_list->SetValue(ti, i);

    }

     

    What happens is that all the array handles are pointing to the same temporary variable's last info which is not what I want to do (i.e. if ti's handle is pointing to addres z, then the entire array is also pointing to z and if the data in z changes so does the array).  So how to you create an array of objects that contain managed code so that I can change each object? 

    [...]

     

    If you change this:

     

    Code Snippet

    trace_info^ ti = gcnew trace_info;

    for(i = 0; i!= x; i++)

    {

        fill temporary variable ti from a file then

        trace_list->SetValue(ti, i);

    }

     

     

    to

     

    Code Snippet

    for(i = 0; i!= x; i++)

    {

        trace_info^ ti = gcnew trace_info;

        fill temporary variable ti from a file then

        trace_list->SetValue(ti, i);

    }

     

     

     

    then this solves your problem, I hope.

     

    Friday, December 21, 2007 10:58 AM

All replies

  • Hi~

    I can't understand exactly what do you want.

    Do you want access an array's item?

    It's same as C array.

    Code Snippet

     trace_list[0]->name = "name";
     trace_list[0]->via_count = "ccc";
     trace_list[0]->delay = "100";

     

     

    On your code, all array's items pointing a same object(ti).

     

    And If you going to add to array.

     

    Code Snippet

       ref class trace_info
         {
         public:
          String^ name;
          String^ via_count;
          String^ delay;
          trace_info() //constructor
          {
            name = gcnew String("");
            via_count = gcnew String("");
            delay = gcnew String("");
          }
       trace_info(String^ n, String^ c, String^d)
       {
        name = n;
        via_count= c;
        delay = d;
       }
        };

     

    .................

    ...............

    ............

     

     

     

    trace_list[1] = gcnew trace_info(gcnew String("aaa"),gcnew String("aaa"),gcnew String("aaa")) ;

     

     

    And reference below msdn pages about array and string.

    http://msdn2.microsoft.com/en-us/library/system.array_members(VS.80).aspx

    http://msdn2.microsoft.com/en-us/library/system.string(VS.80).aspx

     

     

    bye~

    Friday, December 21, 2007 8:36 AM
  •  Damon at catapult wrote:

    [...]

    I make the following temporary varrible:

    Code Block
    trace_info^ ti = gcnew trace_info;

     

     

    then fill this variable with info and then do the following in a loop:

    Code Block

    for(i = 0; i!= x; i++)

    {

    fill temporary variable ti from a file then

    trace_list->SetValue(ti, i);

    }

     

    What happens is that all the array handles are pointing to the same temporary variable's last info which is not what I want to do (i.e. if ti's handle is pointing to addres z, then the entire array is also pointing to z and if the data in z changes so does the array).  So how to you create an array of objects that contain managed code so that I can change each object? 

    [...]

     

    If you change this:

     

    Code Snippet

    trace_info^ ti = gcnew trace_info;

    for(i = 0; i!= x; i++)

    {

        fill temporary variable ti from a file then

        trace_list->SetValue(ti, i);

    }

     

     

    to

     

    Code Snippet

    for(i = 0; i!= x; i++)

    {

        trace_info^ ti = gcnew trace_info;

        fill temporary variable ti from a file then

        trace_list->SetValue(ti, i);

    }

     

     

     

    then this solves your problem, I hope.

     

    Friday, December 21, 2007 10:58 AM
  • Using a mixture of the above two replys I was able to get past this issue.  However, I willl have to come back to figure out why things are working the way they are.
    Thanks for both of your help!
    Wednesday, January 02, 2008 5:20 PM