PC Review


Reply
Thread Tools Rate Thread

Boolean Declaration

 
 
Shatin
Guest
Posts: n/a
 
      11th Mar 2008
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 With Quote
 
 
 
 
Chip Pearson
Guest
Posts: n/a
 
      11th Mar 2008
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" <lk1439-(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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 With Quote
 
Shatin
Guest
Posts: n/a
 
      11th Mar 2008
Many thanks Chip. Your answer is most helpful.


"Chip Pearson" <(E-Mail Removed)> wrote in message
news:A5420D3B-217B-4608-96F2-(E-Mail Removed)...
> 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" <lk1439-(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> 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 With Quote
 
Patrick Molloy
Guest
Posts: n/a
 
      12th Mar 2008
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 With Quote
 
Shatin
Guest
Posts: n/a
 
      12th Mar 2008
Many thanks for your reply Patrick. I must brush up my knowledge of class
modules.


"Patrick Molloy" <(E-Mail Removed)> wrote in message
news:CD0E02DC-F1F8-4FD6-93BE-(E-Mail Removed)...
> 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 With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple Boolean Declaration =?Utf-8?B?Um9k?= Microsoft Access VBA Modules 1 13th Jun 2006 03:32 PM
NotSupportedException Type.GetType (String, Boolean, Boolean) jonfroehlich Microsoft Dot NET Compact Framework 1 20th Apr 2006 02:44 PM
if declaration =?Utf-8?B?YXNodzE5ODQ=?= Microsoft Excel Programming 2 30th Jan 2006 07:35 PM
No boolean object in VB.NET? How to check if a boolean has been explicitely defined then? Lucas Tam Microsoft VB .NET 10 12th Jun 2005 09:29 PM
event-declaration vs interface-event-declaration Alex Sedow Microsoft C# .NET 1 26th Dec 2004 11:03 PM


Features
 

Advertising
 

Newsgroups
 


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