开发者

vb6 & character after a variable [duplicate]

This question already has answers here: What do ! and # mean when attached to numbers in VB6? 开发者_运维知识库 (3 answers) Closed 9 years ago.

There is the following in some code I'm trying to figure out:

For I& = 1 To...

I'm not familiar with the & after a variable. What does that represent?

After some further research, it looks like the I& is being defined as a type LONG. Now my questions is why would they be doing this? Is it overkill or just legacy code?


The legacy BASIC Language had several ways of declaring variables. You could use data type suffixes ($, %, &, !, or #) to the variable name

x$ = "This is a string"   ' $ defines a string
y% = 10                   ' % defines an integer
y& = 150                  ' & defines a long integer
y! = 3.14                 ' ! defines a single
y# = 12.24                ' # defines a double


Legacy. Old-school (pre-.NET) Visual Basic used variable name suffixes in lieu of (optionally) variable types.


You are right - putting an ampersand & after a number or a variable means that it is of a Long 32-bits type.

So the answer is, how many iterations does the loop need - is it possible, that it would exceed 16 bits integer?

With no data type identifier after the i, it is implied to be of the native Integer (the default). Therefore this i is expressed as an Integer, which makes it a 16-bit i.

So, I'd say it is the original developer had this habit of explicitly stating the variable type with &, and whether it was really needed there depends on the number of iterations that the For..Next loop has to support in this case.


Most likely it is either old code ported forward to VB6 from QBasic, etc. or else just a bad habit some individual programmer had from that era. While kind of sloppy its meaning should be obvious to a VB6 programmer, since it can be used with numeric literals in many cases too:

MsgBox &HFFFF
MsgBox &HFFFF&

These display different values because they are different values.

Yes it means Long but it often reflects somebody who fails to set the IDE option to auto-include Option Explicit in new modules when created.


Using symbolic notation (Integer - %, Long - &, Single - !, Double - #, String - $) is an excellent method for variable declaration and usage. It’s usage is consistent with "structured programming" and it’s a good alternative to Hungarian notation.

With Hungarian notation, one might define a string filename as “strFileName”, where the variable name is preceded by a lower case abbreviation of the variable type. The is contrary to another good programming practice of making all global variables begin with an upper case first letter and all local variables begin with a lower case. This helps the reader of your code instantly know the scope of a variable. Ie. firstName$ is a local string variable; LastName$ is a global string variable.

As with all programming, it’s good to follow conventions, ..whether you define your own conventions or somebody else’s conventions or industry conventions. Following no conventions is a very poor programming practice. Using symbolic notation is one type of naming convention.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜