Declaring a variable

M

mcescher

Hi All,

I was looking at some code on Dev Ashish's site, and one function was
declared differently than I had seen before.

Function Lpad(MyValue$, MyPadCharacter$, MyPaddedLength%)
<<some vba code here>>
End Function

I understand the code, but I'm wondering about the declaration.

Is "MyValue$" the same as "MyValue AS String"?

Does the % then specify an integer?

Is there a list of other character declarations?

Does that work with a regular dim statement?
i.e. DIM strMyString$, intMyInteger%

Thanks so much,
Chris M.
 
D

Douglas J. Steele

It's an ancient way of declaring variables, so ancient that it doesn't
include all data types.

There are 5 type-declaration symbols that I'm aware of:

! - Single
# - Double
$ - String
% - Integer
& - Long

(no Date, Boolean, Currency or Double.)

Dim strMyString$, intMyInteger%

is identical to

Dim strMyString As String, intMyInteger As Integer
 
D

Dirk Goldgar

mcescher said:
Hi All,

I was looking at some code on Dev Ashish's site, and one function was
declared differently than I had seen before.

Function Lpad(MyValue$, MyPadCharacter$, MyPaddedLength%)
<<some vba code here>>
End Function

I understand the code, but I'm wondering about the declaration.

Is "MyValue$" the same as "MyValue AS String"?

Does the % then specify an integer?

Yes. It's a "type declaration character".
Is there a list of other character declarations?

Here's one:

http://support.microsoft.com/kb/191713
Does that work with a regular dim statement?
i.e. DIM strMyString$, intMyInteger%

Yes. But it's a discouraged practice.

One place where the type declaration characters come in handy -- when you're
programming for absolute maximum run-time efficiency -- is in the
specification of numeric literals, where you want to avoid any unnecessary
conversions. For example

Dim sngMyVar As Single
' ...
sngMyVar = 1!

Observe this in the Immediate Window:

?typename(1)
Integer
?typename(1.0)
Double
?typename(1!)
Single
?typename(1@)
Currency
 
D

Dirk Goldgar

Douglas J. Steele said:
It's an ancient way of declaring variables, so ancient that it doesn't
include all data types.

There are 5 type-declaration symbols that I'm aware of:

! - Single
# - Double
$ - String
% - Integer
& - Long

(no Date, Boolean, Currency or Double.)


There's Currency: @
 
M

mcescher

It's an ancient way of declaring variables, so ancient that it doesn't
include all data types.

There are 5 type-declaration symbols that I'm aware of:

!  - Single
# - Double
$ - String
% - Integer
& - Long

(no Date, Boolean, Currency or Double.)

Dim strMyString$, intMyInteger%

is identical to

Dim strMyString As String, intMyInteger As Integer

--
Doug Steele, Microsoft Access MVPhttp://I.Am/DougSteele
(no e-mails, please!)














- Show quoted text -

Thank you so much for your quick response. Should this be avoided?

Thanks again,
Chris M.
 
M

mcescher

Hmm. Don't know how I missed it. Thanks, Dirk.

--
Doug Steele, Microsoft Access MVPhttp://I.Am/DougSteele
(no private e-mails, please)- Hide quoted text -

- Show quoted text -

Thanks to all who replied.

Chris M.
 
D

Dirk Goldgar

Linq Adams via AccessMonster.com said:
Hey, who you calling "ancient?" I grew up writing code like that, back
when
"basic" wasn't "visual," hard drives were 20 mbs and Bill's operating
systems
actually came with an operator's manual!


Wow, you're a newbie, aren't you?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top