Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Boolean Declaration
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 |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Boolean Declaration
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) "Shatin" wrote in message ... 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 |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Boolean Declaration
Many thanks Chip. Your answer is most helpful.
"Chip Pearson" wrote in message ... 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) "Shatin" wrote in message ... 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 |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Boolean Declaration
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 ...) "Shatin" wrote: 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 |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Boolean Declaration
Many thanks for your reply Patrick. I must brush up my knowledge of class
modules. "Patrick Molloy" wrote in message ... 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 ...) "Shatin" wrote: 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 |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
if declaration | Excel Programming | |||
Declaration problem | Excel Programming | |||
Declaration name | Excel Programming | |||
which declaration to use | Excel Programming | |||
Declaration? | Excel Programming |