Results 1 to 8 of 8

Thread: Identical Split() call works fine in one program but not in another

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2012
    Posts
    75

    Identical Split() call works fine in one program but not in another

    I have a test line in a VB program:
    Code:
    Dim testArray() As String = Split("ab\c\d", "\")
    which I copied more or less straight from a Visual Basic reference.

    It works fine in one program, but in another I get the error message:

    Error BC30519 Overload resolution failed because no accessible 'Split' can be called without a narrowing conversion:
    'Public Overloads Function Split(ParamArray separator As Char()) As String()': Argument matching parameter 'separator' narrows from 'String' to 'Char'.
    'Public Overloads Function Split(ParamArray separator As Char()) As String()': Argument matching parameter 'separator' narrows from 'String' to 'Char'.
    'Public Overloads Function Split(separator As Char(), count As Integer) As String()': Argument matching parameter 'separator' narrows from 'String' to 'Char()'.
    'Public Overloads Function Split(separator As Char(), count As Integer) As String()': Argument matching parameter 'count' narrows from 'String' to 'Integer'.
    'Public Overloads Function Split(separator As Char(), options As StringSplitOptions) As String()': Argument matching parameter 'separator' narrows from 'String' to 'Char()'.
    'Public Overloads Function Split(separator As Char(), options As StringSplitOptions) As String()': Argument matching parameter 'options' narrows from 'String' to 'StringSplitOptions'.

    I've tried any number of different ways of implementing the Split, but nothing works.

    I also tried:
    Code:
    Dim testString As String = "Look at these!"
    'Returns an array containing "Look", "at", and "these!".
    Dim testArray() As String = Split(testString)
    again copied directly from a Visual Basic reference. It gives the error message:

    Error BC30469 Reference to a non-shared member requires an object reference.

    Again, the same statements work fine when run in another program.

    FWIW, I have:

    Imports System.Threading
    Imports System.IO
    Imports System.String

    Is there a reference missing, or something corrupt?

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Identical Split() call works fine in one program but not in another

    Split requires AT LEAST two parameters... the string to split, and what to split on.... you gave it the first, and nothing else. That's what the error message is telling you.... it's looking at all the versions of split overloads, and none match the one you tried to use.
    Split can be used one of two ways... either as a String.Split method, which requires two parameters at least, or as stringVariable.Split which only requires one at least ... the character to split on.... again, you haven't used Split in the appropriate way.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    Re: Identical Split() call works fine in one program but not in another

    If you want to call the VB Microsoft.VisualBasic.Strings.Split method, then get rid of the "Imports System.String".
    You are confusing the compiler - with that Imports, VB thinks you are calling the 'Split' method of the String class, but you seem to be calling the 'Split' method of the Microsoft.VisualBasic.Strings module.

    Also, "Imports System.String" is pretty useless - for the handful of static string methods, you are just making it more confusing by omitting "String" in the call.
    Last edited by David Anton; Oct 3rd, 2021 at 07:17 PM.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  4. #4
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    Re: Identical Split() call works fine in one program but not in another

    Quote Originally Posted by techgnome View Post
    Split requires AT LEAST two parameters... the string to split, and what to split on....
    They are trying to call the Microsoft.VisualBasic.Strings method, which only requires one parameter (the delimiter is assumed to be a space if omitted).
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2012
    Posts
    75

    Re: Identical Split() call works fine in one program but not in another

    Thanks for the reply, but I'm afraid I don't understand. The first version does have two parameters: "ab-c-d" and "-". The second, as I said, was copied directly from an example shown on a Microsoft Visual Basic help page. Furthermore, both versions do work in a different program that I put them in as a test.

    [In this reply, I've changed the backslash to a dash, since otherwise the html translator here changes the "{backslash}" to "". The backslash is retained only when I put it between code tags.]

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2012
    Posts
    75

    Re: Identical Split() call works fine in one program but not in another

    Yes! Removing "Imports System.String" fixed the problem. I'm actually not sure why I had it there in the first place. Thanks!

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Identical Split() call works fine in one program but not in another

    Don't use VB Runtime functions in the first place. They are reimplemented holdovers from VB6, often with convoluted implementations in order to ensure the same quirky behaviour as in VB6 to allow existing VB6 code to be upgraded without changing behaviour. If you're writing new VB.NET code then use .NET from the outset. If you want to split a String then call the Split method of that String object, not the Microsoft.VisualBasic.Strings.Split method. If you can call a method without qualifying the name with a type or reference/value then there's a good chance that you're calling a VB Runtime function, so don't. This:
    vb.net Code:
    1. Dim testArray() As String = Split("ab\c\d", "")
    should be this:
    vb.net Code:
    1. Dim testArray As String() = "ab\c\d".Split("")
    and this:
    vb.net Code:
    1. Dim testString As String = "Look at these!"
    2. 'Returns an array containing "Look", "at", and "these!".
    3. Dim testArray() As String = Split(testString)
    should be this:
    vb.net Code:
    1. Dim testString As String = "Look at these!"
    2. 'Returns an array containing "Look", "at", and "these!".
    3. Dim testArray As String() = testString.Split()
    Last edited by jmcilhinney; Oct 4th, 2021 at 09:15 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Feb 2012
    Posts
    75

    Re: Identical Split() call works fine in one program but not in another

    Thanks for the information. I wasn't sure what the difference was in the two calls (not only for Split but for other functions as well). I'll follow your advice.

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