|
-
July 27th, 2010, 09:20 PM
#1
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.
-
July 27th, 2010, 09:30 PM
#2
Re: Accessing a List in managed code
 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
-
July 28th, 2010, 02:58 AM
#3
Re: Accessing a List in managed code
-
August 12th, 2010, 01:53 AM
#4
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
-
April 14th, 2011, 05:54 PM
#5
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];
}
-
April 15th, 2011, 04:52 AM
#6
Re: Accessing a List in managed code
public ref class Poly
{
public:
Cord^ c1;
Cord^ c2;
Cord^ c3;
};
-
April 15th, 2011, 09:08 PM
#7
Re: Accessing a List in managed code
 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.
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.
-
April 16th, 2011, 12:50 AM
#8
Re: Accessing a List in managed code
 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|