Sign in to follow this  
Jaap85

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

Recommended Posts

Jaap85    263
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:

[code]
List<Market> listMarkets = new List<Market>();
[/code]

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:

[code]
Market[] MarketArray = new Market[100];
[/code]

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.

Share this post


Link to post
Share on other sites
darookie    1441
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] Edited by darookie

Share this post


Link to post
Share on other sites
reloadead    1795
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.

Share this post


Link to post
Share on other sites
ForteBass    106
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.

[quote name='darookie' timestamp='1344786721' post='4968736']
[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]
[/quote]
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]

Share this post


Link to post
Share on other sites
Jaap85    263
[quote name='ForteBass' timestamp='1344805370' post='4968826']
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.
[/quote]

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 :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this