Solved

Generating Class instance names dynamically

Posted on 2008-06-25
6
249 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
Comment Utility
What do you mean by dynamically generate?
0
 
LVL 1

Author Comment

by:techsuppoprt
Comment Utility
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
Comment Utility
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 to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Accepted Solution

by:
Babycorn-Starfish earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks guys.
Joe, it's a good solution even though it won't work for me.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now