Implicit conversion from System.Object Array

G

Guest

Good morning

The following code generates the exception: "Option Strict On disallows implicit conversion from System.Array to 1-dimensional array of 'String'.

I've been unable to find the conversion command that works. If any of you fine folks could point me in the right direction it would be appreciated

------------------------------------

Dim rdr As SqlDataReader = cmd.ExecuteReade
Dim roleList As New ArrayLis
Do While rdr.Rea
roleList.Add(rdr("group_name")
Loo

'Convert the roleList ArrayList to a String arra
' EXCEPTION GENERATED HER
Dim roleListArray As String() = roleList.ToArray(GetType(String)

Regards
Mar
 
A

Alex Papadimoulis

Mark,

Although ToArray returns a string array, it is boxed in a generic array. You
need to cast it to a string array. Try this:
Dim roleListArray As String() =
DirectCast(roleList.ToArray(GetType(String)),String())

-- Alex Papadimoulis

Petro said:
Good morning,

The following code generates the exception: "Option Strict On disallows
implicit conversion from System.Array to 1-dimensional array of 'String'."
I've been unable to find the conversion command that works. If any of you
fine folks could point me in the right direction it would be appreciated.
 
A

Armin Zingler

Petro said:
Good morning,

The following code generates the exception: "Option Strict On
disallows implicit conversion from System.Array to 1-dimensional
array of 'String'."

I've been unable to find the conversion command that works. If any
of you fine folks could point me in the right direction it would be
appreciated.

-------------------------------------

Dim rdr As SqlDataReader = cmd.ExecuteReader
Dim roleList As New ArrayList
Do While rdr.Read
roleList.Add(rdr("group_name"))
Loop

'Convert the roleList ArrayList to a String array
' EXCEPTION GENERATED HERE
Dim roleListArray As String() =
roleList.ToArray(GetType(String))


Dim roleListArray As String()
roleListArray = directcast( _
roleList.ToArray(GetType(String)), _
string() _
)
 
G

Guest

Alex

Thank you. I tried converting using many different ways but not the way that you suggested. ;-

You used DirectCast, as do many other members here, when type conversion is required. With reference to the VB Language Reference for this command, <A href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vakeydirectcast.asp" /A>, CType also works. It may be because I'm relatively new to programming that I don't quite grasp the subtilties of the difference between the two, so why choose DirectCast when it seems that CType will succeed where DirectCast will not? Is run-time performance the only advantage

Regards
Mar
 
A

Alex Papadimoulis

Petro,

The main difference is that CType will try to convert where as DirectCast
will not. If the returned object is what you want it to be (such as in the
Array vs String() case), then use DirectCast. I suspect you'll probably
rarely use CType, as the builtin conversions (such as CInt,CStr, etc) work
the same and are easier to use. Another example:

DirectCast( 4.2, Integer) -- fail, because 4.2 is a floating point
CType(4.2, Integer) -- success, returns 4

-- Alex Papadimoulis


Petro said:
Alex,

Thank you. I tried converting using many different ways but not the way that you suggested. ;-)

You used DirectCast, as do many other members here, when type conversion
is required. With reference to the VB Language Reference for this command,
<A
href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7
/html/vakeydirectcast.asp" /A>, CType also works. It may be because I'm
relatively new to programming that I don't quite grasp the subtilties of the
difference between the two, so why choose DirectCast when it seems that
CType will succeed where DirectCast will not? Is run-time performance the
only advantage?
 

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

Top