Generic List to Array

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
 
S

SAL

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
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,828
Latest member
LauraCastr

Latest Threads

Top