Solved

Generating Class instance names dynamically

Posted on 2008-06-25
6
251 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 11

Accepted Solution

by:
Babycorn-Starfish earned 200 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 200 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

Technology Partners: 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!

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled

756 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