473,406 Members | 2,281 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,406 software developers and data experts.

Implicit conversion from System.Object Array

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

Nov 20 '05 #1
4 8546
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)),Strin g())

-- Alex Papadimoulis

"Petro" <ma*********************@yahoo.com> wrote in message
news:B9**********************************@microsof t.com...
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))
Regards,
Mark

Nov 20 '05 #2
"Petro" <ma*********************@yahoo.com> schrieb
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() _
)
--
Armin

Nov 20 '05 #3
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
Nov 20 '05 #4
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" <ma********************@yahoo.com> wrote in message
news:97**********************************@microsof t.com...
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,
Mark

Nov 20 '05 #5

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

Similar topics

3
by: Siemel Naran | last post by:
Here is a question about implicit conversion from T (&) to ptrcarray<T>. I wrote a class template <class T> struct ptrcarray { T * array; size_t size;
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
9
by: Girish | last post by:
Im trying to understand implicit type conversions from object -> string and vice versa. I have two classes, one Driver and one called StringWrapper. These are just test classes that try and...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
36
by: Chad Z. Hower aka Kudzu | last post by:
I have an implicit conversion set up in an assembly from a Stream to something else. In C#, it works. In VB it does not. Does VB support implicit conversions? And if so any idea why it would work...
1
thav
by: thav | last post by:
I'm taking an intro class right now , I'm not having any fun so far. I keep getting implicit conversion. I tried everything from .toString and declaring the array as a string which gives me problems...
1
by: drop | last post by:
Hi all, I'd like to know if it's possible to declare an implicit type conversion for when I use declarative Synthax in html. For example, when I have the DropDownList, I can declatively set...
8
by: jonpb | last post by:
Hi, Is it possible to define a implicit operator from base to derived types in C#? I have a Point class and would like to derive a Vector class from it and add a couple new vector related...
5
by: johanatan | last post by:
Does anyone know the reasons for the lack of an implicit casting operator in any greater depth than: A. Automatic conversion is believed to be too error prone. (from the FAQ at the bottom of:...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.