Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2007

Tip: Looking for answers? Try searching our database.

Storing objects into a generic list.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Miesha.James@gmail.com - 29 Mar 2007 21:57 GMT
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?
Kevin Frey - 30 Mar 2007 03:53 GMT
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

> Hello,
>
[quoted text clipped - 59 lines]
>
> Does anyone have any suggestions?

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.