Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Generating Class instance names dynamically

Posted on 2008-06-25
6
Medium Priority
?
255 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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Integration Management Part 2
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

916 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