-
August 20th, 2010, 11:17 PM
#1
Need help with stacked Attributes
I want to create a class that can be further defined by a series of options.
So lets say we have a class called Car
Car myCar = new Car();
I then want to define the car further like this.
Car myCar = new Car(Company.Honda, Model.CR-Z);
I tried this with an Enum but I could only get as far as the Company.
I want the list of selections from the Model to be dependant on Company
that way if I pick Company.BMW - I get a different list to choose from in Model
does that make sense???
Last edited by bixel; August 20th, 2010 at 11:21 PM.
-
August 21st, 2010, 08:50 AM
#2
Re: Need help with stacked Attributes
I'm not that into C# but what you want is impossible. When you type the car parameter the intellisense has forgot all about the company parameter. Also, even if the intellisense did this correlation the type of the second parameter should be of some kind of union type to be capable of accepting several different enumerations.
It might be that you could do some other magic type using System.Runtime.InteropServices and create a union that allows you to get what you want using a single parameter but that's above my C# knowledge.
Last edited by S_M_A; August 21st, 2010 at 08:56 AM.
-
August 23rd, 2010, 12:29 AM
#3
Re: Need help with stacked Attributes
My first thought is if the 2nd parameter is directly related to the 1st, then why is the first parameter of the ctor even needed?
You might want to consider having the model depend on the Company (you don't need to pass the company if the model knows what Company it is a part of).
Code:
public class Company
{
public ICollection<Model> Models { get; }
}
public class Model
{
public Company Company { get; set; }
}
public class Car
{
public Car(Model model)
{
// get company from model.Company. No need to pass it as an arg
}
}
void Main()
{
Company honda = new Company();
Model accord = new Model() { Company = honda };
Car car = new Car(accord);
}
I didn't add the code for it, but you can use the Company.Models collection to list out all possible models for a particular company.
Hope this helps.
Last edited by enfekted; August 23rd, 2010 at 12:34 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|