-
Sep 18th, 2021, 10:36 PM
#1
Thread Starter
Frenzied Member
What are square brackets used for in VB6?
They do appear to be a legit thing, as VB6 recognizes when only an opening bracket is used without a closing bracket. So it doesn't see it as just a normal number or letter. It clearly has some special use for it. But what? I read that VB.net uses square brackets, but I've never read anything about VB6 having a special use for them, though it seems VB6 does have some special use for them. What is it?
-
Sep 18th, 2021, 11:15 PM
#2
Re: What are square brackets used for in VB6?
They can be used in a few restricted cases. One is for delimiting strings that are not legal identifiers (spaces between words for example) when using the "bang" syntax to access a collection item. Another is for illegal-syntax enum value names, typically ones beginning with an "_" symbol meant to be hidden in type information.
So just as described in the documentation.
-
Sep 18th, 2021, 11:24 PM
#3
Re: What are square brackets used for in VB6?
Oh, another case can be used in calling a DISPID_EVALUATE member of a late-bound object.
Code:
Private Sub Main()
Dim Fester As Object 'Must be late-bound instance.
Dim Range As Collection
Dim Msg As String
Dim I As Integer
Set Fester = New Fester
Set Range = Fester.[x:z]
With Range
For I = 1 To .Count
Msg = Msg & CStr(.Item(I)) & vbNewLine
Next
End With
MsgBox Msg, , "Range [x:z]"
Msg = vbNullString
Set Range = Fester.[i:k]
With Range
For I = 1 To .Count
Msg = Msg & CStr(.Item(I)) & vbNewLine
Next
End With
MsgBox Msg, , "Range [i:k]"
Msg = vbNullString
Set Range = Fester.[I]
With Range
For I = 1 To .Count
Msg = Msg & CStr(.Item(I)) & vbNewLine
Next
End With
MsgBox Msg, , "Range [I]"
End Sub
-
Sep 19th, 2021, 03:21 AM
#4
Re: What are square brackets used for in VB6?
Originally Posted by Ben321
They do appear to be a legit thing, as VB6 recognizes when only an opening bracket is used without a closing bracket. So it doesn't see it as just a normal number or letter. It clearly has some special use for it. But what? I read that VB.net uses square brackets, but I've never read anything about VB6 having a special use for them, though it seems VB6 does have some special use for them. What is it?
I wish VB6 had *more* use of square brackets as escape sequence for identifiers in general. Currently only enum names and callsite function/property names can be escaped.
It is not possible to escape function declarations like with Private Function [My Function] or Private Function [Close] so we cannot have a VB6 class which implements a Close method like ADODB.Connection for instance.
Edit: Btw, just tested TwinBasic and quoted identifiers are valid syntax there which makes so much sense. This compiles ok under TB:
Code:
Public Sub [Let's test if we can "break" quoted identifiers with long test name]()
Debug.Print "Test"
End Sub
cheers,
</wqw>
Last edited by wqweto; Sep 19th, 2021 at 09:57 AM.
-
Sep 19th, 2021, 10:32 AM
#5
Addicted Member
Re: What are square brackets used for in VB6?
what is the fester object? can be any?
Ten Years After - 01 You Give Me Loving
-
Sep 19th, 2021, 11:13 AM
#6
Re: What are square brackets used for in VB6?
Fester is just a silly demo class with a DISPID_EVALUATE member.
Fester.cls
Code:
Option Explicit
Private Space As Collection
'NOTE: Procedure ID = -5
Public Function Range(ByVal Scope As String) As Collection
'Scope value is a range of Items in Space collection addressed as
'letters from A to Z (case insensitive).
'
'For a degenerate range provide a single letter, for a range provide:
'
' {first letter}:{last letter}
Dim Bounds() As String
Dim First As Integer
Dim I As Integer
Bounds = Split(UCase$(Scope), ":")
First = Asc(Trim$(Bounds(0))) - Asc("@")
Set Range = New Collection
With Range
If UBound(Bounds) = 0 Then
.Add Space.Item(First)
Else
For I = First To Asc(Trim$(Bounds(1))) - Asc("@")
.Add Space.Item(I)
Next
End If
End With
End Function
Private Sub Class_Initialize()
Dim I As Integer
Set Space = New Collection
With Space
For I = 1 To 26
.Add I * 100000 + I
Next
End With
End Sub
DISPID values are documented in the MSDN Library Help that comes with VB/VS6.
Last edited by dilettante; Sep 19th, 2021 at 11:17 AM.
-
Sep 19th, 2021, 10:11 PM
#7
Re: What are square brackets used for in VB6?
About the only place I use them is as field name delimiters when getting/setting data within a database, and I've gotten to where I exclusively do it that way, except in the cases where I have to "build" the field name (i.e., quasi array).
Code:
BalanceServices![TotalTime] = CopBal.TotalTime
BalanceServices![SwayPathLengthMM] = CopBal.SwayPathLength
BalanceServices![AvgVelocityMMps] = CopBal.AvgVelocity
BalanceServices![AvgAccelMMps2] = CopBal.AvgAccel
BalanceServices![AvgJerkMps3] = CopBal.AvgJerk
BalanceServices![StdRadialDisplaceMM] = CopBal.StdRadialDisplace
BalanceServices![AvgRadialDisplaceMM] = CopBal.AvgRadialDisplace
BalanceServices![CircleArea95CIcm2] = CopBal.CircleArea95
BalanceServices![CovarianceXYmm2] = CopBal.CovarianceXY
BalanceServices![EllipseArea95CIcm2] = CopBal.EllipseArea95
BalanceServices![EllipseAngle] = CopBal.EllipseAngle
BalanceServices![FreqRevolveHertz] = CopBal.FreqRevolve
BalanceServices![AreaPerSecMM2ps] = CopBal.AreaPerSec
(The CopBal is just a UDT.)
It's just a habit I've gotten into, and I like it. I know there are other ways to address DB fields, but this works well.
I occasionally see them in an Enum I might grab for some API call, but I've never found any reason to use them in any Enum I've written myself.
Also, I suppose you might use them for VB6's search, when the "Pattern Matching" is turned on, which allows a limited type of RegEx searching. However, I extremely seldom use them for those purposes either.
Last edited by Elroy; Sep 19th, 2021 at 10:22 PM.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Sep 20th, 2021, 06:19 AM
#8
Re: What are square brackets used for in VB6?
Slightly off topic but may be relevant.
The behavior recently changed within the last couple months. Interop code and classes using the brackets [] in VS 2008 is now com-visible to VB6. I tried for weeks to get that working, but could not get the names visible or overridden through interop. Nothing worked. However, I just tried today and it works. It must of been in a Windows update with the cumulative rollup for 3.5, I'm assuming. I know for a fact, this did not work before but now it does. I'm making some bracket updates now... Thanks!
-
Sep 21st, 2021, 04:35 PM
#9
Thread Starter
Frenzied Member
Re: What are square brackets used for in VB6?
Originally Posted by dilettante
Oh, another case can be used in calling a DISPID_EVALUATE member of a late-bound object.
Code:
Private Sub Main()
Dim Fester As Object 'Must be late-bound instance.
Dim Range As Collection
Dim Msg As String
Dim I As Integer
Set Fester = New Fester
Set Range = Fester.[x:z]
With Range
For I = 1 To .Count
Msg = Msg & CStr(.Item(I)) & vbNewLine
Next
End With
MsgBox Msg, , "Range [x:z]"
Msg = vbNullString
Set Range = Fester.[i:k]
With Range
For I = 1 To .Count
Msg = Msg & CStr(.Item(I)) & vbNewLine
Next
End With
MsgBox Msg, , "Range [i:k]"
Msg = vbNullString
Set Range = Fester.[I]
With Range
For I = 1 To .Count
Msg = Msg & CStr(.Item(I)) & vbNewLine
Next
End With
MsgBox Msg, , "Range [I]"
End Sub
I tried using that, along with the code you posted below for the Fester class. It doesn't work. The line "Set Range = Fester.[x:z]" produces the error "Object doesn't support this property or method".
-
Sep 21st, 2021, 05:01 PM
#10
Re: What are square brackets used for in VB6?
But did you assign the DISPID ("Procedure ID") value to DISPID_EVALUATE (-5) on Fester's Range() method?
The comment doesn't do it, the comment is just a reminder. You have to set the value in metadata that doesn't appear and can't be edited in code windows within the IDE. That's why we have the "Tools|Procedure Attributes..." dialog.
-
Sep 21st, 2021, 11:31 PM
#11
Thread Starter
Frenzied Member
Re: What are square brackets used for in VB6?
Originally Posted by dilettante
But did you assign the DISPID ("Procedure ID") value to DISPID_EVALUATE (-5) on Fester's Range() method?
The comment doesn't do it, the comment is just a reminder. You have to set the value in metadata that doesn't appear and can't be edited in code windows within the IDE. That's why we have the "Tools|Procedure Attributes..." dialog.
What is Procedure ID, and how do you set it? Is this something you can do in VB6? Or do I need to compile this and then use external software to hack the Procedure ID in the compiled EXE file?
-
Sep 22nd, 2021, 02:54 AM
#12
Re: What are square brackets used for in VB6?
In the IDE's Tools menu there is an item Procedure Attributes... to open that dialog. There you can select Advanced>> to see and manipulate the advanced options of the current object module's Public members.
It's all in the manual.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|