Word Document Properties


Listing 4-1 Examples of Variable and Constant Declarations with Scope



Download 8,82 Mb.
Pdf ko'rish
bet78/207
Sana16.03.2022
Hajmi8,82 Mb.
#496217
1   ...   74   75   76   77   78   79   80   81   ...   207
Bog'liq
VBA For Dummies 5th Ed.pdf ( PDFDrive )

Listing 4-1
Examples of Variable and Constant Declarations with Scope
Option Explicit
‘ This variable is visible to other modules.
Public MyPublicVariable As String
‘ This variable is visible only to this module.
Private MyPrivateVariable As String
‘ Using Dim is the same as making the variable private.
Dim MyDimVariable As String
‘ A constant is only used for conditional compilation.
#Const MyConditionalConstant = “Hello”
‘ This constant is visible to other modules.
Public Const MyPublicConstant = “Hello”
‘ This constant is visible only to this module.
Private Const MyPrivateConstant = “Hello”
Public Sub DataDeclarations()
80
Part II: Learning the Ropes 
09_046500 ch04.qxp 12/5/06 5:54 PM Page 80


‘ Only this Sub can see this variable.
Dim MyDimSubVariable As String
‘ Only this Sub can see this constant.
Const MySubConstant = “Hello”
End Sub
Begin by looking at the variables in Listing 4-1. The example includes three
declarations, along with their associated comments. Always include com-
ments with your declarations so that you know what purpose the variable
serves. The variable declarations rely on 
Public
and 
Private
for scope
as well as on a data type keyword following the variable name to define the
data type. All the variables in this example are strings (or text), but VBA 
supports many other data types. Using the 
Dim
keyword makes a variable 
private. However, actually using the word 
Private
is clearer, so that’s what
you should use in your code.
The next three declarations in Listing 4-1 are for constants. Notice that the first
constant declaration is for conditional compilation. This kind of declaration
always uses the 
#Const
keyword. You can use this declaration in place of the
Conditional Compilation Arguments field entry (see the “Adding conditional
compilation” section of Chapter 3 for details). You can’t define this type of con-
stant as private or public because VBA hides the value from other modules.
The other two kinds of constant declaration do rely on 
Public
or 
Private
for scope. Notice that you do include the keyword 
Const
to mark the value
as a constant. The constant has a name and a value assigned to it. However,
notice that it doesn’t have a data type because you can’t assign a data type
to a constant. VBA stores constants as a series of bits by using the type of
the information that you provide. Because you can never change the value 
of a constant, the question of data type isn’t important. The fact that a con-
stant doesn’t have a data type is one reason that it improves application per-
formance and is more efficient to store.
The next section of Listing 4-1 contains a 
Sub
that includes both a constant
and a variable declaration. Variables or constants defined within a 
Sub
or
Function
are private to that 
Sub
or 
Function
. Consequently, VBA requires
that you use the 
Dim
keyword in this case. Notice that the constant still
includes the 
Const
keyword but lacks a 
Private
or 
Public
keyword for 
the same reason.
Knowing which storage type to use
At first, it might seem like you should always use variables so that you can
always access the data and change it. Variables do provide flexibility that
81

Download 8,82 Mb.

Do'stlaringiz bilan baham:
1   ...   74   75   76   77   78   79   80   81   ...   207




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish