ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Question about dim statement (https://www.excelbanter.com/excel-programming/334888-question-about-dim-statement.html)

Gregg[_4_]

Question about dim statement
 
While debugging my code today, I was surprised to find the fix was to
change this dim statement...
Dim lNumber, lValue, lWhatever as Long

to this...
Dim lNumber as Long
Dim lValue as Long
Dim lWhatever as Long

The value would not work correctly unless I separated out the dim
statement. Once I did that, it worked flawlessly and I set it back and
forth to be sure.

I've never encountered that before. I've always been able to place
multiple items in a dim statement. Is Long different? Can someone
enlighten me on this?

Thanks.


Dave Peterson

Question about dim statement
 
When you do this:
Dim lNumber, lValue, lWhatever as Long
it's seen as:
Dim lNumber as Variant, lValue as Variant, lWhatever as Long

But I'm not sure what you had to fix to make it work. But I would suspect it
had something to do with the way a Long is initialized (as 0) vs a Variant (as
Empty).

But
if lnumber = 0 then
msgbox "hi"
end if

worked ok for me in xl2003. (It saw that I wanted to compare numbers and Empty
was treated as zero.)

Dim lNumber as long, lValue as long, lWhatever as long

if you wanted just one line



Gregg wrote:

While debugging my code today, I was surprised to find the fix was to
change this dim statement...
Dim lNumber, lValue, lWhatever as Long

to this...
Dim lNumber as Long
Dim lValue as Long
Dim lWhatever as Long

The value would not work correctly unless I separated out the dim
statement. Once I did that, it worked flawlessly and I set it back and
forth to be sure.

I've never encountered that before. I've always been able to place
multiple items in a dim statement. Is Long different? Can someone
enlighten me on this?

Thanks.


--

Dave Peterson

Norman Jones

Question about dim statement
 
Hi Gregg,

Dim lNumber, lValue, lWhatever as Long


the above line does *not* dim each of the three variables as long; INumber
is implicitly dimmed as variant and Ivalue is similarly dimmed as variant.
IWhatever is, howver, explicitly dimmed as long.

This has nothing to do with single or multiline declaration which is
essentially a question of style. So, to explicitly dim the three variables
in a single line declaration, you would write it thus::

Dim lNumber as Long, lValue As Long , lWhatever as Long

Normally, using the implicit default variant may be less efficient but
whether it can engender subtle problems depends on the code you are using.
Personally, I have rarely encountered such a problem. But then again, I
endeavour always to explicitly declare variables.



---
Regards,
Norman



"Gregg" wrote in message
oups.com...
While debugging my code today, I was surprised to find the fix was to
change this dim statement...
Dim lNumber, lValue, lWhatever as Long

to this...
Dim lNumber as Long
Dim lValue as Long
Dim lWhatever as Long

The value would not work correctly unless I separated out the dim
statement. Once I did that, it worked flawlessly and I set it back and
forth to be sure.

I've never encountered that before. I've always been able to place
multiple items in a dim statement. Is Long different? Can someone
enlighten me on this?

Thanks.




keepITcool

Question about dim statement
 


Dim x,y as long '=y as long , x as variant.
Dim x,y as string '=y as string, x as variant
Dim x as long,y as long '=correct "combined" syntax.

The errors will start when you assign a variable to another
and 1 is not a variant... Or when you use a function
where either return value or arguments are "typed".


dim s as string, i as integer, l as long, v as variant
'or dim s$, i%, l&, v

v=s 'ok since variant will accept other types
v=i 'ok
v=l 'ok
but
s=v (may give probems depending on the content of v
i=s Will give problems
l=i will give problems...

somae goes for functions/procedure arguments

Function HowLong(s as string) as long
Howlong = len(s)
end function

i=howlong(s) will give a problem as Howlong returns a Long.
l=howlong(v) will also give a problem as Howlong expects a string.

Function HowWide(byval s as string) as long
HowWide=len(s)
end function

l=howwide(v) will now work since the argument s is passed byval
but
i=howwide(v) will NOT work as howwide returns a long.


so.. you could stick to using variants. BUT variant carry a lot of
"fat" and are much slower than using correct types.

also when you use variants for objects you will not have intellisense,
and you may get run time errors that otherwise could be picked up
when compiling.

this stuff is explained in all VB(A) books.
in the first few boring chapters that many people skip cuz they want
to the more exciting stuff....

VBA performance can be very sensitive to correct data typing.
(you may not notice when you run a function 1x.. but you WILL notice
when a function is called 100000 times..)


--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Gregg wrote :

While debugging my code today, I was surprised to find the fix was to
change this dim statement...
Dim lNumber, lValue, lWhatever as Long

to this...
Dim lNumber as Long
Dim lValue as Long
Dim lWhatever as Long

The value would not work correctly unless I separated out the dim
statement. Once I did that, it worked flawlessly and I set it back and
forth to be sure.

I've never encountered that before. I've always been able to place
multiple items in a dim statement. Is Long different? Can someone
enlighten me on this?

Thanks.


Gregg[_4_]

Question about dim statement
 
keepITcool wrote:
The errors will start when you assign a variable to another
and 1 is not a variant... Or when you use a function
where either return value or arguments are "typed".


this stuff is explained in all VB(A) books.
in the first few boring chapters that many people skip cuz they want
to the more exciting stuff....


I totally misread that part in the chapter! Looking back at my notes
from when I first learned multiple line dim statements, it specifically
warns NOT to dim as I did. <jeesh Somehow, I misread and did exactly
that.

Oddly enough, since using that incorrect method for about a year now, I
have not ran into trouble until now. Lucky, I guess.

I'll be sure, however, to go back on all my programming and change the
dim statements to the correct method.

Thank you, Dave, Norman, and KeepItCool for your time and patience in
explaining this.



All times are GMT +1. The time now is 09:29 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com