【问题标题】:Constant Values Required in Variable Declartion变量声明中需要的常量值
【发布时间】:2009-11-11 16:53:16
【问题描述】:

我们在我们的项目中使用了一个自定义 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 

SomeMethod 假设返回要在 RangeLookup 构造函数中传递的字符串数组。这意味着如果我们更改枚举,那么我们不必更新声明

这个问题可能很奇怪,我知道有更好的方法可以解决,但由于一些自定义 API,基础有限。

【问题讨论】:

    标签: .net vb.net enums attributes


    【解决方案1】:

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

    这里的问题是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()> 
    

    【讨论】:

      猜你喜欢
      • 2015-03-25
      • 1970-01-01
      • 2011-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多