变量声明中所需的常量值
我们在项目中使用自定义 API,它为类字段/成员提供属性,使界面能够显示一些范围值(例如“开/关”)的弹出窗口,并将选择的相应值传递给我们的代码。该属性需要一个字符串数组才能知道该值。
我们为这些范围定义了许多枚举,我们正在考虑使用 Enum.GetValues() 类方法来获取此方法的字符串数组。
但是,正如我们所知,字段声明不允许在声明中使用动态值?那么还有其他有效的方式做同样的事情吗? 为了澄清这个问题,我将写下面的例子;
当前工作
<RangeLookUp("On:1","Off:2")> Public ASimpleRangeVariable As Integer
虽然我想做这样的事情,或者类似
<RangeLookUp(SomeMethod())> Public ASimpleRangeVariable As Integer
Public Shared Function SomeMethod() as String()
'use Enum to get all the items as string values forexample Enum.GetValues & enu,.GetValues
'Return array of string
End Function
Where SomeMethod 假设返回要在 RangeLookup 构造函数中传递的字符串数组。这意味着如果我们更改枚举,那么我们不必更新声明
这个问题可能很奇怪,我知道有更好的方法可以做到这一点,但由于一些自定义 API,基础是有限的。
We are using a custom API in our project which provide an attribute for class fields/members which lets the interface to present a popup of some range values like "On/OFF" and pass the corresponding values of the choice to our code. The attribute requires a string array to know that values.
We have many enumerations defined for these ranges,We are thinking to use Enum.GetValues() kind method to get a string array for this method.
However, As we know the field declaration do not allow dynamic values in the declaration? so is there any other of doing same thing in efficient way.
To clerify the problem i will write the examples below;
Current Working
<RangeLookUp("On:1","Off:2")> Public ASimpleRangeVariable As Integer
While I wanted to do like this or kind of
<RangeLookUp(SomeMethod())> Public ASimpleRangeVariable As Integer
Public Shared Function SomeMethod() as String()
'use Enum to get all the items as string values forexample Enum.GetValues & enu,.GetValues
'Return array of string
End Function
Where SomeMethod suppose to return string array to be passed in the RangeLookup constructor.Which means if we change enumeration then we don't have to update the declaration
This question may be odd and i know there are better ways to do it but due to some custom API, the ground is limited.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如你所说,即使可以,也有更好的方法可以做到。
这里的问题是
SomeMethod()
可以是任何方法,因此没有向程序员提示哪些值是允许的或可用的。更好的解决方案可能是:
或者
As you say, even if you can, there are better ways to do it.
The problem here is that
SomeMethod()
could be any method, so there is no hint to the programmer what values are allowed or available.A better solution could be:
Or