开发者

Constant Values Required in Variable Declartion

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

<R开发者_StackOverflowangeLookUp(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.


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()> 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜