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?
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
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.
Re: Identical Split() call works fine in one program but not in another
Quote:
Originally Posted by
techgnome
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).
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.]
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!
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:
Dim testArray() As String = Split("ab\c\d", "")
should be this:
vb.net Code:
Dim testArray As String() = "ab\c\d".Split("")
and this:
vb.net Code:
Dim testString As String = "Look at these!"
'Returns an array containing "Look", "at", and "these!".
Dim testArray() As String = Split(testString)
should be this:
vb.net Code:
Dim testString As String = "Look at these!"
'Returns an array containing "Look", "at", and "these!".
Dim testArray As String() = testString.Split()
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.