Reputation:
Below is the code I have but the method is not available on Enum arrays. I can't work out what I'm doing wrong. Note that I'm unable to test the line Array.ConvertAll
until I have this method available on Enum arrays.
Public Module EnumExtensions
<Extension()>
Function ValuesToString(Source As [Enum]()) As String()
Dim EnumType = Source.GetType()
If Not EnumType.IsEnum Then Return Nothing
Return Array.ConvertAll(Source, Function(x) x.ToString)
End Function
End Module
Upvotes: 0
Views: 1196
Reputation: 54457
As I said in my comment, you could just call Select
and ToArray
as needed. If you really want an extension though, you would need to make your method generic:
<Extension>
Public Function ToStrings(Of T)(source As T()) As String()
'If Not GetType(T).IsEnum Then
' Return Nothing
'End If
Return Array.ConvertAll(source, Function(e) e.ToString())
End Function
There's no generic constraint that can limit that method to be called on just an array of Enum
values so you can use an If
statement to either return Nothing
or throw an exception. I don't really see the point though, as it really doesn't hurt if you allow the same method to be called on another array of any other type anyway.
Upvotes: 2