Storing objects into a generic list.

  • Thread starter Thread starter Miesha.James
  • Start date Start date
M

Miesha.James

Hello,

I'm trying to rewrite visual c++ code into visual c++ .NET code and
I've run across a problem with storing objects into a list.

Here;s an example of the code I have:

ref struct Cookies
{
String^ Name;
array< RaisinCookies ^>^ rCookies;
array< ChocolateCookies ^>^ cCookies;
}

ref struct RaisinCookies
{
String^ RecipeTitle;
String^ shortDesc;
}

Ref struct ChocolateCookies
{
String^ RecipeTitle;
String^ shortDesc;
}

Static Generic::List<Cookies^>^ m_cookieRecipeList = gcnew
Generic::List<Cookies^>();

Cookies^ accessCookie;

Initialize()
{
accessCookie->rCookies = gcnew array<RaisinCookies^>(20);
accessCookie->cCookies = gcnew array<ChocolateCookies^>(20);

for( int I = 0; I < 20; i++ )
{
accessCookie->rCookies = gcnew RaisinCookies();
accessCookie->cCookies=gcnew ChocolateCookies();
}
}

The problem is when the data when I store new data into the
accessCookie like so

accessCookie->rCookies->RecipeTitle = "Mary's Gourmet Raisin
Cookies";
accessCookie->rCookies->shortDesc = "Sweet treat full of raisins."
M_cookieRecipeList->Add(accessCookie);

accessCookie->cCookies->RecipeTitle = "Choco Chocolate";
accessCookie->cCookies->shortDesc = "Mm hm good."
M_cookieRecipeList->Add(accessCookie);

The second sCookie data overwrites the raisin data. So, I'm trying to
figure out how I can store new RaisinCookies objects in the list
without it overwriting the previous entry.
I've tried to use accessCookie = gcnew RaisinCookies. But a
NullException is thrown on the ChocolateCookies and RaisinCookies objs
when you try to use them as written above.

Does anyone have any suggestions?
 
I'm not sure I fully understand your problem, because I don't really
understand what the data structure is trying to model in a real-world sense,
but the first question I have to ask is why are you using a fixed-size array
in the first place?

Why not just use a generic List< > and add items to the end of that list as
needed?

Kevin
 
Back
Top