Click here to Skip to main content
15,904,638 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I'm looking for a method to store about 5 strrings of information under one name in the simplest method possible.

I have a list of 20-50 names, each one with a bunch of information. For example:

Name: John
CarType: Honda
CarColor: Green
Age: 40

Name: Roger
CarType: Mazda
CarColor: White
Age: 70

Now all I want is a simple lookup type code, so I can do something like this:

MessageBox.Show(Roger.CarType);

I could create a method called Names and use:

Names Roger = new Names();<br />
string car = Roger.CarType;


The only problem with this is there will need to be a set amount of objects and I don't have the names to be saved yet.



Thanks
Posted

C#
public class _you_name_it_
{
    public string Name;
    public string CarType;
    public string CarColor;
    public int Age;  //make it string if you like
    public string Another_Attribute;
    public override string ToString()
    {
        return string.Format ("Name: {0} CarType: {1} CarColour: {3} Age: {4}",
            Name, CarType, CarColor, Age ); // Add another attribut if you like
    }
}

Use it as
C#
System.Collections.Generic.Dictionary<string,_you_name_it_> myList= new 
    System.Collections.Generic.Dictionary<string,_you_name_it_>();
_you_name_it_ dummy = new _you_name_it_();
dummy.Name = "John";
dummy.CarType = "Honda";
dummy.CarColor = "Green";
dummy.Age = 40;

myList[dummy.Name]=dummy;

MessageBox.Show(myList["John"].ToString());
 
Share this answer
 
Comments
john123451 8-May-11 15:43pm    
Reading over your code this seems to be a good solution to my problem. I was previously using a similar method to break up a string and rejoin it but I was using a string.split method instead of using string.format and Dictionary.

Thanks a lot for posting.
Sergey Alexandrovich Kryukov 8-May-11 15:52pm    
Correct valid advice, my 5.
OP's reply shows a big lack of understanding though. I took a look at his previous questions -- same thing. Just look at "similar (!) method to break up a string... using string.split...".

I even more strongly recommend stoppig CodeProject questions and learning basics.
--SA
john123451 8-May-11 18:31pm    
I wrote method by accident. Just because I have problems asking questions on this forum and explaining what I need done doesn't mean I can't code. How about you stop posting complaints that aren't constructive in anyway and either post an answer or don't post at all. Seriously... you've been on this forum for 5 years.
furyous 8-May-11 19:43pm    
No offense John but you are asking for help with creating classes and storing those classes in a list this is very SIMPLE. You should learn the language before asking a question like this.
john123451 8-May-11 19:52pm    
So you are saying when I get stuck I can't ask a question until I know the language more... I see. At what point does someone know a language sufficient enough to ask questions?

PS don't use "no offense" before anything. Saying it doesn't remove the offensive content from your statement.
This semantic looks like a class or structure. Is also looks like you have a little to know idea what is that — just yet. You're also confused about what is the method. You Names is not.

Nobody can help you until you read and understand the very basics of .NET and C#. No, just the very basics of programming, especially OOP. Until you do it, your questions here at CodeProject would not help you. The is such thing as prerequisites. Please make sure you have them first. After you do we will be more than happy to help.

Thank you for understanding.

—SA
 
Share this answer
 
Comments
yesotaso 8-May-11 13:56pm    
Hmm, thats a fair point indeed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900