Solved

Generating Class instance names dynamically

Posted on 2008-06-25
6
252 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Consume a webservice via VB in Visual Studio 2015 3 45
parse string in c# 5 41
vb.net application has warrnings about VB6 calls. 2 48
How does this modal work? 3 34
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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.

739 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