变量声明中所需的常量值

发布于 2024-08-10 10:01:11 字数 808 浏览 2 评论 0原文

我们在项目中使用自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧情勿念 2024-08-17 10:01:11

正如你所说,即使可以,也有更好的方法可以做到。

这里的问题是 SomeMethod() 可以是任何方法,因此没有向程序员提示哪些值是允许的或可用的。

更好的解决方案可能是:

'Using the same attribute, but setting a enum of allowed enums
<RangeLookUp(Ranges.OnOff)> 

或者

'Using different attribute names, and let the attribute inherits from other
<RangeLookUp_OnOff()> 

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:

'Using the same attribute, but setting a enum of allowed enums
<RangeLookUp(Ranges.OnOff)> 

Or

'Using different attribute names, and let the attribute inherits from other
<RangeLookUp_OnOff()> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文