Generic List to Array

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I have a generic list as follows:
Dim categories As Generic.List(Of Enumeration.Category)

Category is an enumeration as follows:

Public Enum Category
Book
Document
Paper
End Enum

How can I convert categories to a string array which would become
something as follows:
Book,Document,Paper, ...

I tried:
categories.ToArray

And I get the error:
Value of type '1-dimensional array of Category' cannot be converted to
'1-dimensional array of String'

Thanks,
Miguel
 
What you could do is inherit from Generic.List (type safe) and then add a
ConvertToStringArray method and do the work of converting it to an array of
string arrays..

Just a thought

S
 
Shapper

this is much easier
Class enumsExample

Public Shared Sub Main()
For Each value As String In [Enum].GetNames(GetType(Category))
'just show the value in the console window for now
Console.WriteLine(value)
Next value
End Sub
End Class

and if you want to place it in an array you create the array before hand and
stuff each 'value' into the right cubby-hole in the array, or even adding it
to a ArrayList and use ToArray on that...

There is a also a GetValues for getting at the underlying Integer values
Shared method of the [Enum] class (has to be in square brackets in VB as it's
a reserved word as well as a class name)
I hope this helps
Cheers
CharlesA
 
Back
Top