Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
When DIM statements don't work
I have been using VBA for several years. This may be a
really stupid question, but I have recently encountered a funny phenomena. I use have always assumed that you could "stack" variables of the same type in a DIM statement. For example Dim A, B, C as Integer and Dim A as Integer Dim B as Intger Dim C as Integer would have the same effect, i.e., all variables would be of type Integer. I now find that this appears to not be true and that some variables in a multi-variable Dim statement are considered Variants. What is supposed to happen with a multi-variable Dim statement? If only one of the variables is acutallly types as I want, are the rest of type Variant? What is the value of allowing multiple variables if they are not typed the same? Thanks |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
When DIM statements don't work
What you have discovered is the case and has been discussed in recent weeks.
The two examples you quote do not have the same effect. Only c is defined as an integer in the first case. An extract from the VBA Help file for Dim: Dim Statement Example This example shows the Dim statement used to declare variables. It also shows the Dim statement used to declare arrays. The default lower bound for array subscripts is 0 and can be overridden at the module level using the Option Base statement. ' AnyValue and MyValue are declared as Variant by default with values ' set to Empty. Dim AnyValue, MyValue ' Explicitly declare a variable of type Integer. Dim Number As Integer ' Multiple declarations on a single line. AnotherVar is of type Variant ' because its type is omitted. Dim AnotherVar, Choice As Boolean, BirthDate As Date Regards Trevor "MWE" wrote in message ... I have been using VBA for several years. This may be a really stupid question, but I have recently encountered a funny phenomena. I use have always assumed that you could "stack" variables of the same type in a DIM statement. For example Dim A, B, C as Integer and Dim A as Integer Dim B as Intger Dim C as Integer would have the same effect, i.e., all variables would be of type Integer. I now find that this appears to not be true and that some variables in a multi-variable Dim statement are considered Variants. What is supposed to happen with a multi-variable Dim statement? If only one of the variables is acutallly types as I want, are the rest of type Variant? What is the value of allowing multiple variables if they are not typed the same? Thanks |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
When DIM statements don't work
You're mostly right. Variant is the default type for a variable and
that is what you'll get if you don't specify otherwise. When you do; Dim a, b, c, d, e As Integer Only "e" is of type "Integer", the rest are variants. You could do this: Dim a As Integer, b As Integer, c As Integer, d, e As Integer "a" "b" "c" and "e" will be Integers and "d" will be a Variant. - Pikus --- Message posted from http://www.ExcelForum.com/ |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
When DIM statements don't work
Many thanks to both Trevor and Pikus for their help. Two
things still bother me: 1. what is the use of a Dim statement if the default type is what you get anyway? 2. why would the originator of VB not follow the conventions of other programming languages and allow multi variables to be typed the same in the same line? |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
When DIM statements don't work
Each programming language has its on syntax and conventions. If they were
all the same, there would be no reason to have more than one language. Conventions are determined by the author of the language. A better question is why would someone try to use a programming language without learning the syntax and conventions. Use of Dim is not required. So if you want the default type, then you don't need to use it. However, the usual recommendation is to put in Option Explict at the top of the module. This forces every variable to be declared with a dim statement. It is useful because if you mispell you variable, the debugger will flag it as undeclared. Otherwise, you might miss that you have created a subtle error. -- Regards, Tom Ogilvy wrote in message ... Many thanks to both Trevor and Pikus for their help. Two things still bother me: 1. what is the use of a Dim statement if the default type is what you get anyway? 2. why would the originator of VB not follow the conventions of other programming languages and allow multi variables to be typed the same in the same line? |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
IF statements with * character doesn't work | Excel Worksheet Functions | |||
IF Statements (Mutliple Statements) | Excel Worksheet Functions | |||
Getting multiple IF statements to work | Excel Discussion (Misc queries) | |||
I'm desperate I posted earlier about "If" statements that don't work but are correct. Now... | Excel Worksheet Functions | |||
Cant get totals to work with IF statements | Excel Worksheet Functions |