Click to See Complete Forum and Search --> : [RESOLVED] Lists and Classes
HKothari
March 12th, 2008, 07:23 PM
I have this class (for the example "Moo") and I need to add elements of that class to a list, so I tried to create a list by using:
List<Moo>^ moolist; but it didn't work, and I'm really confused as to why it didn't. How would I go through with this?
darwen
March 15th, 2008, 05:09 AM
What errors are you getting ?
Try this :
// This is because List exists in namespace System::Collections::Generic
// If you don't know about namespaces google about them.
using namespace System::Collections::Generic;
void ExampleMethod()
{
List<Moo ^> ^mooList = gcnew List<Moo ^>;
Moo ^moo1 = gcnew Moo;
Moo ^moo2 = gcnew Moo;
Moo ^moo3 = gcnew Moo;
mooList->Add(moo1);
mooList->Add(moo2);
mooList->Add(moo3);
}
Darwen.
HKothari
March 15th, 2008, 08:36 AM
I tried using it like the example, and that didn't compile either, but the error I got with my example in the first post is:
Error 1 error C3225: generic type argument for 'T' cannot be 'Moo', it must be a value type or a handle to a reference type frmMain.h 22
darwen
March 15th, 2008, 08:44 AM
Do
List<Moo ^> ^ <-- notice the ^ before the >
NOT
List<Moo> ^
as you originally had.
And 'Moo' is a .NET class isn't it i.e. declared like
ref class Moo
{
} ;
Darwen.
HKothari
March 15th, 2008, 09:35 AM
Ok, that worked, thanks :D
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.