By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,910 Members | 1,274 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,910 IT Pros & Developers. It's quick & easy.

Storing objects into a generic list.

P: n/a
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[i] = gcnew RaisinCookies();
accessCookie->cCookies[i]=gcnew ChocolateCookies();
}
}

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

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

accessCookie->cCookies[i]->RecipeTitle = "Choco Chocolate";
accessCookie->cCookies[i]->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?

Mar 29 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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

<Mi**********@gmail.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
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[i] = gcnew RaisinCookies();
accessCookie->cCookies[i]=gcnew ChocolateCookies();
}
}

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

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

accessCookie->cCookies[i]->RecipeTitle = "Choco Chocolate";
accessCookie->cCookies[i]->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?

Mar 30 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.