Not a lot of people know this (or do they?)
I was browsing through some of my posts in another forum and tripped over something I'd completely forgotten.
Quite often we see things like
Code:
Dim int1, int2, int3 As Integer
where the author expects all 3 variables to be defined as Integer Types, whereas in reality only int3 will be of Integer Type the others will default to Variant.
Normally the suggestion would be to change to
Code:
Dim int1 As Integer, int2 As Integer, int3 As Integer
an alternative solution could be
Code:
DefInt i
Dim int1, int2, int3
The 'DefInt i' statement declares that all variables starting with the letter 'i' default to Integer Type, hence the Dim statement will define all 3 variables as Integer.
(Look up the 'DefType' statement in msdn)
Does anyone use the DefType statement ? If not why not ?
Re: Not a lot of people know this (or do they?)
Something to look at...NO, don't use, never used (never KNEW) this trade 'secret'. PROBABLY won't use it much, as I use (not good practice) 'x', 'y' and 'z' a lot as ints, not prefixing them as I should. So, I might end up doing this...Dim x, y, z and REALLY open up a can of worms....BUT, I DO see it as shortcut for the very busy programmer on a schedule....but as far as that goes, (as I doubt many programmers know about this), if the code is passed on to someone else (in a team or a replacement), they'd get confused. BUT, interesting. Thanks for the post.
Re: Not a lot of people know this (or do they?)
In addition, I see this statement:
Quote:
Deftype statements can only be used at the module level (that is, not within procedures).
Hence, if someone does not look at the MODULE, just at variable declarations within forms, he/she could scratch their heads.
Re: Not a lot of people know this (or do they?)
News to me. Can't see me adopting this, though...
Re: Not a lot of people know this (or do they?)
News to me as well, never used it, doubt that I will use it.
Re: Not a lot of people know this (or do they?)
I have notice one thing following I can declare inside the general section of code Window.i am able to compile it .but when I tried to declare the same variables inside any sub or Method I am getting error.invalid inside procedure
Code:
Option Explicit
DefInt C
Dim cInt1
Re: Not a lot of people know this (or do they?)
Knew it but never thought it was worth my while to use it
Re: Not a lot of people know this (or do they?)
I use it as routinely as Option Explicit at the top of most of my code as in DefInt A-Z. I have done that ever since Variants were made the default type (a long time ago now), that way you never end up with a Variant unless you explicitly Dim one. In the very old days Integers were the default Type.
These days I generally prefer DefLng A-Z because Longs are more versatile and quicker.
Use makes converting a whole module that previously used integers into one using Longs a very simple task.
Re: Not a lot of people know this (or do they?)
Used it since GW-Basic (or before?)
Quote:
Syntax:
DEFtype letters
Comments:
type is INT (integer),
SNG (single-precision number),
DBL (double-precision number),
STR (string of 0-255 characters).
letters are letters (separated by commas) or range of letters of the alphabet.
A DEFtype statement declares that variable names beginning with the letter(s) specify that type of variable.
However, a type declaration character (%,!,#,$) always takes precedence over a DEFtype statement in the typing of a variable.
Re: Not a lot of people know this (or do they?)
I could not see using DEFInt A-Z as there are always string vars in my apps,
I could see
DEFInt i
DefLng L
DefString S
But really would only consider using it if it was faster than the other ways
Re: Not a lot of people know this (or do they?)
lol....Def is a hold out from QuickBasic Extended. DefInt A-Z was a staple in my QuickBasic programs. I abandoned the practice when I moved to VB2 though.
Quote:
Originally Posted by
Magic Ink
In the very old days Integers were the default Type.
Actually, Single was the default type in the old days, at least in the days of QuickBasic and BasicA.
Re: Not a lot of people know this (or do they?)
In the old days I always used a lot of $ and # notations ;)
Re: Not a lot of people know this (or do they?)
Quote:
Originally Posted by
DataMiser
In the old days I always used a lot of $ and # notations ;)
Yea.
Good times....
Re: Not a lot of people know this (or do they?)
dm>I could not see using DEFInt A-Z as there are always string vars in my apps
jggtz>However, a type declaration character (%,!,#,$) always takes precedence over a DEFtype statement in the typing of a variable.
So you can override the default by simply explicitly dimming a variable as some other Type as in;
Dim v as Variant
Dim S as String
Dim T$
Dim i 'however will be an integer because 1. Integer is the default type and 2. no Type is explicitly specified
....
Re: Not a lot of people know this (or do they?)
>Actually, Single was the default type in the old days...
You are correct and I have been using DefInt A-Z for longer than I had thought...
Re: Not a lot of people know this (or do they?)
Quote:
Originally Posted by
Magic Ink
d
So you can override the default by simply explicitly dimming a variable as some other Type as in;
Ok, so then there really is not much point in using it then, I prefer Dim vars in such a way that you can tell what they are where they are dimmed rather than having to go and look for the DEF statement in one of the modules.
Re: Not a lot of people know this (or do they?)
I have seen that too! I remember reading somewhere that while you can get away with
Code:
Dim int1, int2, int3 As Integer
in .Net in VB6 you need to use
Code:
Dim int1 As Integer, int2 As Integer, int3 As Integer
Edit:
I should point out that the .Net language I am referring to is VB.NET rather then C# but the way they define variables is different.
Re: Not a lot of people know this (or do they?)
Quote:
Originally Posted by
Nightwalker83
I have seen that too! I remember reading somewhere that while you can get away with
Code:
Dim int1, int2, int3 As Integer
in .Net in VB6 you need to use
Code:
Dim int1 As Integer, int2 As Integer, int3 As Integer
Edit:
I should point out that the .Net language I am referring to is VB.NET rather then C# but the way they define variables is different.
Yep.
Re: Not a lot of people know this (or do they?)
You can't 'get away with" your VB6 statement if you want all of them to be integers.
In VB6, When you use:
Dim int1, int2, int3 As Integer, only int3 is declared an integer, the other two are Variants.
Re: Not a lot of people know this (or do they?)
Quote:
Originally Posted by
SamOscarBrown
You can't 'get away with" your VB6 statement if you want all of them to be integers.
In VB6, When you use:
Dim int1, int2, int3 As Integer, only int3 is declared an integer, the other two are Variants.
What he was saying is that you can in VB.Net and that is correct, VB.Net will dim them all as Integers when written that way. VB6 will not
Re: Not a lot of people know this (or do they?)
You can always save on typing and use
Dim Int1%, Int2%,int3%
Re: Not a lot of people know this (or do they?)
Yeah....misread....darn eyes! He said same as I.