Uncomment and U get a Ubound of -1. So what does SPLIT do to the array?VB Code:
Private Sub Command1_Click() Dim strT As String, strX() As String 'strX = Split(strT) MsgBox UBound(strX) End Sub
Uncomment and U get a Ubound of -1. So what does SPLIT do to the array?VB Code:
Private Sub Command1_Click() Dim strT As String, strX() As String 'strX = Split(strT) MsgBox UBound(strX) End Sub
Split returns a String array. Its documented somewhere in the help that when Split and Filter return empty arrays, the LBound is 0, while the UBound is -1. They do that as a means of error checking without raising an actual error.
Try putting something into the string then read the ubound
String array. Plain and simple.
VB Code:
Private Sub Form_Load() Dim x As String: x = "a,b,c" '' returns String() '' MsgBox TypeName(Split(x, ",")) '' returns 'a' '' MsgBox Split(x, ",")(0) End Sub
... never heard of Filter(). Do you have a sample by any chance ?Quote:
Originally posted by crptcblade
Split returns a String array. Its documented somewhere in the help that when Split and Filter return empty arrays, the LBound is 0, while the UBound is -1. They do that as a means of error checking without raising an actual error.
Actually found a vbs sample
VB Code:
dim a(5),b a(0)="Saturday" a(1)="Sunday" a(2)="Monday" a(3)="Tuesday" a(4)="Wednesday" b=Filter(a,"n") document.write(b(0) & "<br />") document.write(b(1) & "<br />") document.write(b(2)) Output:Sunday Monday Wednesday
Looks handy!
The reason I asked was when an empty String Array's Ubound is sought, a list index error occurs; but when an empty string is split, the result is the same empty string array, which does not errror out. Or is it the same?
:)
String and Filter return an initialized array, just initialized as empty. It sounds the same, but its quite different than an uninitialized array.
I would have to argue with you on this.Quote:
Originally posted by plenderj
String array. Plain and simple.
VB Code:
Private Sub Form_Load() Dim x As String: x = "a,b,c" '' returns String() '' MsgBox TypeName(Split(x, ",")) '' returns 'a' '' MsgBox Split(x, ",")(0) End Sub
If Split() were to return a variant, your first MessageBox would give you "String" anyways. Try this code:
VB Code:
Dim vVar As Variant vVar = 5 MsgBox TypeName(vVar) vVar = "cool" MsgBox TypeName(vVar)
So just because TypeName() gives you string after a Split() function doesn't prove that it's a String array. If it's Variant, it will tell you whatever the contents of the variable are.
And, if you look in the Object Explorer, for Split(), you see:
Other functions, such as Left read the same way, except Left$ reads:Quote:
Function Split(Expression As String, [Delimiter], [Limit As Long = -1], [Compare As VbCompareMethod = vbBinaryCompare])
So I have to argue with you and say that Split() returns a variant.Quote:
Function Left$(String As String, Length As Long) [b]As String[b]
Maybe the following piece of code using the Split function could help:
Private Sub Command1_Click()
Dim str As String
Dim vElements As Variant
Dim vElement As Variant
str = "a,b,c,d,e,f"
vElements = Split(str, ",")
For Each vElement In vElements
Debug.Print vElement
Next
End Sub
I'm still going with strings as the element types in split. From MSDN:
andQuote:
Returns a zero-based, one-dimensional array containing a specified number of substrings.
Quote:
delimiter - Optional. String character used to identify substring limits. If omitted, the space character (" ") is assumed to be the delimiter. If delimiter is a zero-length string, a single-element array containing the entire expression string is returned.
I am pretty sure it returns a string. try these to see what I mean:
VB Code:
Private Sub Command1_Click() 'THIS RUNS Dim arr() As String arr() = Split("a-1-e-234-1.11", "-") End Sub
VB Code:
Private Sub Command1_Click() 'THIS CRASHES Dim arr() As Variant arr() = Split("a-1-e-234-1.11", "-") End Sub
No where does that state that an array of strings is returned. Just that the elements are strings. And variants can hold any data type, including strings.Quote:
Originally posted by VBGuy
I'm still going with strings as the element types in split. From MSDN:
and
Quote:
Originally posted by Muddy
I am pretty sure it returns a string. try these to see what I mean:
VB Code:
Private Sub Command1_Click() 'THIS RUNS Dim arr() As String arr() = Split("a-1-e-234-1.11", "-") End Sub
VB Code:
Private Sub Command1_Click() 'THIS CRASHES Dim arr() As Variant arr() = Split("a-1-e-234-1.11", "-") End Sub
VB Code:
Private Sub Command1_Click() Dim arr As Variant arr = Split("a-1-e-234-1.11", "-") End Sub
Well since Split works in VB.net and VB.net does not have Variants i would assume it returns a string.
They key is in how the array is decalred.... in the orignal post, it was dim as an array of Strings... that's why split reurns a type of string.... I've also used it to split numbers into an array of variants (an array of longs didn't work... not sur why though)Quote:
Originally posted by The Hobo
No where does that state that an array of strings is returned. Just that the elements are strings. And variants can hold any data type, including strings.
VB Code:
Private Sub Command1_Click() Dim arr As Variant arr = Split("a-1-e-234-1.11", "-") End Sub
Also what would be the point of using variants? THe Input is a string, there is no reason ot use a variant...you can't input an Integer or Long etc.. only strings, therefore only a string array would ever be needed.
As pointless as this discussion is :D ...
I have to believe that this would work if Split returned a Variant (it doesnt work):
VB Code:
Private Sub Command1_Click() Dim arr() As Variant arr = Split("a-1-e-234-1.11", "-") End Sub
Uh, sure there is..... real-world example... I have a grid... I store the col headers in a string that I can split out and use to set the headres, at the same time, I have a secondary string that contains 0's and 1' that indicate if the col is to be shown or not. Since the Split can return a variant, I don't have to worry about CLng'ng the array element.... it's ready fopr me to use as is.Quote:
Originally posted by Arc
Also what would be the point of using variants? THe Input is a string, there is no reason ot use a variant...you can't input an Integer or Long etc.. only strings, therefore only a string array would ever be needed.
You're making it sound like the split would return the numbers as Variant/Integer ... and it won't (I'm pretty sure anyway). When splitting to a Variant "Splits in" a Variant/String array. So if you are in the habit of Clng your Strings, you'd still find the same need to Clng your Variant/Strings would you not?Quote:
Originally posted by techgnome
Uh, sure there is..... real-world example... I have a grid... I store the col headers in a string that I can split out and use to set the headres, at the same time, I have a secondary string that contains 0's and 1' that indicate if the col is to be shown or not. Since the Split can return a variant, I don't have to worry about CLng'ng the array element.... it's ready fopr me to use as is.
From the horse's mouth:
Quote:
Microsoft Office 2000/Visual Basic Programmer's Guide
--------------------------------------------------------------------------------
The Split Function
The Split function takes a string and converts it into an array of strings. By default, it divides the string into elements by using the space character as a delimiter, so that if you pass in a sentence, each element of the array contains a word. For example, if you pass this string to the Split function
...
Uhh well it's up to you to convert the string into a lng etc.. It would be stupid for Microsoft to use a Variant in a Function that only accepts strings.. If they wanted to return a Variant they would allow you to input a variant as the value IMHO!:DQuote:
Originally posted by techgnome
Uh, sure there is..... real-world example... I have a grid... I store the col headers in a string that I can split out and use to set the headres, at the same time, I have a secondary string that contains 0's and 1' that indicate if the col is to be shown or not. Since the Split can return a variant, I don't have to worry about CLng'ng the array element.... it's ready fopr me to use as is.
For Example :
Function Split(Item as Variant, Delimiter as Variant) as Variant
That way you could input a Number or a string, which is the only reason to ever use a variant.
A=Split(123.45,.)
This would return 123 and 45
Split returns a Variant array scoped as a string array, same as Mid, Left, Right returns a variant scoped as a string (however with those functions you can use Mid$, Left$, Right$ to return a string).
That this doesn't work:Is because the variant is declared in the wrong way the code should look like this:VB Code:
Dim v() As Variant v = Split("x-y-z", "-")In other words no paranthesis in the declaration.VB Code:
Dim v As Variant v = Split("x-y-z", "-")
The fact that VB.Net also has a Split method has nothing to do with the VB6 Split function, the implementation is different.
The reason it returns a variant array is because Split existed in VBScript before it was introduced in VB6.
However this discussion is purely academic since it doesn't really matter.
I don't see where the confusion comes from. According to help,Since all elements of an array are the same type, and the above says the elements are strings, doesn't that mean it returns a string array?Quote:
Split Function
Returns a zero-based, one-dimensional array containing a specified number of substrings
As I said this discussion is purely academic.... The fact that it returns an array of substrings doesn't contradict the fact that it actually returns a variant array scoped as strings.Quote:
Originally posted by seaweed
I don't see where the confusion comes from. According to help,Since all elements of an array are the same type, and the above says the elements are strings, doesn't that mean it returns a string array?
Dim v() as Variant isn't "wrong"Quote:
Originally posted by Joacim Andersson
Split returns a Variant array scoped as a string array, same as Mid, Left, Right returns a variant scoped as a string (however with those functions you can use Mid$, Left$, Right$ to return a string).
That this doesn't work:Is because the variant is declared in the wrong way the code should look like this:VB Code:
Dim v() As Variant v = Split("x-y-z", "-")In other words no paranthesis in the declaration.VB Code:
Dim v As Variant v = Split("x-y-z", "-")
The fact that VB.Net also has a Split method has nothing to do with the VB6 Split function, the implementation is different.
The reason it returns a variant array is because Split existed in VBScript before it was introduced in VB6.
However this discussion is purely academic since it doesn't really matter.
in the example with v() you are creating a variant array
in the example with v you are creating an variant that is later used to hold a string array
just out of curiosity is there a reference that says Split returns a Variant/String?
By the way, I agree that the argument is purely academic ... Im just hanging around out of curiosity ... and am still convinced Split returns a String ... :D
I already regret I even started to participate in this discussion :)
Anyway I hope you agree that it defiantly not return a string, if anything a string array is not a string even if it might just contain one string ;)
Joacim, I know you know your stuff from seeing previous posts. That said, the fact that I cant understand a single word of what you just typed must mean that I am way out of my league ... so Ill just bow out while I am ahead ... :DQuote:
Originally posted by Joacim Andersson
I already regret I even started to participate in this discussion :)
Anyway I hope you agree that it defiantly not return a string, if anything a string array is not a string even if it might just contain one string ;)
I'll just have to remain unconvinced by reason of my own ignorance :D
Cheers!
Well the truth is it's not academic... if Split returns a Variant(which it doesnt) then memory is being wasted for no reason since it can only accept strings!:D It would make more sense to create your own Split Function that used a string array in order to save memory.
Here is undeniable proff that Split returns a string.
VB Code:
Dim I As Integer Dim A As Variant A = Split("Test,test,test", ",") I = A(1)
You can't say I=A(1) becasue it is a string and you get a type mismatch error. If it were a Variant it wouldnt be a problem :)
not a good test IMHO ... try this
VB Code:
Private Sub Command1_Click() Dim i As Integer Dim s As String Dim a As Variant a = "11" s = "11" i = a MsgBox i i = s MsgBox i s = Split("11", "-") MsgBox s i = Split("11", "-") MsgBox i End Sub
now check this reference that supports Split returns Variant
http://support.microsoft.com/default...en-us%3B188007
then again , Ive seen blatant errors in the msdn kb before
Quote:
Originally posted by Muddy
not a good test IMHO ... try this
VB Code:
Private Sub Command1_Click() Dim i As Integer Dim s As String Dim a As Variant a = "11" s = "11" i = a MsgBox i i = s MsgBox i s = Split("11", "-") MsgBox s i = Split("11", "-") MsgBox i End Sub
now check this reference that supports Split returns Variant
http://support.microsoft.com/default...en-us%3B188007
then again , Ive seen blatant errors in the msdn kb before
Uhh if it returned a Variant you wouldnt get a type mismatch error in the code i provided... pretty simple stuff.
An assumption is far from fact.Quote:
Originally posted by Arc
Well since Split works in VB.net and VB.net does not have Variants i would assume it returns a string.
Any array of strings does not mean a string array. A variant can be an array of strings.Quote:
Originally posted by Muddy
From the horse's mouth:
The Split function takes a string and converts it into an array of strings.
Don't make me kick the horse in the mouth. ;)
No. According to what you posted, it is an array of substrings. Variants can be an array of substrings.Quote:
Originally posted by seaweed
Since all elements of an array are the same type, and the above says the elements are strings, doesn't that mean it returns a string array?
you are right of course if your saying my code was pointless ... I went brain dead (not uncommon for me)Quote:
Originally posted by Arc
Uhh if it returned a Variant you wouldnt get a type mismatch error in the code i provided... pretty simple stuff.
still not sure what point your test makes though ...
I give up ... :D
Maybe some machine language guru will dig into it and give a qualified answer ...
What I meant is that a string array is an array of strings. An array is not the same as a string. Split doesn't return a string.Quote:
Originally posted by Muddy
Joacim, I know you know your stuff from seeing previous posts. That said, the fact that I cant understand a single word of what you just typed must mean that I am way out of my league ... so Ill just bow out while I am ahead ... :D
I'll just have to remain unconvinced by reason of my own ignorance :D
Cheers!
Yupp, you can't assign a string to an integer unless the string contains a value that can be converted to an integer which this doesn't. A(1) is a Variant since you have declared it as such but it contains a string not an integer.Quote:
Originally posted by Arc
Here is undeniable proff that Split returns a string.
VB Code:
Dim I As Integer Dim A As Variant A = Split("Test,test,test", ",") I = A(1)
You can't say I=A(1) becasue it is a string and you get a type mismatch error. If it were a Variant it wouldnt be a problem :)
Can everyone agree that the Left and Right functions returns a substring? Yes it does! But the return value is a variant scoped as a string. If you want them to return a string you must use the Left$ and Right$ functions instead. The same goes for a bunch of other functions such as the Format, Mid, and Str functions.
Just run an application in MS debugger (or any other debugger) and watch the call stack just before the Split function returns and you'll see the memory usage for the return value is larger than the memory used by the string array receiving the return value (if that is declared as a String array that is).
Now I see your point .. and I do agree (even though I dont see the relevance to the topic.Quote:
Originally posted by Joacim Andersson
What I meant is that a string array is an array of strings. An array is not the same as a string. Split doesn't return a string.
That experiment you did with the debugger is convincing though ...
The relevancy is that you said that Split returned a string :)... Nevermind though, it was meant as a joke...Quote:
Originally posted by Muddy
... and am still convinced Split returns a String ... :D
Busted .... yeah that was mispoken.Quote:
Originally posted by Joacim Andersson
The relevancy is that you said that Split returned a string :)... Nevermind though, it was meant as a joke...
Thanks for letting me pick your brain on a pointless yet curiously addictive topic. :D