-
Feb 5th, 2023, 05:14 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Array to Obj conversion failure
Hello all.
Consider you want to import available serialports into a combobox while option strict is on. following error occurs during building:
Error BC30512 Option Strict On disallows implicit conversions from 'Object' to 'Object()'.
Code:
Dim MyPort As Array
MyPort = IO.Ports.SerialPort.GetPortNames()
ComboBox1.Items.AddRange(CObj(MyPort))
I'm still lagging in data conversions.
-
Feb 5th, 2023, 05:23 AM
#2
Re: Array to Obj conversion failure
Here:-
Code:
ComboBox1.Items.AddRange(IO.Ports.SerialPort.GetPortNames())
-
Feb 5th, 2023, 05:26 AM
#3
Re: Array to Obj conversion failure
Or if you're not comfortable about the implicit conversion from a String array to an Object array, you can do the conversion yourself explicitly:-
Code:
Dim ports As Object() = (From p In IO.Ports.SerialPort.GetPortNames() Select CObj(p)).ToArray
ComboBox1.Items.AddRange(ports)
-
Feb 5th, 2023, 05:48 AM
#4
Re: Array to Obj conversion failure
Code:
ComboBox1.Items.AddRange(Array.ConvertAll(IO.Ports.SerialPort.GetPortNames(), Function(pn) CObj(pn)))
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 5th, 2023, 06:07 AM
#5
Re: Array to Obj conversion failure
You should pretty much NEVER be using the Array class other than to call Shared methods. That SerialPort.GetPortNames method returns a String(), i.e an String array, so that's the type you should be using. There's absolutely no need or point to any conversion because you can provide any reference type array where an Object array is expected. If you have a value type array, e.g. an Integer array, then you will need to convert. That's because value types require boxing to get an Object reference and that won't be done implicitly.
In short, just do what Niya demonstrated in post #2 and never use an Array variable again.
-
Feb 5th, 2023, 06:17 AM
#6
Re: Array to Obj conversion failure
@pourkascheff… you were attempting to convert the whole array to a single object, when you needed an array of objects. But as Niya and jmcilhinney have told you, an array of Strings is perfectly acceptable…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 5th, 2023, 06:26 AM
#7
Thread Starter
Hyperactive Member
Re: Array to Obj conversion failure
wow, learned lots of thing today morning. Thanks to you Niya, jmcilhinney and .paul.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|