C# List<t> problem when adding Classes to it

Started by
3 comments, last by Jaap85 11 years, 8 months ago
Good afternoon everybody,

i am writing a program that gathers data at certain moments in time (for example: after every 30 seconds). This data is organised into classes.

So what i want to have eventually is a List of Classes. This itself is not a big problem, since i can easily create a list. The one thing i don't get is when i add items to the list. Let's say i have the following List:


List<Market> listMarkets = new List<Market>();


That list works just fine.

Now let's say i want to add a Market to the list of markets every 30 seconds. How do i name this Market? So far i have created an array:


Market[] MarketArray = new Market[100];


Now at every time update, i create a new entry into the array and then add that to the list. This is (as far as i know) the only way to give the new entry a unique name. Is this correct or is there a simpler way?

I feel like it should be able to work without using the array and just adding the new item to the list directly (like you can do with strings), but i cant figure out how to make sure that my class has a unique name.

this might be a bit vague, but i hope anyone can help me. Any thoughts or ideas are appreciated.

My personal blog on game development!

Black Wolf Game Development

Advertisement
I don't understand what you mean by "unique name". Each new entry in the list can already be identified by its list index, which is by definition unique with each list (e.g. no two separate instances can have the same list index at the same time).
If you need a way to identify list items given only a reference, you can always add a property to your class like a time stamp or just use the list index:
[source lang="csharp"]// 1) Unique name using time of instantiation
class Market1
{
public Market1()
{
Name = dateTime.UtcNow.Ticks;
}

long Name { get; private set; }
}

// 2) Unique name using list index (example only works for single list per class type)
class Market2
{
// here the constructor not only assign the name but also adds the instance to the list
public Market2(IList&lt;Market2&gt; list)
{
Name = list.Count;
list.Add(this);
}

int Name { get; private set; }
}[/source]
Exactly what darookie says.

Next to having the index which makes you able to access that specific entry already, the most logical other way would be to give your market class some unique identifier (an integer or string) and access it through that.
In line with Rid and Darookie, why are you creating an List and an array? ListMarkets is already a dynamically-sized array with all the same functionality of the static-sized MarketArray. That is you can give it a ListMarkets[integer] just like you can do a MarketArray[integer].

If by unique names you meant unique variable names, then you might misunderstand reference types. Take a look at this code:
[source lang="csharp"]class MarketPlace{
List<Market> listMarkets = new List<Markets>();
void MakeMarket(){
Market aMarket = new Market();
listMarkets.Add(aMarket);
}
// This is, of course, bad because listMarkets could be empty, but it's just an example.
void ChangeMarket(){
listMarkets[0].value = 200;
}
}[/source]
So, if you call MakeMarkets twice, then ChangeMarket, listMarkets[1]. value is still the default of 0 (or whatever is in the Market constructor). You don't need an array to keep unique variable names. This is because the variable name aMarket is local to the MakeMarket call, so after the call finishes, that variable is deleted. But since the "new" operator creates instances in the heap, the "new Market" is saved by the listMarkets as long as it's not deleted from that list. aMarket and listMarkets keep a "reference" to the Market made in MakeMarkets. But the Market instance itself is stored elsewhere in memory.


[source lang="csharp"]
// 2) Unique name using list index (example only works for single list per class type)
class Market2
{
// here the constructor not only assign the name but also adds the instance to the list
public Market2(IList&lt;Market2&gt; list)
{
Name = list.Count;
list.Add(this);
}
int Name { get; private set; }
}[/source]

Don't use this if you ever plan on deleting Markets. If you make two markets, then delete the first one, the next one added will have the same name as the one remaining. In this case, it's better to use a Dictionary<int, Market>, it should look more like this:

[source lang="csharp"]
// 2) Unique name using list index (example works for multiple lists per class type)
class Market2
{
static int marketCount = 0;
// here the constructor not only assign the name but also adds the instance to the list
public Market2(IDictionary&lt;int, Market2&gt; list)
{
Name = marketCount++;
list.Add(Name, this);
}
int Name { get; private set; }
}[/source]

So, if you call MakeMarkets twice, then ChangeMarket, listMarkets[1]. value is still the default of 0 (or whatever is in the Market constructor). You don't need an array to keep unique variable names. This is because the variable name aMarket is local to the MakeMarket call, so after the call finishes, that variable is deleted. But since the "new" operator creates instances in the heap, the "new Market" is saved by the listMarkets as long as it's not deleted from that list. aMarket and listMarkets keep a "reference" to the Market made in MakeMarkets. But the Market instance itself is stored elsewhere in memory.


This is the part i wasn't understanding, but now that i read this, it is exactly what i was looking for. For all of you, thank you very much for your explanation. I will definitely implement this in my code. It makes life a lot simpler :)

My personal blog on game development!

Black Wolf Game Development

This topic is closed to new replies.

Advertisement