|
-
Nov 2nd, 2013, 07:11 AM
#1
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 ?
-
Nov 2nd, 2013, 07:20 AM
#2
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.
-
Nov 2nd, 2013, 07:24 AM
#3
Re: Not a lot of people know this (or do they?)
In addition, I see this statement:
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.
-
Nov 2nd, 2013, 08:45 AM
#4
Re: Not a lot of people know this (or do they?)
News to me. Can't see me adopting this, though...
If you don't know where you're going, any road will take you there...
My VB6 love-children: Vee-Hive and Vee-Launcher
-
Nov 2nd, 2013, 09:03 AM
#5
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.
-
Nov 2nd, 2013, 10:32 AM
#6
Frenzied Member
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
-
Nov 2nd, 2013, 11:21 AM
#7
Re: Not a lot of people know this (or do they?)
Knew it but never thought it was worth my while to use it
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Nov 2nd, 2013, 11:25 AM
#8
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.
-
Nov 2nd, 2013, 11:49 AM
#9
Re: Not a lot of people know this (or do they?)
Used it since GW-Basic (or before?)
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.
Last edited by jggtz; Nov 2nd, 2013 at 11:54 AM.
JG
... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...
-
Nov 2nd, 2013, 01:22 PM
#10
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
-
Nov 2nd, 2013, 01:39 PM
#11
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.
 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.
-
Nov 2nd, 2013, 02:52 PM
#12
Re: Not a lot of people know this (or do they?)
In the old days I always used a lot of $ and # notations
-
Nov 2nd, 2013, 02:59 PM
#13
Re: Not a lot of people know this (or do they?)
 Originally Posted by DataMiser
In the old days I always used a lot of $ and # notations 
Yea.
Good times....
-
Nov 2nd, 2013, 04:30 PM
#14
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
....
-
Nov 2nd, 2013, 04:56 PM
#15
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...
-
Nov 2nd, 2013, 05:32 PM
#16
Re: Not a lot of people know this (or do they?)
 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.
-
Nov 2nd, 2013, 07:52 PM
#17
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.
Last edited by Nightwalker83; Nov 2nd, 2013 at 07:54 PM.
Reason: Adding more!
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Nov 2nd, 2013, 09:52 PM
#18
Re: Not a lot of people know this (or do they?)
 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.
-
Nov 3rd, 2013, 08:25 AM
#19
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.
-
Nov 3rd, 2013, 08:31 AM
#20
Re: Not a lot of people know this (or do they?)
 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
-
Nov 3rd, 2013, 09:15 AM
#21
Addicted Member
Re: Not a lot of people know this (or do they?)
You can always save on typing and use
Dim Int1%, Int2%,int3%
-
Nov 3rd, 2013, 05:22 PM
#22
Re: Not a lot of people know this (or do they?)
Yeah....misread....darn eyes! He said same as I.
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
|