Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
4.33/5 (3 votes)
See more:
Hi Experts,

I need to implement data types whose ranges of values are dependent of another variable's value. An example would be
public enum BaseColor{ invalid, red, yellow, green, cyan, blue, magenta};
public enum ColorSelection{ all, rgb, cmy};

private BaseColor someColor = BaseColor.red;
private ColorSelection colorSelection = ColorSelection.all;

In case colorSelection would be set to ColorSelection.rgb, someColor should only be allowed to have values red, green and blue.
In case colorSelection would be set to ColorSelection.cmy, someColor should only be allowed to have values cyan, magenta and yellow.

For a given set of values, types and dependencies, I could write code into the application. But I am supposed to invent something that is open to later change. Something like an ini-file representing dependencies, but at this moment i don't have a clue where to start.

Is there any well-known solution to a similar problem?

Thank you for your patience,

luker
Posted

I'm not sure if you question is about runtime code injection..
Can you provide some real life example? I'm not exactly sure what you need.
 
Share this answer
 
Perhaps you could create a class that effectively wraps your BaseColor enum with a setter for its instance data member that validates based on the contents of some class data members. The class data members would have to be initialized before/at first use from your ini/xml/database configuration file. You would presumably also need to provide a way to enumerate all legal values so that you can provide the appropriate choices to the user in your user interface. You'd have to work out the details of what configurations you support and how you represent them.

I don't know if this is really what you want, it's just another approach to think about.
 
Share this answer
 
You can use reflection and actually create the type BaseColor and its appropriate values at runtime.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900