1

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?

1
  • Is there a particular reason you want to avoid classes / structs other than thinking it is overkill for your situation?
    – Joey
    Sep 30, 2011 at 20:39

8 Answers 8

12
class Foo
{
   public string Name { get; set; }
   public double PreviousValue { get; set; }
   public double NewValue { get; set; }
}
2
  • yes but I just said in my question that it is not really that much big that I want to use a CLASS for it, looking for something simpler. thanks.
    – Bohn
    Sep 30, 2011 at 20:21
  • 10
    @BDotA, if you are thinking that defining a class is too big a deal, I think you are approaching OOP the wrong way.
    – Kirk Woll
    Sep 30, 2011 at 20:24
3

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.

2

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

4
  • 5
    Yeah, and the poor guy that inherits that codebase posts it on the Daily WTF ;) Sep 30, 2011 at 20:39
  • Yeah, it is answering the question as posed though :P.
    – Joey
    Sep 30, 2011 at 20:44
  • If OP considers classes to be overkill, then what are generics (even nested ones)? Congratulations for the rep though :) Sep 30, 2011 at 21:07
  • I think Tuple<string, double, double> is better than Dictionary<string, KeyValuePair<double, double>> Dec 22, 2011 at 8:56
1

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...
}
5
  • yes but I just said in my question that it is not really that much big that I want to use a CLASS for it, looking for something simpler. thanks
    – Bohn
    Sep 30, 2011 at 20:22
  • Do you know the difference between a Structure and a Class? Sep 30, 2011 at 20:24
  • structs are passed by value, classes are reference types.
    – Joey
    Sep 30, 2011 at 20:28
  • 1
    I'm not picking on Grant, I agree with his answer and have voted for it. I was merely answering what I thought was an honest question. I think when I answered it Ramhound's comment read differently, something like, "What's the difference..."
    – Joey
    Sep 30, 2011 at 20:41
  • @GrantWinney - I am asking the author if they know the difference because of his comment about looking for something simpler. Oct 3, 2011 at 10:47
1

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.

1

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.

1

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.

1

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.

1
  • nice...I did not know about Tuples, MSDN says they are for .NET 4.0 and I think on our Citrix boxes we have .NET 3.5..cannot practically use it for my question but very good learning for myself.
    – Bohn
    Sep 30, 2011 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.