CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2008
    Posts
    154

    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.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Feb 2004
    Location
    Seattle, USA
    Posts
    137

    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
  •  





Click Here to Expand Forum to Full Width

Featured