vb6 & character after a variable [duplicate] - vb6

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.

Related

Warning : previous definition of Variable was here - Ruby

Every single time I load my program, even for the fist time, it says
file.rb:9: warning: already initialized constant W_mum
file.rb:6: warning: previous definition of W_mum was here.
a little help here?
W_mum = gets.to_i
elsif (W_mum = 1)
Ruby uses two different "storage bins" for data: variables and constants. In your source code, you can identify them y their first letter: constants always have a capital letter at the start of their name, variables a lower-case letter.
In your case, you thus have a constant named W_mum. Now, when you first set a value to a constant and then later set a different value to it, Ruby will show a warning (as such: you can set new values to constants, but you should not).
Now, as for why Ruby warns here: in your elsif, you are actually assigning the constant the value 1. This might be a bug though. Instead of an assignment with =, you likely intended to use a comparison here, using the == operator.

global optin available to set all numbers in script as decimal?

I just discovered the problem doing arithmetic using vars with leading 0's. I found the solution for setting individual vars to decimal using:
N=016
N=$((10#$N)) # force decimal (base 10)
echo $((N + 2))
# result is 18, ok
But I have multiple vars in my script that may or may not take a leading zero when run. I wonder if there is a global option that can be set to specify that all numbers in the script are to be interpreted as decimal? Or would there be a potential problem with doing so that I perhaps did not take into account?
I thought the set command might have such an option but after referring to the man page I did not read anything that looked like it would do the job.
As far as I can tell, this is an (unfortunate) convention established by the B language than a leading 0 introduces an octal number.
By looking at the bash sources, it seems that this convention is hard-coded in several places (lib/sh/strtol.c, builtins/common.c and concerning that specific case in expr.c, function strlong). So to answer to your question, no there isn't a global option to set all numbers as decimal.
If you have number in base 10 potentially prefixed by 0 you want perform calculation on, you might use the ${N#0} notation to refer to them.
sh$ N=010
sh$ echo $((${N#0}+0))
10
I don't know if this is more readable, or even less error prone_ than the solution you proposed in your question, though.

What is good style for variable declaration in fortran?

So I have a whole lot of variables I need to declare, and the original code looked like this:
DIMENSION energy_t(20000),nrt(20000),npsh(1000),xx(1000),yy(1000),
:step(1000),stepz(1000),r1(1000),rr(1000),ic(1000),diffrr(1000)
And I rewrote it as this:
DIMENSION
:energy_t(20000),
:nrt(20000),
:npsh(1000),
:step(1000),
:r1(1000),
:rr(1000),
:ic(1000),
:diffrr(1000)
Is this considered good style, or are there better ways? Note that the second way allows for comments with each variable, and I don't have to use line continuations if I might add another variable.
P.S.: is there a consensus/style bible/widely regarded source on Fortran programming style & good practices?
Good style is not to use the dimension statement in the first place. Especially if you use implicit typing. Every variable should have a declared type and is better to put the array dimension there. Use attributes with the type declaration (Fortran 90+).
real :: energy_t(20000), nrt(20000)
real, dimension(1000) :: npsh, xx, yy, step, stepz, r1, rr, ic, diffrr
Keep lines not too long. Both ways of declaring size (shape) are possible.
If you need Fortran 77, you are more limited, but still
real energy_t(20000), nrt(20000)
real npsh(1000), xx(1000), yy(1000), step(1000), stepz(1000)
real r1(1000), rr(1000), ic(1000), diffrr(1000)
is probably better.
Try to group related variables on one line and the others on different lines.
I would also suggest to declare parameter constants for the sizes 1000 and 20000.
Good style would be to parametrize the dimensions
integer, parameter:: NODES_MAX = 1000, TIMES_MAX = 2000, COORD_MAX = 1000
real energy_t(TIMES_MAX), ..
real npsh(NODES_MAX), xx(COORD_MAX) ...
so that the loops can be parameterized.
do ii = 1, COORD_MAX
xx(ii) = ...
yy(ii) = ..
end do
and error checks can be made
if (ii .gt. NODES_MAX) then
print *, 'Please increase NODES_MAX oldvalue=', NODES_MAX, ' required=', ii
pause
end if
This will also minimize the number of changes required when the dimensions are increased/decreased. This style could also have been applied 30+ years ago when F77 came out.

Understanding Const expression in VBScript

Well, I try to understand limitations in Const expressions in VBScript. I was not able to use anything except literals. What the docs say is:
Literal or other constant, or any combination that includes all
arithmetic or logical operators except Is.
So, if "that includes all arithmetic or logical operators" then logically I expect I can do something like this:
Const X = (1 + 2)
But that brings the error "Expected literal constant". I found an interesting answer here that allows one to cheat, at some level, so the above can be done with:
Execute "Const X = " & (1 + 2)
But my question is about standard constant declaration. If by chance the docs said something like "expression could be ONLY literal", then I would never ask.
So what Else I can use (besides literal)?
Script56.chm says the following in the Remarks section:
Constants are public by default. Within procedures, constants are always private; their visibility can't be changed. Within a script, the default visibility of a script-level constant can be changed using the Private keyword.
To combine several constant declarations on the same line, separate each constant assignment with a comma. When constant declarations are combined in this way, the Public or Private keyword, if used, applies to all of them.
You can't use variables, user-defined functions, or intrinsic VBScript functions (such as Chr) in constant declarations. By definition, they can't be constants. You also can't create a constant from any expression that involves an operator, that is, only simple constants are allowed. Constants declared in a Sub or Function procedure are local to that procedure. A constant declared outside a procedure is defined throughout the script in which it is declared. You can use constants anywhere you can use an expression.
The bit in italics above makes a nonsense of the "or any combination that includes all arithmetic or logical operators except Is" claim.

<variable>! syntax in Visual Basic 6

I'm working through some legacy code for a client and I think I understand this line but I need vb expert to double-check me.
QS1! = Unit1.XICFMc(1)
My guess is that this saying:
"If QS1 doesn't exist, DIM it and assign it the value in the first position in Unit1.XICFMc"
Am I right? I can't find a definition for QS1 anywhere in the project - which is what lead me to my guess above.
Given the lack of Option Explicit, a variable is implicitly created when first used.
The ! clarifies that QS1 is of type Single.
Isn't a trailing ! mean that the QS1 variable should be typed as Single. This is a holdover from early versions of basic that used postfixes to declare types. v$ was a string, v% was an integer etc. IIRC, you cannot have option explicit on for this to work.

Resources