non numeric constants? 
Author Message
 non numeric constants?

I'm just beginning to get to grips with class modules and enumerated
constants

       Public Enum Pallette

         eRed = &H908070&
        e Blue = &HFF901E&
        e Green = &H7FFF00&

        End Enum

and then use the enumeration to set a value

        Dim Colouring as Pallette

        Colouring =eRed

How do you do a similar thing for string variables such as country codes
e.g.. US, GB, D, etc.  Enum only supports numeric values?

Can you use a class collection object to produce the same effect for non
numeric constants which would allow the following?

        Dim Nationality as Abbreviations

        Nationality=eGB

 I want to be able to use the string value of the constant which of course
is "GB" within my program

Apologies if this is the wrong group but not sure which newsgroup for class
related issues



Mon, 21 May 2001 03:00:00 GMT  
 non numeric constants?
Enumerated constants can only be numeric.

Glenn Eastlack
Broadreach Consulting
http://www.broadreachconsulting.com

Quote:

> I'm just beginning to get to grips with class modules and enumerated
> constants

>        Public Enum Pallette

>          eRed = &H908070&
>         e Blue = &HFF901E&
>         e Green = &H7FFF00&

>         End Enum

> and then use the enumeration to set a value

>         Dim Colouring as Pallette

>         Colouring =eRed

> How do you do a similar thing for string variables such as country codes
> e.g.. US, GB, D, etc.  Enum only supports numeric values?

> Can you use a class collection object to produce the same effect for non
> numeric constants which would allow the following?

>         Dim Nationality as Abbreviations

>         Nationality=eGB

>  I want to be able to use the string value of the constant which of course
> is "GB" within my program

> Apologies if this is the wrong group but not sure which newsgroup for class
> related issues



Mon, 21 May 2001 03:00:00 GMT  
 non numeric constants?

Quote:
>> How do you do a similar thing for string variables such as country codes
>> e.g.. US, GB, D, etc.  Enum only supports numeric values?

>> Can you use a class collection object to produce the same effect for non
>> numeric constants which would allow the following?

>>         Dim Nationality as Abbreviations

>>         Nationality=eGB

>>  I want to be able to use the string value of the constant which of
course
>> is "GB" within my program

>Enumerated constants can only be numeric.

To expand further on this...

A VB enumeration can only be numeric...and in fact, it can only be a long.
If you try to enum something and set it to 4.50, VB will probably round it
to 5.

A common way to get public constants of any data type from class modules is
to use properties.  For instance, if you want to have GB_abbrev as a public
string constant equal to "GB", consider using

Public Property Get GB_abbrev() as String
    GB_abbrev = "GB"
End Property

You could fake a string enumeration by creating a class called Abbreviation
and having all of your abbreviations listed as property gets.  The syntax
might be a little quirky because (depending on the instancing mode of the
class) you'd probably have to create a new instance of the class and you
wouldn't be able to reference it quite the same.

If you did that, your example might look something like:

Dim Nationality as String
Dim enumNationalities as Abbreviation
Set enumNationalities = New Abbreviation

Nationality = enumNationalities.GB

Set enumNationalities = Nothing

If I were a user of your API and this is how it was exported, I'd probably
find it to be cumbersome if you did this.  If you are just wanting to
enforce strong typing in your code internally, however, this isn't a bad way
to do it.  (There again, however, if you make good decisions about
consistent variable/constant naming it would be obvious to you as the
developer without having to use such a construct as I've described above)

Jason White
Staff Software Engineer
National Instruments

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Check out ComponentWorks, a collection of ActiveX controls from
National Instruments for PC Measurement and Automation.  Information
and free demo at http://www.natinst.com/cworks/



Mon, 21 May 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. enumerating non numeric constants?

2. Routine to transform a Numeric into a non Numeric

3. a non-numeric character was found where a numeric was expected

4. Trap Keypress to igmore non numeric entries

5. Need Regular Expression to strip non-numeric characters from string

6. not a valid month / a non-numeric character was found

7. Non-numeric characters in integer variable

8. how do you test for non numeric data?

9. using change event to trap non numeric chrs

10. Q:error checking for non-numeric input

11. strip non numeric characters from string

12. Textbox which won't allow non-numeric

 

 
Powered by phpBB® Forum Software