Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Generating Class instance names dynamically

Posted on 2008-06-25
6
Medium Priority
?
254 Views
Last Modified: 2013-12-16
Hi Experts,
Need some help here please.

Lets say I have a simple Car() class with the list of properties. Something like this:

Car()
{
         public string Make{ get; set; }
        public string Color{ get; set; }
        public string Engine{ get; set; }
        public string Class{ get; set; }
        public string Height{ get; set; }
        public string Legroom{ get; set; }
}

Also, I have a list of cars (string[] array ) :

Ford
Toyota
Honda
Audi
VW
Ferrari
Lamborghini
...whatever...

How can I dynamically generate appropriate classes for all those cars ? I want all car makes  to have properties of the Car() class.  

Thank you in advance!
0
Comment
Question by:techsuppoprt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 13

Expert Comment

by:joechina
ID: 21868257
What do you mean by dynamically generate?
0
 
LVL 1

Author Comment

by:techsuppoprt
ID: 21868305
I want to somehow dynamically create Classes with car names based on that main Car() class so I could easily have something like this later in the program:

Ford.Color="Blue";

or

Honda.Make="Accord";

Does it make sense ?
0
 
LVL 1

Author Comment

by:techsuppoprt
ID: 21868357
The long version of what I"m trying to achive would be:

string[] carBrand=new string[] 
{
 Ford,
 Toyota,
 Honda,
 Audi,
 VW,
 Ferrari,
 Lamborghini
}
 
Car Ford = new Car();
Car Toyota= new Car();
Car Honda =new Car();
Car VW = new Car();
Car Audi = new Car();
...ETC...
 
//This is ridiculous.. there must be a better way to go about this. I //have hundreds of cars and the list changes all the time. There must be a way to populate the classes dynamically.

Open in new window

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 11

Accepted Solution

by:
Babycorn-Starfish earned 800 total points
ID: 21868524
Hi, you can't really do it as you'd like it.

I suggest you create another field/property in your class for Brand, that way you'd just add the Brand information to the creation of the object.
0
 
LVL 13

Assisted Solution

by:joechina
joechina earned 800 total points
ID: 21870543
You might do it through reflection emit.
But I just wonder why you want to do that?

How about you define a dictionary of car name and car:

Dictionary<string, Car>   Cars = new Dictionary<string,Car>();

You can create a list of cars by
foreach(string carName in carBrand)
{
     Cars.Add(carName,new Car());
}

And you can use it by

Car Ford = Cars["Ford"];
0
 
LVL 1

Author Closing Comment

by:techsuppoprt
ID: 31470682
Thanks guys.
Joe, it's a good solution even though it won't work for me.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question