Click to See Complete Forum and Search --> : Accessing a List in managed code


codeMojo
July 27th, 2010, 09:20 PM
Hi Guys,

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

Here's my 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.

Paul McKenzie
July 27th, 2010, 09:30 PM
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++.

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

Marc G
July 28th, 2010, 02:58 AM
[ moved thread ]

mani3355
August 12th, 2010, 01:53 AM
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

ninetyCent
April 14th, 2011, 05:54 PM
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];
}

Alex F
April 15th, 2011, 04:52 AM
public ref class Poly
{
public:
Cord^ c1;
Cord^ c2;
Cord^ c3;
};

Eri523
April 15th, 2011, 09:08 PM
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.

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:


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.)

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:


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 (http://www.codeguru.com/forum/misc.php?do=bbcode#code) when posting code.

But, at any rate, welcome to CodeGuru! :)

Alex F
April 16th, 2011, 12:50 AM
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 :(