Results 1 to 22 of 22

Thread: Not a lot of people know this (or do they?)

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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 ?

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,624

    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.

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,624

    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.

  4. #4
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,272

    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

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    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.

  6. #6
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    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

  7. #7
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  8. #8
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    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.

  9. #9
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    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 ...

  10. #10
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    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

  11. #11
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    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 View Post
    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  12. #12
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Not a lot of people know this (or do they?)

    In the old days I always used a lot of $ and # notations

  13. #13
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Not a lot of people know this (or do they?)

    Quote Originally Posted by DataMiser View Post
    In the old days I always used a lot of $ and # notations
    Yea.
    Code:
    Dim A$
    Good times....
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  14. #14
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    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
    ....

  15. #15
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    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...

  16. #16
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Not a lot of people know this (or do they?)

    Quote Originally Posted by Magic Ink View Post
    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.

  17. #17
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    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

  18. #18
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Not a lot of people know this (or do they?)

    Quote Originally Posted by Nightwalker83 View Post
    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  19. #19
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,624

    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.

  20. #20
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Not a lot of people know this (or do they?)

    Quote Originally Posted by SamOscarBrown View Post
    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

  21. #21
    Addicted Member
    Join Date
    Jul 2006
    Posts
    159

    Re: Not a lot of people know this (or do they?)

    You can always save on typing and use

    Dim Int1%, Int2%,int3%

  22. #22
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,624

    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
  •  



Click Here to Expand Forum to Full Width