Reputation: 26919
C# WinForms: The things I want to save:
name: string
previous value: double
new value: double
so each entity has those three fields, the number of them will not be too much at all, maybe 10 max. All I want to do with them is just to be able to add some members to it and then loop through them and read them later.
so one easy way would be having three arrays, one for each. It is not really thay big of a deal to create a Class for it, etc..so I am looking for some good strudcutres that I can use, Arrays like what I said above? any better choice?
Upvotes: 1
Views: 183
Reputation: 6867
If you don't want to create a class or struct then you could store your data in a list of tuples.
List<Tuple<string, double, double>> MyData = new List<Tuple<string, double, double>>();
MyData.Add(new Tuple<string, double, double>("test1", 1, 2));
MyData.Add(new Tuple<string, double, double>("test2", 3, 4));
If it were me though I would just go ahead and create a class, it will make it easier for future developers to figure out your code.
Upvotes: 1
Reputation: 51634
What do you mean with "not really that big of a deal to create a class"?
Classes are not evil. They help structure your data. That's one of the main reasons why they have been invented. Make it a class. It won't kill your code's performance, but will definitely increase readability, maintainability and whatnot compared to using primitives and collections thereof.
If not for yourself, then for the poor chap that will eventually inherit your codebase.
Use a class.
I can point you to some good reading material on object orientation if needed.
Upvotes: 1
Reputation: 611
I think that a class really is the most simple way to store this data. I know that you keep saying it's not a big enough deal to use a class, but if you need to save this data set easily for use later you really need to be using a class. You even said yourself that:
each entity has those three fields
The "entity" you're talking about is an object, which is an instance of a class. You can have a List<T>
of the objects and iterate through them easily with a foreach
.
Upvotes: 1
Reputation: 1790
Class or struct is clearly the way to go. If you want to avoid a class you could use a Dictionary<string, KeyValuePair<double, double>>
where the key is the name and the KeyValuePair represents the previous and new values
Upvotes: 2
Reputation: 21855
If you're into OOP (and you should) then everything is a class. What's the problem with the classes? They're not selected based on how big they are. If they model something real then they should exist.
I won't create anything different that what others have said.
Upvotes: 1
Reputation: 40811
There's nothing wrong with creating classes to hold custom data.
You can even create a private inner class if its not going to be used by anything else.
There is something wrong with using arrays for a need that doesn't require an indexed set of data.
Upvotes: 3
Reputation: 66439
Create an object for them:
public class Foo
{
public string Name { get; set; }
public double Value { get; set; }
public double OldValue { get; set; }
}
Then you can add them to a List and loop through them and access them:
foreach(var foo in myListOfFoos)
{
// Do something with foo.Name, foo.Value, etc...
}
Upvotes: 1
Reputation: 46008
class Foo
{
public string Name { get; set; }
public double PreviousValue { get; set; }
public double NewValue { get; set; }
}
Upvotes: 12