dbTalk Databases Forums  

Multiple Variable Declaration

comp.databases.ms-access comp.databases.ms-access


Discuss Multiple Variable Declaration in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
David-W-Fenton
 
Posts: n/a

Default Multiple Variable Declaration - 01-18-2011 , 04:00 PM






Just read this:

http://thedailywtf.com/Articles/Feng-Shui.aspx

....and some commenters said that variable declarations of this form:

Dim lng1, lng2 As Long

....would create a variant and a long. Now, I'd always thought that
people said that it would make all the variables Longs, but I tested
and it's correct that this doesn't work, i.e., that only the last
variable is properly typed.

Am I misremembering that people have recommeded that? It's not
actually something I would ever do in my own code (I'm a one line
per variable type of coder), but I always thought it was something
you COULD do.

Am I misremembering? It's certainly the case that it doesn't work!

Public Sub test()
Dim lng1, lng2 As Long
Dim lng3 As Long, lng4 As Long

Debug.Print VarType(lng1) = VarType(lng2)
Debug.Print VarType(lng1)
Debug.Print VarType(lng2)
Debug.Print VarType(lng3) = VarType(lng4)
Debug.Print VarType(lng3)
Debug.Print VarType(lng4)
End Sub

(and in regard to the DailyWTF article, the programmer should turn
on Option Explicit before starting any search and replace
operation...)

--
David W. Fenton http://www.dfenton.com/
contact via website only http://www.dfenton.com/DFA/

Reply With Quote
  #2  
Old   
Bob Barrows
 
Posts: n/a

Default Re: Multiple Variable Declaration - 01-18-2011 , 04:37 PM






It's been that way as long as I can remember ... back to the mid 90s that is
:-)
David-W-Fenton wrote:
Quote:
Just read this:

http://thedailywtf.com/Articles/Feng-Shui.aspx

...and some commenters said that variable declarations of this form:

Dim lng1, lng2 As Long

...would create a variant and a long. Now, I'd always thought that
people said that it would make all the variables Longs, but I tested
and it's correct that this doesn't work, i.e., that only the last
variable is properly typed.

Reply With Quote
  #3  
Old   
Clif McIrvin
 
Posts: n/a

Default Re: Multiple Variable Declaration - 01-18-2011 , 04:55 PM



It *may* be that GWBasic used to allow multiple declarations as you
remember, but my memory isn't clear on that point, and I'm probably not
going to go fire up my last copy of DOS 6.x and try it to see <g>.

Clif

"Bob Barrows" <reb01501 (AT) NOSPAMyahoo (DOT) com> wrote

Quote:
It's been that way as long as I can remember ... back to the mid 90s
that is :-)
David-W-Fenton wrote:
Just read this:

http://thedailywtf.com/Articles/Feng-Shui.aspx

...and some commenters said that variable declarations of this form:

Dim lng1, lng2 As Long

...would create a variant and a long. Now, I'd always thought that
people said that it would make all the variables Longs, but I tested
and it's correct that this doesn't work, i.e., that only the last
variable is properly typed.





--
Clif McIrvin

(clare reads his mail with moe, nomail feeds the bit bucket :-)

Reply With Quote
  #4  
Old   
Tony Toews
 
Posts: n/a

Default Re: Multiple Variable Declaration - 01-18-2011 , 05:48 PM



On 18 Jan 2011 22:00:59 GMT, "David-W-Fenton" <dfassoc (AT) dfenton (DOT) com>
wrote:

Quote:
...and some commenters said that variable declarations of this form:

Dim lng1, lng2 As Long

...would create a variant and a long.
correct.

Quote:
Now, I'd always thought that
people said that it would make all the variables Longs,
I think different languages do work that way. Possibly C or C++ and
that this has tripped folks who are new to VB and VBA.

Tony
--
Tony Toews, Microsoft Access MVP
Tony's Main MS Access pages - http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
For a convenient utility to keep your users FEs and other files
updated see http://www.autofeupdater.com/

Reply With Quote
  #5  
Old   
Stuart McCall
 
Posts: n/a

Default Re: Multiple Variable Declaration - 01-18-2011 , 09:27 PM



"David-W-Fenton" <dfassoc (AT) dfenton (DOT) com> wrote

Quote:
Just read this:

http://thedailywtf.com/Articles/Feng-Shui.aspx

...and some commenters said that variable declarations of this form:

Dim lng1, lng2 As Long

...would create a variant and a long. Now, I'd always thought that
people said that it would make all the variables Longs, but I tested
and it's correct that this doesn't work, i.e., that only the last
variable is properly typed.

Am I misremembering that people have recommeded that? It's not
actually something I would ever do in my own code (I'm a one line
per variable type of coder), but I always thought it was something
you COULD do.

Am I misremembering? It's certainly the case that it doesn't work!

Public Sub test()
Dim lng1, lng2 As Long
Dim lng3 As Long, lng4 As Long

Debug.Print VarType(lng1) = VarType(lng2)
Debug.Print VarType(lng1)
Debug.Print VarType(lng2)
Debug.Print VarType(lng3) = VarType(lng4)
Debug.Print VarType(lng3)
Debug.Print VarType(lng4)
End Sub

(and in regard to the DailyWTF article, the programmer should turn
on Option Explicit before starting any search and replace
operation...)

--
David W. Fenton http://www.dfenton.com/
contact via website only http://www.dfenton.com/DFA/
It's always been that way since Quick Basic. Prior to that, in Gwiz there
were two ways to declare vars, declaration characters or DEF* ie:

DEFINT [A-G]
DEFSNG [H-L]

etc.

Buit you can still use declaration chars in VB/VBA if you wish, ie:

Dim lng1&, lng2&, lng3&

which is the closest legal syntax to the incorrect one described.

However, since there are far more variable types to declare these days,
which don't have declaration chars, 'As Type' must be used for them, so to
be consistent all vars ought to be declared this way (IMO). One line per var
declaration would be my recommendation too because it's less error prone to
scan lists than sentences.

Reply With Quote
  #6  
Old   
Tony Toews
 
Posts: n/a

Default Re: Multiple Variable Declaration - 01-19-2011 , 12:40 AM



On Wed, 19 Jan 2011 03:27:09 -0000, "Stuart McCall"
<smccall (AT) myunrealbox (DOT) com> wrote:

Quote:
One line per var
declaration would be my recommendation too because it's less error prone to
scan lists than sentences.
I declare as many as I can get on a line and still stay on the VBA
code window. Which usually isn't many as I
TendToUseLongVariableNames. And I use CamelCaseToCheckForTypos.

Tony

--
Tony Toews, Microsoft Access MVP
Tony's Main MS Access pages - http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
For a convenient utility to keep your users FEs and other files
updated see http://www.autofeupdater.com/

Reply With Quote
  #7  
Old   
Stuart McCall
 
Posts: n/a

Default Re: Multiple Variable Declaration - 01-19-2011 , 12:28 PM



"Tony Toews" <ttoews (AT) telusplanet (DOT) net> wrote

Quote:
On Wed, 19 Jan 2011 03:27:09 -0000, "Stuart McCall"
smccall (AT) myunrealbox (DOT) com> wrote:

One line per var
declaration would be my recommendation too because it's less error prone
to
scan lists than sentences.

I declare as many as I can get on a line and still stay on the VBA
code window. Which usually isn't many as I
TendToUseLongVariableNames. And I use CamelCaseToCheckForTypos.
I also use camel-case, but for readability, not typo checking. There's
Option Explicit for that.

Quote:
Tony

--
Tony Toews, Microsoft Access MVP
Tony's Main MS Access pages - http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
For a convenient utility to keep your users FEs and other files
updated see http://www.autofeupdater.com/

Reply With Quote
  #8  
Old   
Access Developer
 
Posts: n/a

Default Re: Multiple Variable Declaration - 01-19-2011 , 02:45 PM



The default, since the beginning of Access / and classic VB has always been
"variant" and each variable name without its own "AS clause" has defaulted
to "variant".

And, Option Explicit has, as long as I can remember, been a recommended
option. I have both Option Explicit and Option Compare Database, by default,
in all my modules.

Larry
Microsoft Office Access MVP
Co-Author, "Microsoft Access Small Business Solutions"



"David-W-Fenton" <dfassoc (AT) dfenton (DOT) com> wrote

Quote:
Just read this:

http://thedailywtf.com/Articles/Feng-Shui.aspx

...and some commenters said that variable declarations of this form:

Dim lng1, lng2 As Long

...would create a variant and a long. Now, I'd always thought that
people said that it would make all the variables Longs, but I tested
and it's correct that this doesn't work, i.e., that only the last
variable is properly typed.

Am I misremembering that people have recommeded that? It's not
actually something I would ever do in my own code (I'm a one line
per variable type of coder), but I always thought it was something
you COULD do.

Am I misremembering? It's certainly the case that it doesn't work!

Public Sub test()
Dim lng1, lng2 As Long
Dim lng3 As Long, lng4 As Long

Debug.Print VarType(lng1) = VarType(lng2)
Debug.Print VarType(lng1)
Debug.Print VarType(lng2)
Debug.Print VarType(lng3) = VarType(lng4)
Debug.Print VarType(lng3)
Debug.Print VarType(lng4)
End Sub

(and in regard to the DailyWTF article, the programmer should turn
on Option Explicit before starting any search and replace
operation...)

--
David W. Fenton http://www.dfenton.com/
contact via website only http://www.dfenton.com/DFA/

Reply With Quote
  #9  
Old   
Tony Toews
 
Posts: n/a

Default Re: Multiple Variable Declaration - 01-19-2011 , 04:55 PM



On Wed, 19 Jan 2011 18:28:49 -0000, "Stuart McCall"
<smccall (AT) myunrealbox (DOT) com> wrote:

Quote:
One line per var
declaration would be my recommendation too because it's less error prone
to
scan lists than sentences.

I declare as many as I can get on a line and still stay on the VBA
code window. Which usually isn't many as I
TendToUseLongVariableNames. And I use CamelCaseToCheckForTypos.

I also use camel-case, but for readability, not typo checking. There's
Option Explicit for that.
Immediate feedback. If the VBA editor doesn't change the variable
name to camel case instantly after hitting the space bar or
return/enter then I know I've made a typo. I don't have to wait to
hit the compile button.

Tony
--
Tony Toews, Microsoft Access MVP
Tony's Main MS Access pages - http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
For a convenient utility to keep your users FEs and other files
updated see http://www.autofeupdater.com/

Reply With Quote
  #10  
Old   
Clif McIrvin
 
Posts: n/a

Default Re: Multiple Variable Declaration - 01-19-2011 , 06:54 PM



"Tony Toews" <ttoews (AT) telusplanet (DOT) net> wrote

Quote:
On Wed, 19 Jan 2011 18:28:49 -0000, "Stuart McCall"
smccall (AT) myunrealbox (DOT) com> wrote:

One line per var
declaration would be my recommendation too because it's less error
prone
to
scan lists than sentences.

I declare as many as I can get on a line and still stay on the VBA
code window. Which usually isn't many as I
TendToUseLongVariableNames. And I use CamelCaseToCheckForTypos.

I also use camel-case, but for readability, not typo checking. There's
Option Explicit for that.

Immediate feedback. If the VBA editor doesn't change the variable
name to camel case instantly after hitting the space bar or
return/enter then I know I've made a typo. I don't have to wait to
hit the compile button.

Tony

I use that feature myself. Sometimes, I even remember to hit
Ctrl+<space> before I've typed AllThatLongVariableName and let
intellisense plug the name in for me (of course, I type it as
allthatlongvariablename).

--
Clif McIrvin

(clare reads his mail with moe, nomail feeds the bit bucket :-)

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.