Boolean Declaration

S

Shatin

Hi All,

Say I have a variable called Height and I want to declare it as a Boolean
variable with possible values "Tall" or "Short" rather than True or False.
How should I declare it?

TIA
 
C

Chip Pearson

Basically, you can't do that exactly as you want. You can declare Height as
Boolean, and then have constants TALL and SHORT equal to TRUE and FALSE.
E.g.,

Dim bHeight As Boolean
Const TALL = True
Const SHORT = False

Another way is to use an Enum variable type, which is really a Long type.
The enum must be declared before and outside of any Sub or Function
procedure.

Enum Height
TALL = True
SHORT = False
End Enum

Then, declare a variable of this type.

Dim H As Height

and give it a value.

H = TALL
' Or
H = SHORT

Note that declaring a variable as an enum type does NOT prevent any other
value from being assigned to that variable. For example, it is perfectly
legal to assign any valid Long value to the H variable, even if that value
is neither TALL or SHORT.


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 
S

Shatin

Many thanks Chip. Your answer is most helpful.


Chip Pearson said:
Basically, you can't do that exactly as you want. You can declare Height
as Boolean, and then have constants TALL and SHORT equal to TRUE and
FALSE. E.g.,

Dim bHeight As Boolean
Const TALL = True
Const SHORT = False

Another way is to use an Enum variable type, which is really a Long type.
The enum must be declared before and outside of any Sub or Function
procedure.

Enum Height
TALL = True
SHORT = False
End Enum

Then, declare a variable of this type.

Dim H As Height

and give it a value.

H = TALL
' Or
H = SHORT

Note that declaring a variable as an enum type does NOT prevent any other
value from being assigned to that variable. For example, it is perfectly
legal to assign any valid Long value to the H variable, even if that value
is neither TALL or SHORT.


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 
P

Patrick Molloy

as an alternative to Chip's answer, you might find a CLASS object useful

the following demonstrates...

' STANDARD MODULE
Option Explicit
Sub Main()
Dim MyHeight As cHEIGHT
Set MyHeight = New cHEIGHT
MyHeight.TALL False
MsgBox MyHeight.HEIGHT
MyHeight.TALL True
MsgBox MyHeight.HEIGHT
End Sub

' CLASS MODULE: cHEIGHT
Option Explicit
Private m_height As Boolean
Public Function HEIGHT() As String
If m_height Then
HEIGHT = "TALL"
Else
HEIGHT = "SHORT"
End If
End Function
Sub TALL(newHeight As Boolean)
m_height = newHeight
End Sub


regards
Patrick
(once an MVP, always er ...)
 

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