Usage of Deftype statements

MrKowz

Well-known Member
Joined
Jun 30, 2008
Messages
6,653
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Through my "research" at work, I came across the Deftype statements that can be used in VBA, and was wondering if it is ever useful, especially when Option Explicit it used (as it seems almost all of us prefer to use Option Explicit).
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Perhaps more appropriately, is it ever used?

Sounds like a good idea for some things but it also sounds like you might need to make sure that you comply to a naming convention for your variables.

Obviously you can create your own and simply override Deftype using 'standard' Dim

Actually I don't quite see much advantage, it could even lead to sloppy programmining.

You might end up relying on Deftype for typing variables.

Wonder how it works in conjunction with Option Explicit?

Also wondering why there's no example in online help, found one in offline help though.
Rich (BB code):
' Variable names beginning with A through K default to Integer.DefInt A-K' Variable names beginning with L through Z default to String.DefStr L-ZCalcVar = 4    ' Initialize Integer.StringVar = "Hello there"    ' Initialize String.AnyVar = "Hello"     ' Causes "Type mismatch" error.Dim Calc As Double    ' Explicitly set the type to Double.Calc = 2.3455    ' Assign a Double.' Deftype statements also apply to function procedures.CalcNum = ATestFunction(4)    ' Call user-defined function.' ATestFunction function procedure definition.Function ATestFunction(INumber)    ATestFunction = INumber * 2    ' Return value is an integer.End Function

Another thing, would you really want all variables beginning with a certain letter(s) to default to a particular type.
 
Upvote 0
quoting Paul Lomax:
"Unless you are using a strict coding convention, this way of declaring variables is a way to become extremely confused with the data types used in your application in as short a time as possible."
-- VB and VBA in a Nutshell, 1998

His reference says that if you have Option Explicit set, you must still declare the variables, but if the dim doesn't specify a type then the Def statement determines the type:
Code:
Option Explicit
Defstr s 

Dim strMyVar1
strMyVar1 is a String, not a Variant.

ξ

Note: I never heard of this before ... and now I shall forget about it. ;) It's seems an uncommon convention that could create much confusion, especially if someone else inherits your code.
 
Upvote 0

Forum statistics

Threads
1,225,535
Messages
6,185,501
Members
453,298
Latest member
Adam1258

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top