Results 1 to 7 of 7

Thread: [RESOLVED] Array to Obj conversion failure

  1. #1

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    Resolved [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.

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Array to Obj conversion failure

    Here:-
    Code:
    ComboBox1.Items.AddRange(IO.Ports.SerialPort.GetPortNames())
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    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)
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,458

    Re: Array to Obj conversion failure

    Code:
    ComboBox1.Items.AddRange(Array.ConvertAll(IO.Ports.SerialPort.GetPortNames(), Function(pn) CObj(pn)))

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    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.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,458

    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…

  7. #7

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    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
  •  



Click Here to Expand Forum to Full Width