Enums as Global Constants

winshent

Registered User.
Local time
Today, 19:20
Joined
Mar 3, 2008
Messages
162
Is there a reason why I cannot create a Global Const of type Enum ?

Code:
Public Enum EnvironmentStatus
  
  DevelopmentEnvironment
  TestEnvironment
  ProductionEnvironment

End Enum

[B]Global Const g_EnvironmentStatus As EnvironmentStatus = DevelopmentEnvironment[/B]

Sub ConnectTables()

  Dim db As DAO.Database, t As DAO.TableDef
  Set db = CurrentDb
  
  For Each t In db.TableDefs
       
    If InStr(1, t.Connect, "CB.mdb") <> 0 Then
    
      Select Case g_EnvironmentStatus
    
        Case DevelopmentEnvironment
          t.Connect = ";DATABASE=P:\DatabaseDevelopment\CB.mdb"
        Case ProductionEnvironment
          t.Connect = ";DATABASE=P:\CB_Live\CB.mdb"
        Case TestEnvironment
          t.Connect = ";DATABASE=P:\DatabaseTesting\CB.mdb"
          
      End Select
          
      t.RefreshLink
      
    End If
  
  Next
  
End Sub
 
offhand, I would think so, if it compiles OK. Have you tried it?
 
This little demo does what you are planning but in a different way. This may help you.
 
I can confirm that VBA does not like creating a constant of type enum. Never tried to do that before so I have no idea why it'd not accept enum as a possible data type for a constant other than the speculation that constant may be meant to hold primitive data types only. This also is an error:

Code:
Const rs as DAO.TableDef = Null

So constants can't have objects, which makes sense. So, enum may be a case that get lumped with objects and thus doesn't compile.

You can at least work around this by setting the data type to long, which is the default data type of enum anyway and set it to the appropriate enum value.
 
Cheers guys

decided to go with this..

Code:
Public Enum EnvironmentStatus
  
  DevelopmentEnvironment = 1
  TestEnvironment = 2
  ProductionEnvironment = 3

End Enum

Global Const g_EnvironmentStatus As integer = DevelopmentEnvironment

Sub ConnectTables()

  Dim db As DAO.Database, t As DAO.TableDef
  Set db = CurrentDb
  
  For Each t In db.TableDefs
       
    If InStr(1, t.Connect, "CB.mdb") <> 0 Then
    
      Select Case g_EnvironmentStatus
    
        Case DevelopmentEnvironment
          t.Connect = ";DATABASE=P:\DatabaseDevelopment\CB.mdb"
        Case ProductionEnvironment
          t.Connect = ";DATABASE=P:\CB_Live\CB.mdb"
        Case TestEnvironment
          t.Connect = ";DATABASE=P:\DatabaseTesting\CB.mdb"
          
      End Select
          
      t.RefreshLink
      
    End If
  
  Next
  
End Sub
 
If you had placed the Enum in a Standard Module (not a Form or Report Module) it would have been global.

Code:
Global Const g_EnvironmentStatus As integer = DevelopmentEnvironment
Is there any reason why you're declaring another Const variable (albeit with a different name) when an Enum is already a Const?
 
I believe he meant to instantiate a variable containing a particular value of enum. I also guess that he wanted it to be const because he didn't want to change the value at the runtime only at development/deployment.
 
Ah yes I see the difference between the declarations now. In the new working code, the value is the same and an Enum is a set of constant values, so I don't get why the second constant declaration. Why not simply use the DevelopmentEnvironment variable and write a comment to indicate that it's actually supposed to mean EnvironmentStatus within that section of the code?

By the way winshent, you can have the Enum part like this:
Code:
Public Enum EnvironmentStatus

  DevelopmentEnvironment = [COLOR=Red]1[/COLOR]
[COLOR=Red]  TestEnvironment
  ProductionEnvironment[/COLOR]

End Enum
The values for the other two variables will automatically increment, i.e. TestEnvironment will become 2 and ProductionEnvironment will become 3.
 
Ah yes I see the difference between the declarations now. In the new working code, the value is the same and an Enum is a set of constant values, so I don't get why the second constant declaration. Why not simply use the DevelopmentEnvironment variable and write a comment to indicate that it's actually supposed to mean EnvironmentStatus within that section of the code?

By the way winshent, you can have the Enum part like this:
Code:
Public Enum EnvironmentStatus

  DevelopmentEnvironment = [COLOR=Red]1[/COLOR]
[COLOR=Red]  TestEnvironment
  ProductionEnvironment[/COLOR]

End Enum

Yeah, essentially I just wanted a switch for managing connections when deploying and I may also use it to relay environment info to the on the main menu..



The values for the other two variables will automatically increment, i.e. TestEnvironment will become 2 and ProductionEnvironment will become 3.

Didn't know that.. thanks
 
Alrighty. That's a long name for a switch though :)

I would have gone for something like this:
Code:
Public Enum EnviroStatus

  Dev = 1
  Test
  Prod

End Enum
Since you already have Environment as the name of the Enum it just seems like you're repeating the word.

I would then call it like this:
Code:
Public Const g_EnvironmentStatus As integer = [COLOR=Red]EnviroStatus.Dev[/COLOR]
I've fully qualified the reference so it makes sense not to have the word Environment (or Enviro) within the enums. It's adviseable to fully qualify the reference to the enum so that you know what type of variable you're referencing and allows you declare a variable with the name Dev for other use.

Oh yeah, Global is an old keyword. You're better off using Public.
 
As far as I know you can't assign a variable to a constant (otherwise it wouldn't be constant). If you need a variable for it, you might consider creating it as a property instead.
 
Let Constant to Constant - is fine

Let Constant to Non-Constant - not allowed.
 
Also, some expressions are OK.

e.g.

Code:
Const Sum As Long = 1 + 1
Const A As Long = 1
Const B As Long = 2
Const AnotherSum AS Long = A + B

I frequently do something similar to support multiple assignments, typically with bit flags:
Code:
Public Enum MyFlags
 A = 1
 B = 2
 AllFlags = A Or B 'Set both A and B at same time
End Enum

Of course, an expression like "=Now()" would be invalid.
 

Users who are viewing this thread

Back
Top Bottom