Home > Software engineering >  Circular Dependency Error when using Enumerations
Circular Dependency Error when using Enumerations

Time:02-02

I want to assign certain time weightage values to the variables of a data set. So I am trying to use Enumerations/Constants so to improve my code quality and make it easy to maintain. But on compiling my project, it throws in a Error Snap

CodePudding user response:

  • The error message is incorrect: your code does not have any circular references.
    • This is more of a bug in the VBA interpreter: your code is still incorrect and invalid, but VBA is showing the wrong error message.
    • Given that VBA remains effectively frozen-in-time since 2002 (excepting 64-bit support in 2007), so don't expect any fixes, let alone any enhancements, ever (Though MS Office's COM automation tooling is slowly shifting to JavaScript (maybe Python? please?).
  • The actual problem with your code is threefold:
    • You cannot use a Function to initialize a Const value in VBA.
    • You cannot use a Function to define values for a VBA Enum either.
    • You cannot have Enum members typed as Double: VBA only supports Integer and Long values for Enum members.
      • Curiously, VBA does allow Const values to be typed as Double - but most other languages don't because Double is an IEEE-754 floating point type that does not have a machine-portable representation, but as VBA is only for Win32 on x86/x64 I guess that means Microsoft made it work given the very narrow gamut of hardware that VBA programs will run on.

Anyway, if you want "named values" typed as Double that you can use anywhere, then try this:

  1. Create a new Module (not a Class Module).

  2. Rename the Module from Module1 to Weightage.

  3. Put this code in Weightage:

    Private Function ConvTohr(val As Integer) As Double
        ConvTohr = Round((val / 60), 2)
    End Function
    
    Public Property Get Var1_Weightage() As Double
        Var1_Weightage = ConvTohr(3)
    End Property
    Public Property Get Var2_Weightage() As Double
        Var2_Weightage = ConvTohr(11)
    End Property
    Public Property Get Var3_Weightage() As Double
        Var3_Weightage = ConvTohr(2)
    End Property
    Public Property Get Var4_Weightage() As Double
        Var4_Weightage = ConvTohr(9)
    End Property
    Public Property Get Var5_Weightage() As Double
        Var5_Weightage = ConvTohr(0)
    End Property
    
  4. Screenshot proof:

    (See output in the Immediate pane):

    enter image description here

  •  Tags:  
  • Related