Search code examples
vb.netoption-strict

Option strict disallows late binding for Array


enter image description here
Could you please help how to solve this above issue.


Solution

  • You should pretty much NEVER declare something as type Array. If you are creating a String array then declare that:

    Dim itemNames As String() = System.Enum.GetNames(GetType(Configuration.eSystemType))
    

    Of course, given that Enum.GetNames has a return type of String(), you could just use type inference:

    Dim itemNames = Enum.GetNames(GetType(Configuration.eSystemType))
    

    I've also dropped the superfluous System namespace qualifier there.

    Perhaps you were fooled by the fact that Enum.GetValues is declared as type Array. I did say "pretty much never". There are rare cases where it's required but you, as an application developer, will almost certainly never have to do it. The array returned is of the type you specified so you should cast as that type:

    Dim itemValues = DirectCast(Enum.GetValues(GetType(Configuration.eSystemType)),
                                Configuration.eSystemType())
    

    The ListItem constructor still requires two String arguments though, so you still need to convert the Configuration.eSystemType values to Strings:

    Dim item As New ListItem(itemNames(i), itemValues(i).ToString())