473,383 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Storing objects into a generic list.

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
1 2153
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

49
by: Steven Bethard | last post by:
I promised I'd put together a PEP for a 'generic object' data type for Python 2.5 that allows one to replace __getitem__ style access with dotted-attribute style access (without declaring another...
10
by: Diego F. | last post by:
Hello. I need to store custom objects in a SQL Server 2000 table. Which is the easiest way to do it? Do I need to write methods to store each attribute separately from C# app to the table and the...
10
by: Mark Rae | last post by:
Hi, This relates to the previous thread "Disappearing Sessions", but is a bit more generic so I thought I'd start a new thread. This one relates to the storing of objects in Session once only to...
0
by: metaperl | last post by:
A Comparison of Python Class Objects and Init Files for Program Configuration ============================================================================= Terrence Brannon bauhaus@metaperl.com...
3
by: Hunter | last post by:
Hi, In one of my forms I need hold an array of objects (same class) that is dynamic at runtime. Instead of using an array I am looking at ArrayList or Hashtable. Any suggestions?
3
by: Seth Gecko | last post by:
Hi I am working with generic lists of various objects and a control dealing with these lists. For instance: A parent form holds: dim Walls as List(Of wall) dim Segments as List(Of segment) ...
3
by: bg_ie | last post by:
Hi, I currently use ArrayList to store a set of objects all of which have the same type. The reason I use ArrayList is because it allows me Add new items to the array. Should I be using...
5
by: Cirene | last post by:
I am writing a shopping cart app in asp.net. So far I created 2 classes, 1 for customer_info (name, address, email), 1 for product_info (prodname, price, description). As the shopper adds...
2
by: Andrus | last post by:
Winforms UI assembly has static FormManager.FormCreator method which creates forms taking entity as parameter. I need to pass this method to business objects in business assembly so that business...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.