Generic List to Array

Discussion in 'ASP .Net' started by shapper, May 25, 2007.

  1. shapper

    shapper Guest

    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
     
    shapper, May 25, 2007
    #1
    1. Advertising

  2. shapper

    SAL Guest

    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" <> wrote in message
    news:...
    > 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
    >
     
    SAL, May 25, 2007
    #2
    1. Advertising

  3. 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
     
    =?Utf-8?B?Q2hhcmxlc0E=?=, May 25, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Murat Tasan
    Replies:
    1
    Views:
    8,120
    Chaitanya
    Feb 3, 2009
  2. Replies:
    5
    Views:
    19,073
    Thomas Hawtin
    Jul 18, 2005
  3. Replies:
    4
    Views:
    539
  4. Replies:
    2
    Views:
    473
  5. minlearn
    Replies:
    2
    Views:
    485
    red floyd
    Mar 13, 2009
Loading...

Share This Page