值范围如何取决于其他变量的值? [英] How can value ranges depend on other variable's values?

查看:77
本文介绍了值范围如何取决于其他变量的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,专家,

我需要实现其数据范围取决于另一个变量的值的数据类型.一个例子是

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;


如果将colorSelection设置为ColorSelection.rgb,则someColor仅应具有红色,绿色和蓝色的值.
如果将colorSelection设置为ColorSelection.cmy,则someColor仅应具有青色,品红色和黄色值.

对于给定的一组值,类型和依赖项,我可以将代码编写到应用程序中.但是我应该发明一些以后可以改变的东西.类似于表示依赖项的ini文件,但此刻我不知道从哪里开始.

有没有类似问题的知名解决方案?

谢谢您的耐心配合,

luker


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

推荐答案

我不确定您是否质疑运行时代码注入. 您能提供一些现实生活中的例子吗?我不确定您需要什么.
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.


也许您可以创建一个类,该类使用其setter为其实例数据成员有效地包装BaseColor枚举,该setter会根据一些内容进行验证类数据成员.类数据成员必须在ini/xml/database配置文件中使用之前/第一次使用时进行初始化.您可能还需要提供一种枚举所有合法值的方法,以便您可以在用户界面中为用户提供适当的选择.您必须确定支持哪些配置以及如何表示它们的详细信息.

我不知道这是否真的是您想要的,这只是考虑的另一种方法.
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.


您可以使用反射并实际创建类型BaseColor及其在运行时提供适当的值.
You can use reflection and actually create the type BaseColor and its appropriate values at runtime.


这篇关于值范围如何取决于其他变量的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆