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.
Re: Accessing a List in managed code
Quote:
Originally Posted by
codeMojo
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
Re: Accessing a List in managed code
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
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];
}
Re: Accessing a List in managed code
public ref class Poly
{
public:
Cord^ c1;
Cord^ c2;
Cord^ c3;
};
Re: Accessing a List in managed code
Quote:
Originally Posted by
ninetyCent
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.
Quote:
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.)
Quote:
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! :)
Re: Accessing a List in managed code
Quote:
Originally Posted by
Eri523
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 :(