Page 2 of 2 FirstFirst 12
Results 41 to 59 of 59

Thread: What kind of an array does SPLIT return? A Variant Scoped with String Elements!

  1. #41

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    From the posts thus far, am I right in understanding that
    VB Code:
    1. dim strArr() as String
    is a STRING array, and
    VB Code:
    1. strArr = Split(arg1, arg2)
    is a VARIANT populated with strings and that there is a difference between the two?

    If so
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim strT As String
    3. Dim varT As Variant
    4. Dim strX() As String
    5. Dim varX As Variant
    6.  
    7. strT = "A-TEST-FOR-FINDING-OUT-WHAT-SPLIT-DOES"
    8. varT = "A-TEST-FOR-FINDING-OUT-WHAT-SPLIT-DOES"
    9. MsgBox TypeName(strT)
    10. MsgBox TypeName(varT)
    11.  
    12. strT = varT
    13. MsgBox TypeName(strT)
    14. strT = "A-TEST-FOR-FINDING-OUT-WHAT-SPLIT-DOES"
    15. varT = strT
    16. MsgBox TypeName(varT)
    17.  
    18.  
    19.  
    20. strX = Split(strT, "-")
    21. varX = Split(varT, "-")
    22. MsgBox TypeName(strX)
    23. MsgBox TypeName(varX)
    24.  
    25. strX = varX
    26. MsgBox TypeName(strX)
    27. strX = Split(strT, "-")
    28. varX = strX
    29. MsgBox TypeName(varX)
    30.  
    31. ReDim strX(12) As String
    32. strX(0) = "W"
    33. strX(1) = "H"
    34. strX(2) = "A"
    35. strX(3) = "T"
    36. strX(4) = " "
    37. strX(5) = "I"
    38. strX(6) = "S"
    39. strX(7) = " "
    40. strX(8) = "T"
    41. strX(9) = "H"
    42. strX(10) = "I"
    43. strX(11) = "S"
    44. strX(12) = "?"
    45.  
    46. MsgBox TypeName(strX)
    47. strX = varX
    48. MsgBox TypeName(varX)
    49.    
    50. End Sub
    does not show any other result other than STRING() and/or STRING for the variables. Using VarType gives the undifferentiable result of 8200 for both as well, though I am not able to deciphet that result.

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  2. #42

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Is this proof that SPLIT has everything to do with STRINGS and nothing to do with VARIANTS?
    VB Code:
    1. Private Sub Command1_Click()
    2. 'VarType 8192 is VbArray
    3. 'VarType 3 is Long
    4. 'VarType 8 is String
    5. 'VarType 12 is Variant
    6.  
    7. On Error Resume Next
    8. Dim x() As Long, y() As String
    9. Dim z() As Variant, xyz As Variant
    10. MsgBox varType(x) & _
    11.     vbCrLf & _
    12.     varType(y) & _
    13.     vbCrLf & _
    14.     varType(z) & _
    15.     vbCrLf & _
    16.     varType(xyz)
    17.  
    18. 'X  is 8195 --> 8192 + 3
    19. 'Y is 8200 --> 8192 + 8
    20. 'Z is 8204 --> 8192 + 12
    21. 'XYZ is 0 --> Empty
    22.    
    23.  
    24. x = Split("1-2-3-4", "-") 'Type mismatch
    25. y = Split("1-2-3-4", "-")
    26. z = Split("1-2-3-4", "-") 'Type Mismatch
    27. xyz = Split("1-2-3-4", "-")
    28.  
    29. MsgBox varType(x) & _
    30.     vbCrLf & _
    31.     varType(y) & _
    32.     vbCrLf & _
    33.     varType(z) & _
    34.     vbCrLf & _
    35.     varType(xyz)
    36.    
    37. 'Now the results are
    38. 'X  is 8195 --> 8192 + 3
    39.                             'Take a look at the next line and
    40. 'Y is 8200 --> 8192 + 8
    41.  
    42. 'Z is 8204 --> 8192 + 12
    43.            
    44.                             'the one after this
    45. 'XYZ is 8200 --> 8192 + 8
    46.  
    47. 'Split returns a vbArray of Strings
    48. 'not a vbArray of variants
    49.  
    50. 'Meaning a vbArray is different from a vbVariant
    51.  
    52. End Sub

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  3. #43

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Originally posted by Joacim Andersson
    .......
    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 missed the above, while reading thro' the thread.

    A word of warning: Don't you ever say "..academic since it doesn't really matter" anywhere within my earshot (or eyesight). I come from a family (within 2 generations and only once removed) of proffessors, school principals, teachers, college lecturers, authors, philosophers, language scholars and of course my pa is a lawyer. Acedemics is perhaps the only thing that matters.

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  4. #44
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Using VarType doesn't prove anything else than the data is a string. A variant can hold a string and if it does the return value for VarType is vbString. The only time VarType returns vbVariant is when when you have an array of Variants. An array of variants is not the same as a variant array scoped as string. An array of variants is an array of different types of data, a string, an integer, a single and so on.

    If you assign a variant array of strings to a string array you don't have anything else but a string array. Again I suggest to anyone to step through a VB EXE, that uses the Split function, through a binary debugger and watch the stack just before Split returns and you will see that the memory used for the array is the equal of manually setting up the same string array in variants.

    I didn't mean to offend anyone by saying "academic" and "doesn't matter" in the same sentence But in my opinion it still doesn't matter how VB internally treat the return value of the function. Of course if it would have used pure VB strings it would have been slightly faster and less memory would be consumed while the function is running. But I don't care, memory is released as soon as the call fall out of scope anyway. The importent thing is not to assign the return value to a variant. However even if I don't think the return type is important this is still an academic discussion (of no larger importance). Everything doesn't become important just because it's academic you know

  5. #45
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by KayJay

    does not show any other result other than STRING() and/or STRING for the variables. Using VarType gives the undifferentiable result of 8200 for both as well, though I am not able to deciphet that result.
    using TypeName doesn't prove anything, hence:

    VB Code:
    1. Private Sub Form_Load()
    2. Dim VarType As Variant
    3.  
    4.   VarType = 42
    5.   MsgBox TypeName(VarType) 'Integer
    6.  
    7.   VarType = "This is a string in a variant."
    8.   MsgBox TypeName(VarType) 'String
    9.  
    10. End Sub

    Never returns variant, yet your datatypes are variant.

    I still have not see any proof that it returns a String(), and have seen hard evidence that it returns a Variant.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #46

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    JA: I was asking whether vbArray is a different type of variable that vbVariant. The object browser says so. Not withstanding the fact that I am currently unable to debug the way you suggested, the second set of code I posted (using VarType) results in something as below
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim strX() As String
    3. Dim strY() As String
    4. Dim varX As Variant
    5.  
    6. ReDim strX(4) As String
    7. strX(0) = "A"
    8. strX(1) = "B"
    9. strX(2) = "C"
    10. strX(3) = "D"
    11. strX(4) = "E"
    12.  
    13. strY = Split("A-B-C-D-E", "-", -1, vbBinaryCompare)
    14. varX = Split("A-B-C-D-E", "-", -1, vbBinaryCompare)
    15.  
    16. MsgBox VarType(strX) _
    17. & vbCrLf & _
    18. VarType(strY) _
    19. & vbCrLf & _
    20. VarType(varX)
    21. End Sub
    All are vbArray + vbString.

    If what U say is true and proven, i.e. Split results in a Variant Scoped as a String and not a String Array, the above code leads me to conclude that there is no such thing as a string array at all in VB. Even
    VB Code:
    1. Dim strX() As String
    2.  
    3. ReDim strX(4) As String
    4. strX(0) = "A"
    5. strX(1) = "B"
    6. strX(2) = "C"
    7. strX(3) = "D"
    8. strX(4) = "E"
    results in a Variant Scoped as a String and not a String Array

    Am I right in my conclusion?

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  7. #47

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Originally posted by The Hobo
    using TypeName doesn't prove anything, hence:

    VB Code:
    1. Private Sub Form_Load()
    2. Dim VarType As Variant
    3.  
    4.   VarType = 42
    5.   MsgBox TypeName(VarType) 'Integer
    6.  
    7.   VarType = "This is a string in a variant."
    8.   MsgBox TypeName(VarType) 'String
    9.  
    10. End Sub

    Never returns variant, yet your datatypes are variant.

    I still have not see any proof that it returns a String(), and have seen hard evidence that it returns a Variant.
    An array of variants with the same datatypes in all its elements
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim varX() As Variant, varY As Variant
    3. ReDim varX(4) As Variant
    4. varX(0) = CStr("A")
    5. varX(1) = CStr("B")
    6. varX(2) = CStr("C")
    7. varX(3) = CStr("D")
    8. varX(4) = CStr("E")
    9. MsgBox VarType(varX) '8192 + 12 = 8204 --> An array of variants
    10. varY = varX()
    11. MsgBox VarType(varY) '8192 + 12 = 8204 --> An array of variants
    12. varY = Split("A,B,C,D,E", ",", -1, vbBinaryCompare)
    13. MsgBox VarType(varY) '8192 + 8 = 8200 --> An array of strings
    14. End Sub

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  8. #48
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    Originally posted by KayJay
    \
    VB Code:
    1. Dim strX() As String
    2.  
    3. ReDim strX(4) As String
    4. strX(0) = "A"
    5. strX(1) = "B"
    6. strX(2) = "C"
    7. strX(3) = "D"
    8. strX(4) = "E"
    results in a Variant Scoped as a String and not a String Array

    Am I right in my conclusion?
    THis would result in a String array ... what led you to the conclusion that it was a variant scoped as a string in this case?

  9. #49

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Muddy, because VarType VBARRAY plus VarType VBSTRING is the result of both a hard coded array of strings and the result of a SPLIT() function. Hence if SPLIT() results in Variant Scoped as a String, then so should a hard code array of strings.

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  10. #50
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    Originally posted by KayJay
    Muddy, because VarType VBARRAY plus VarType VBSTRING is the result of both a hard coded array of strings and the result of a SPLIT() function. Hence if SPLIT() results in Variant Scoped as a String, then so should a hard code array of strings.
    I dont understand what you are saying, but debug / run the following code with a watch on strx stry and strz
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim strX() As String
    3. Dim strY() As Variant
    4. Dim strZ As Variant
    5.  
    6. ReDim strX(1)
    7. ReDim strY(1)
    8.  
    9. strX(0) = "A"
    10. strX(1) = "B"
    11. strY(0) = "A"
    12. strY(1) = "B"
    13. strZ = strX
    14.  
    15.  
    16. End Sub

    now look at the var types in the watch window. It shows that:
    strx is a string array
    stry is a variant array with each element scoped to a string
    strz is a variant (NOT a variant array) that is scoped as a string array

    Split seems to return the the third example (a variant scoped as a string array)

  11. #51

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    I did and some.
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim strX() As String
    3. Dim strY() As Variant
    4. Dim strZ As Variant
    5. Dim strXYZ() As String
    6. Dim varXYZ As Variant
    7.  
    8. ReDim strX(1)
    9. ReDim strY(1)
    10.  
    11. strX(0) = "A"
    12. strX(1) = "B"
    13. strY(0) = "A"
    14. strY(1) = "B"
    15. strZ = strX
    16.  
    17. strXYZ = Split("a,b,c,d", ",")
    18. varXYZ = Split("a,b,c,d", ",")
    19. varXYZ = 10
    20. End Sub
    Conclusions:

    1) strY is not a variant array with each element scoped to a string. It is Variant/Variant(0 to 1). Scoped to a varaint, not a string

    2) strZ, I agree with you.

    3) strXYZ, the result of a Split, is String(0 to 1). That is not a Variant with a scope of String. Just a String Array.

    4) varXYZ , when assigned to a Split, does retain its vartype of Variant and gains a scope of String. It retains its type and gains a scope of an Integer as well.

    Ergo, declaring a variable as a variant retains its type and gains a scope depending on the RHS. Agreed.

    How that does prove that a SPLIT in the RHS generates a Variant with a Scope of Strings on the LHS?


    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  12. #52
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    1) strY is not a variant array with each element scoped to a string. It is Variant/Variant(0 to 1). Scoped to a varaint, not a string
    expand strY in the debug watch and you can see each element is scoped to a string
    3) strXYZ, the result of a Split, is String(0 to 1). That is not a Variant with a scope of String. Just a String Array.
    i agree that strXYZ is a string array but that doesnt mean that split returns a String ... You could assign the contents of any variable type to a string and it would be coerced to string
    Ergo, declaring a variable as a variant retains its type and gains a scope depending on the RHS. Agreed.
    agreed
    How that does prove that a SPLIT in the RHS generates a Variant with a Scope of Strings on the LHS?
    I dont think any of this proves it one way or another. I can say the Jaocim's test (stack size in assembly debugger) was a convincing argument that Split returns a variant ...

  13. #53
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    Although I don't understand the importance of the discussion, I would come to the conclusion that Split returns a variant, just by the defination i see in the object browser:
    Function Split(Expression As String, [Delimiter], [Limit As Long = -1], [Compare As VbCompareMethod = vbBinaryCompare])

    No return type is specified, which means it returns a variant.
    Frans

  14. #54

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Good enough...but

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  15. #55
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Originally posted by KayJay
    Muddy, because VarType VBARRAY plus VarType VBSTRING is the result of both a hard coded array of strings and the result of a SPLIT() function. Hence if SPLIT() results in Variant Scoped as a String, then so should a hard code array of strings.
    VB of course have string arrays. When a variant is "scoped" as a type it only means that it has been assigned a value of a specific type. You can with a variant change the type though by assigning a new type of value to it. With an Integer you can't assign a string, right? But if you have assigned a string to a variant it is now scoped as a string which means you can't now assign that value to an integer, however you can still assign an Integer to the variant variable, which is then scoped as an Integer.

    A variable declared as a string is always scoped as a string and can't be changed (even though an Integer can always be converted to a string the opposit is not always possible).

    What the VarType function does is to returned the scoped value. So the VarType of a string is always returned as vbString no matter what variable type it is that holds the string (there's only two types that can hold a string: String and Variant).

    See the following example:
    VB Code:
    1. Dim v As Variant
    2.  
    3. 'check the VarType of v before we have assigned a value
    4. Debug.Print VarType(v) 'returns vbEmpty
    5. v = "Hello World" ' "Hello World" is a string constant
    6. Debug.Print VarType(v) 'returns vbString since v now contains a string
    7. v = 25 ' 25 is an Integer constant
    8. Debug.Print VarType(v) 'returns vbInteger since v now contains an Integer
    9. v = "75" ' "75" is a STRING constant but can be converted to an Integer
    10. Debug.Print VarType(v) 'Even though 75 is an Integer value, "75" is not, returns vbString

  16. #56
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Anybody have any MS contacts they can call up?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  17. #57
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Originally posted by The Hobo
    Anybody have any MS contacts they can call up?
    Why, my tests with a debugger clearly shows that Split returns a variant, and as Frans C mentioned, the Object Browser also shows that. However if you assign the return value to a String array, you've got a string array and nothiing else.

  18. #58
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Well, maybe a test would involve passing the return value to a C++ Dll that only accepted an array of characters.... but that would be complicated... perhaps an API (lol) that accepts an array of strings.... (which probably wouldn't work either since the VB compiler would probably implicity cast them)....

    Anyway, for the record, I believe the return type is variant for .... String Arrays are not a primitive type... while a Variant is...

  19. #59

    Thread Starter
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Ok.

    1) A RHS with a Split() returns a Variant scoped with Strings, in memory, before assigning a value to LHS.

    2) If LHS is a String() then LHS remains a String()

    3) If LHS is Varaint then LHS becomes a Variant scoped with Strings

    4) VarType returns the current Scope of the its Argument

    5) So does VarName

    6) Thank You

    Regards

    KayJay


    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

Page 2 of 2 FirstFirst 12

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