Reputation: 11
im really new to C# so please excuse my sloppy code
public Kid(string name, int age, string location)
{
this.age = age;
this.name = name;
this.location = location;
}
NAL = Console.ReadLine().Split(',');
Console.WriteLine("Name-{0} Age-{1} Location-{2}", NAL);
string name = "Kid" + NAL[0];
Kid [name] = new Kid(NAL[0], Int32.Parse(NAL[1]), NAL[2]);
i need ^ this to work but i dont understand any of the types can some one help explain this to me
Upvotes: 0
Views: 124
Reputation: 1800
You can't dynamically name objects like you want to in C#. You can however implement something that meets your needs using a Dictionary.
var kids = new Dictionary<string, Kid>()
kids.Add(name, new Kid(NAL[0], int.Parse(NAL[1]), NAL[2]);
You can then access the Kid
named Sam by doing
kids["KidSam"]
This gives you access to what are essentially named Kid
objects without the need to name every object.
Upvotes: 1
Reputation: 38333
All you need to change is the "Kid" collection.
Dictionary<string, Kid> Kids = new Dictionary<string, Kid>();
Kids[name] = new Kid(NAL[0], Int32.Parse(NAL[1]), NAL[2]);
Using the Dictionary you can retrieve by name, adding an entry with the same name will overwrite the previous entry.
Upvotes: 2