How do I create a class item with properties?

M

mb

Here is an example of what I want to create:

A class called Cars

public class Cars
{
public string Mazda;
public string Ford;
etc, etc..........
}

then I want to create properties for each car so if I instantiate Cars:

Cars myCar = new Cars();

then I can access this:

myCar.Mazda.Wheels = Color.Black

I hope this makes sense.

Thanks

PS. I want to randomly pick a car and have its Wheel property checked.
 
I

Imran Koradia

What you're talking about is class inheritance.
something like:

public class Cars
{
public System.Drawing.Color Wheels;
}

public class Mazda : Cars
{

}

public class Ford : Cars
{

}

Imran.
 
M

mb

Thanks,

I see how that would work, yet I would have to individually instanciate
every car if I awas to use them all.
I want to just instaniate a Car, pick the car I want from the list of
properties, then pick a property of a property. Is this not possible?
 
C

Cor Ligthert

MB,
I see how that would work, yet I would have to individually instanciate
every car if I awas to use them all.
I want to just instaniate a Car, pick the car I want from the list of
properties, then pick a property of a property. Is this not possible?

In a general way answered, the answer is "No, this is possible" but than you
should create a property "brand" and pass that in your New constructor.

Did you know that for language questions about C# is the very active
newsgroup.
Microsoft.public.dotnet.languages.csharp

I hope this helps?

Cor
 
G

Guest

MB,

I believe the way is:
1. Create a car collection or hashtable and use car name as index.
2. Add the cars you need.
3. Use the syntax like
mycars["Mazda"] = Color.Black or
mycars[CarType.Mazda] = Color.Black

Yang Lu.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top