473,387 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Getting the System.Type for String

I have the following code:

Dim values As New ArrayList()
values.Add("Yes")
values.Add("No")
values.Add("Maybe")
values.Add("Whatever")
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
nj********@hotmail.com
http://www.nathansokalski.com/
Aug 23 '08 #1
3 1188
Nathan Sokalski wrote:
I have the following code:

Dim values As New ArrayList()
values.Add("Yes")
values.Add("No")
values.Add("Maybe")
values.Add("Whatever")
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.ToArray(GetType(String)), String())

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

Aug 23 '08 #2
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
nj********@hotmail.com
http://www.nathansokalski.com/

"Steve Gerrard" <my********@comcast.netwrote in message
news:Br******************************@comcast.com. ..
Nathan Sokalski wrote:
>I have the following code:

Dim values As New ArrayList()
values.Add("Yes")
values.Add("No")
values.Add("Maybe")
values.Add("Whatever")
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.ToArray(GetType(String)), String())

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

Aug 23 '08 #3
Nathan Sokalski wrote:
Dim values As New ArrayList()
values.Add("Yes")
values.Add("No")
values.Add("Maybe")
values.Add("Whatever")
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("Maybe")
values.Add("Whatever")
Dim x as String() = values.ToArray()

HTH,
Phill W.
Aug 26 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Asaf Ganot | last post by:
Hi, I'm looking for a way to retrieve the date type of a registry value using c#. Since I couldn't find a way to do it with 'Microsoft.Win32', I tried to use the good old API call 'RegEnumValue'...
3
by: Hitesh | last post by:
Hi, I am getting the response from another Website by using the HttpHandler in my current site. I am getting the page but all the images on that page are not appearing only placeholder are...
5
by: Erol | last post by:
How do I get a type from a string? I'm retrieving a string value from my database so that I can set my property values dynamically. In the event "Form1_Load", you will see that I'm trying to set...
2
by: Danny Gagne | last post by:
I'm currently working an .net application (I can use 1.1 or 2.0 if needed) that needs to read a wsdl file and generate another piece of code that can use it. I'm encountering a problem where I...
4
by: AshishMishra16 | last post by:
HI friends, I am using the Flex to upload files to server. I m getting all the details about the file, but I m not able to upload it to Server. Here is the code i m using for both flex & for...
0
by: buntyindia | last post by:
Hi, I have a very strange problem with my application. I have developed it using Struts. I have a TextBox With Some fixed value in it and on Submit iam passing it to another page. <html:form...
4
by: jehugaleahsa | last post by:
Hello: Say I were to query the schema tables from a database management system. Say I got a DataType field that held the string 'VARCHAR2'. Now, I know that VARCHAR2 associates to System.String....
0
by: TG | last post by:
Hi! Once again I have hit a brick wall here. I have a combobox in which the user types the server name and then clicks on button 'CONNECT' to populate the next combobox which contains all the...
3
by: Nathan Sokalski | last post by:
I have the following code: Dim values As New ArrayList() values.Add("Yes") values.Add("No") values.Add("Maybe") values.Add("Whatever") dim x as String()=values.ToArray() However, I recieve...
1
by: raghuvendra | last post by:
Hi I have a jsp page with 4 columns: namely Category name , Category order, Input field and a submit button. All these are aligned in a row. And Each Category Name has its corresponding Category...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.