-
Sep 1st, 2021, 08:08 PM
#1
Thread Starter
Fanatic Member
Syntax Styles, Shortcuts and binding in VBA
I have a questions that i think have obvious answers but i just wanted to ask some of you guys here.
I come accross every now and then someone who uses a convention in VBA syntax that almost looks like its obfuscated. Its all late bound with no complete declarations and variable symbols are used instead. for example
Code:
Function ListPaths(dic)
Dim s$, v
For Each v In dic
s = s & v & " --> " & dic(v) & vbLf
Next
ListPaths = s
End Function
Ive been programming for decades and this syntax looks foreign to me until i start breaking it down. but i kind of like its conciseness but it just looks difficult to follow.
What are the benefits of coding like this? im particularly interested in the '$' used to declare the string and can use that technique to also return a string!?
Yes!!!
Working from home is so much better than working in an office...
Nothing can beat the combined stress of getting your work done on time whilst
1. one toddler keeps pressing your AVR's power button
2. one baby keeps crying for milk
3. one child keeps running in and out of the house screaming and shouting
4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
5. working at 1 O'clock in the morning because nobody is awake at that time
6. being grossly underpaid for all your hard work
-
Sep 2nd, 2021, 05:15 AM
#2
Re: Syntax Styles, Shortcuts and binding in VBA
unlike some other languages where all variables on a line are defined by the last type
vb6 /vba has to have every variable type defined individually, makes this shortcut style attractive
i certainly use variants more often than i should, but god gave them to us to use
and can use that technique to also return a string!?
seems to work when i tested
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Sep 2nd, 2021, 05:20 AM
#3
Thread Starter
Fanatic Member
Re: Syntax Styles, Shortcuts and binding in VBA
i certainly use variants more often than i should, but god gave them to us to use
Haha, and i try to be so disciplined with my conventions. i think im going to try using this convention for a bit and see if it improves my time coding, it certainly looks faster.
Yes!!!
Working from home is so much better than working in an office...
Nothing can beat the combined stress of getting your work done on time whilst
1. one toddler keeps pressing your AVR's power button
2. one baby keeps crying for milk
3. one child keeps running in and out of the house screaming and shouting
4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
5. working at 1 O'clock in the morning because nobody is awake at that time
6. being grossly underpaid for all your hard work
-
Sep 2nd, 2021, 06:14 AM
#4
Re: Syntax Styles, Shortcuts and binding in VBA
In general: i don't like the week "type"-Enforcing in vb6/vba, and i don't like variants, that's for sure.
It's just a source of hard to track bugs
Consider this:
Code:
Sub main()
Dim s1$ 'As String
Dim s2$ 'As String
Dim v1 'As Variant
Dim v2 'As Variant
Dim l1& 'As Long
Dim l2& 'As Long
s1 = 10
s2 = 5
v1 = 10
v2 = 5
l1 = 10
l2 = 5
Debug.Print s1 & s2
Debug.Print s1 + s2
Debug.Print v1 & v2
Debug.Print v1 + v2
Debug.Print l1 & l2
Debug.Print l1 + l2
End Sub
Returns:
Code:
105
105
105
15
105
15
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Sep 3rd, 2021, 03:35 AM
#5
Re: Syntax Styles, Shortcuts and binding in VBA
is that different than if the variable are fully dimensioned? i don't believe so, the bigger issue there is the possibly incorrect use of & or +, depending on the desired result
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Sep 3rd, 2021, 04:46 AM
#6
Re: Syntax Styles, Shortcuts and binding in VBA
Originally Posted by westconn1
is that different than if the variable are fully dimensioned? i don't believe so, the bigger issue there is the possibly incorrect use of & or +, depending on the desired result
True, there is no difference in fully Dimensioned, but it was more about to avoid variants.
v1 & v2 implies String-Concat, while v1+v2 implies numeric addition.
Yes, i know, that in this case (as well for l1 & l2) the operator is the culprit by implicitely casting it to the types string/long.
It's just about to avoid variants and as you, correctly, said the "misuse" of those 2 operators
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Sep 8th, 2021, 10:01 AM
#7
Thread Starter
Fanatic Member
Re: Syntax Styles, Shortcuts and binding in VBA
Does this indirectly point to my problem with arrays that a posted a few days ago regarding variant string arrays and actual string variants?
https://www.vbforums.com/showthread....-to-a-function
Yes!!!
Working from home is so much better than working in an office...
Nothing can beat the combined stress of getting your work done on time whilst
1. one toddler keeps pressing your AVR's power button
2. one baby keeps crying for milk
3. one child keeps running in and out of the house screaming and shouting
4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
5. working at 1 O'clock in the morning because nobody is awake at that time
6. being grossly underpaid for all your hard work
-
Sep 8th, 2021, 11:09 AM
#8
Re: Syntax Styles, Shortcuts and binding in VBA
At a guess: yes.
i remember having something similar
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
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
|