Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 56
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 56
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,298
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 56
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
if declaration ashw1984 Excel Programming 2 January 30th 06 07:35 PM
Declaration problem davidm Excel Programming 6 August 25th 05 05:03 AM
Declaration name Robert Hargreaves[_2_] Excel Programming 4 June 6th 05 04:48 PM
which declaration to use Peer Excel Programming 3 August 2nd 04 03:17 PM
Declaration? TJF[_2_] Excel Programming 5 December 18th 03 03:26 PM


All times are GMT +1. The time now is 02:09 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"