CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2010
    Posts
    4

    Accessing a List in managed code

    Hi Guys,

    I need some help in accessing elements from a list. I am using Visual Studio 2008 (c++).

    Here's my code:
    Code:
    using namespace System::Collections::Generic;
    
    public ref class Cord
    {
    public: int numCord;
    public: int x, y, z;
    };
    
    public ref class Poly
    {
    public: Cord c1, c2, c3;
    };
    
    List<Cord^>^ cordList = gcnew List<Cord^>();
    List<Ploy^>^ polyList = gcnew List<Poly^>();
    
    //Add cords to the list
    for(int i =0; i < 12; i++)
    {
    Cord^ newCord = gcnew Cord();
    
    newCord->x = i+1;
    newCord->y = i+2;
    newCord->z = i+3;
    newCord->numCord = i;
    
    cordList->Add(newCord)
    }
    
    //Add polys to the list
    for(int i = 0; i < 3; i++)
    {
    Poly^ newPoly = gcnew Poly();
    newPoly->c1 = cordList[i];
    newPoly->c2 = cordList[i+1];
    newPoly->c3 = cordList[i+2];
    polyList->Add(newPoly);
    }
    now every thing upto adding the cords into cordList works fine. But when I try to do newPoly->c1 = cordList[i]; etc.... and try to compile I get the error
    error C2582: 'operator =' function is unavailable in 'Cord'
    I have researched this error and looked at the msdn help but couldn't find a solution for it.
    I have no idea why this is not working. c1,c2,c3 are of type Cord and cordList contains Cords. I am not sure if I am using the Cord class properly to declare the Poly class.
    Any help would be appreciated as I am totally stumped.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Accessing a List in managed code

    Quote Originally Posted by codeMojo View Post
    Hi Guys,

    I need some help in accessing elements from a list. I am using Visual Studio 2008 (c++).
    and Managed C++.

    Please post in the Managed C++ forum, as this forum is not for Managed C++ questions. The code, as far as this forum is concerned, is invalid, as no such thing exists in non-Managed C++.
    Code:
    List<Cord^>^ cordList = gcnew List<Cord^>();
    List<Ploy^>^ polyList = gcnew List<Poly^>();
    That "^" symbol is invalid in that context. That is the "exclusive-or" operator in C++.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Accessing a List in managed code

    [ moved thread ]
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  4. #4
    Join Date
    Aug 2010
    Posts
    51

    Smile Re: Accessing a List in managed code

    hi,

    Managed C++ forum, as this forum is not for Managed C++ questions. The code, as far as this forum is concerned, is invalid, as no such thing exists in non-Managed C++.
    I have researched this error and looked at the msdn help but couldn't find a solution for it.



    regards,
    phe9oxis,
    http://www.guidebuddha.com

  5. #5
    Join Date
    Apr 2011
    Posts
    1

    Re: Accessing a List in managed code

    Don't know if you guys still need help but to access managed list but there are two ways:



    System::Collections::IEnumerator ^enumer = cordList->GetEnumerator();
    Cord ^ptr = nullptr;
    while (enumer->MoveNext())
    {
    ptr = dynamic_cast<Cord^>(enumer->Current);

    //you've accessed a Cord item in cordList
    }

    or...

    I'm not sure if this is a good way but

    Cord ^ptr = nullptr;
    for (int i = 0; i < cordList->Count; i++)
    {
    ptr = cordList->ToArray()[i];
    }

  6. #6
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Accessing a List in managed code

    public ref class Poly
    {
    public:
    Cord^ c1;
    Cord^ c2;
    Cord^ c3;
    };

  7. #7
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Accessing a List in managed code

    Quote Originally Posted by ninetyCent View Post
    Don't know if you guys still need help [...][.]
    Probably not. The OP hasn't been online here since writing that post and that's almost 8 months ago.

    Code:
    System::Collections::IEnumerator ^enumer = cordList->GetEnumerator();
    Cord ^ptr = nullptr;
    while (enumer->MoveNext())
    {
      ptr = dynamic_cast<Cord^>(enumer->Current);
      
      //you've accessed a Cord item in cordList
    }
    Works, but there's a syntactic "shortcut" that essentially does the same, is much less typing effort and also looks much neater:

    Code:
      for each (Cord ^ptr in cordList)
      {
        Console::WriteLine(ptr);
        //you've accessed a Cord item in cordList
      }
    (I added a quick ToString() overload to the Cord class for checking, so the Console::WriteLine() call actually does something more or less useful.)

    Code:
    Cord ^ptr = nullptr;
    for (int i = 0; i < cordList->Count; i++)
    {
      ptr = cordList->ToArray()[i];
    }
    Works as well, but I'm afraid it's terribly inefficient because it constructs and fills an entire array in each iteration, just to pick out one of the items and then leave the array to the garbage collector to be reclaimed. And the entire array mumbo-jumbo doesn't even gain the slightest bit, since the List<T> itself is random-accessible through an indexer property:

    Code:
      for (int i = 0; i < cordList->Count; i++)
      {
        Console::WriteLine(cordList[i]);
      }
    Besides, I think Alex F has addressed the OP's actual problem in his (pretty short) post following yours.

    Please use code tags when posting code.

    But, at any rate, welcome to CodeGuru!
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  8. #8
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Accessing a List in managed code

    Quote Originally Posted by Eri523 View Post
    Probably not. The OP hasn't been online here since writing that post and that's almost 8 months ago.
    OMG! I fill stupid in such situations! It always starts from somebody posting an answer to very old question. This time it was banned mani3355 and his spam post

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured