Getting the System.Type for String

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nathan Sokalski

    Getting the System.Type for String

    I have the following code:

    Dim values As New ArrayList()
    values.Add("Yes ")
    values.Add("No" )
    values.Add("May be")
    values.Add("Wha tever")
    dim x as String()=values .ToArray()

    However, I recieve an error saying an Object array cannot be implicitly
    converted to a String array. I noticed, however, that there is an overload
    of the ToArray() method that accepts a System.Type to convert the resulting
    array to. However, I am having trouble getting the System.Type for String.
    Can someone help me here? Thanks.
    --
    Nathan Sokalski
    njsokalski@hotm ail.com
    有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。



  • Steve Gerrard

    #2
    Re: Getting the System.Type for String

    Nathan Sokalski wrote:
    I have the following code:
    >
    Dim values As New ArrayList()
    values.Add("Yes ")
    values.Add("No" )
    values.Add("May be")
    values.Add("Wha tever")
    dim x as String()=values .ToArray()
    >
    However, I recieve an error saying an Object array cannot be
    implicitly converted to a String array. I noticed, however, that
    there is an overload of the ToArray() method that accepts a
    System.Type to convert the resulting array to. However, I am having
    trouble getting the System.Type for String. Can someone help me here?
    Thanks.
    I think you need
    GetType(String)

    You will still need a type conversion as well, so you end up with

    Dim x As String() = CType(values.To Array(GetType(S tring)), String())

    I would have just used
    Dim values As New List(Of String)
    ' ....
    Dim x As String() = values.ToArray( )



    Comment

    • Nathan Sokalski

      #3
      Re: Getting the System.Type for String

      Thanks, that worked great! I actually did use your suggestion of List(Of
      String), I guess I just didn't think of that before since my boss gives me
      such boring projects I'm a little out of practice with some of the classes I
      used when I had all the free time I used to. Thanks again.
      --
      Nathan Sokalski
      njsokalski@hotm ail.com
      有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。


      "Steve Gerrard" <mynamehere@com cast.netwrote in message
      news:BrydnXHqio 972i3VnZ2dnUVZ_ ovinZ2d@comcast .com...
      Nathan Sokalski wrote:
      >I have the following code:
      >>
      >Dim values As New ArrayList()
      >values.Add("Ye s")
      >values.Add("No ")
      >values.Add("Ma ybe")
      >values.Add("Wh atever")
      >dim x as String()=values .ToArray()
      >>
      >However, I recieve an error saying an Object array cannot be
      >implicitly converted to a String array. I noticed, however, that
      >there is an overload of the ToArray() method that accepts a
      >System.Type to convert the resulting array to. However, I am having
      >trouble getting the System.Type for String. Can someone help me here?
      >Thanks.
      >
      I think you need
      GetType(String)
      >
      You will still need a type conversion as well, so you end up with
      >
      Dim x As String() = CType(values.To Array(GetType(S tring)), String())
      >
      I would have just used
      Dim values As New List(Of String)
      ' ....
      Dim x As String() = values.ToArray( )
      >
      >
      >

      Comment

      • Phill W.

        #4
        Re: Getting the System.Type for String

        Nathan Sokalski wrote:
        Dim values As New ArrayList()
        values.Add("Yes ")
        values.Add("No" )
        values.Add("May be")
        values.Add("Wha tever")
        dim x as String()=values .ToArray()
        >
        However, I recieve an error saying an Object array cannot be implicitly
        converted to a String array.
        ArrayLists hold Objects (i.e. anything) but an array of Strings can only
        contain Strings, hence your conversion error.

        Are you still using VB'2003?
        If /not/ have a play with Generics:

        Dim values as New List(Of String)
        values.Add("Yes ")
        values.Add("No" )
        values.Add("May be")
        values.Add("Wha tever")
        Dim x as String() = values.ToArray( )

        HTH,
        Phill W.

        Comment

        Working...