0

I need to define enum attributes with variable. This will work fine :

Public Enum CommandsC_e

    <Title("Cmd1")>
    CommandC1

    <Title("Cmd2")>
    CommandC2

End Enum

But this code give me a "constant expression is required." error :

Dim Title as string = "Test"

Public Enum CommandsC_e

    <Title(Title)>
    CommandC1

    <Title("Cmd2")>
    CommandC2

End Enum

I have read somewhere that the enum attributes need to be known at compilation time. Is there a workaround for that ?

TontonVelu
  • 471
  • 2
  • 8
  • 21
  • Those attributes are evaluated at compile time, not at run time. Variables don't have values at compile time and thus cannot be used in that context. What you want to do is not possible. If you declare `Title` as a constant rather than a variable then you will be able to use it, because that value is known at compile time. – jmcilhinney Oct 11 '19 at 07:15
  • that's a bummer, as far as I known, it's impossible to change the attributes in runtime. I guess I'll make an enum for each variable value. Thanks for your help – TontonVelu Oct 11 '19 at 09:31
  • `Private Const enuTitle As String = "Test"` => `Public Enum CommandsC_e (...) End Enum` – Jimi Oct 11 '19 at 15:54

1 Answers1

0

I manage to figure out how to make this work. You need to wrap the variable into a module, there is an exemple with an input box witch populate an enum attribute :

Module Vars

    Property desc As String = InputBox("test", "test", "test")   End Module

End Module

Public Enum CommandsC_e

    <Title(GetType(Vars), NameOf(Vars.desc))>   
    CommandC2

End Enum
TontonVelu
  • 471
  • 2
  • 8
  • 21