[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.
Re: Array to Obj conversion failure
Here:-
Code:
ComboBox1.Items.AddRange(IO.Ports.SerialPort.GetPortNames())
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)
Re: Array to Obj conversion failure
Code:
ComboBox1.Items.AddRange(Array.ConvertAll(IO.Ports.SerialPort.GetPortNames(), Function(pn) CObj(pn)))
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.
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…
Re: Array to Obj conversion failure
wow, learned lots of thing today morning. Thanks to you Niya, jmcilhinney and .paul.