0

I am having trouble assigning a value to constant variant declaration. So the declaration is this

Option Explicit
public const Abc as variant = ?

Abc is a Microfocus rumba object which gets initiated when the workbook is open, something like this

sub workbook_open()
   Set Abc= session.GetRDEObject()
End sub

But I cannot use this to assign it to the const variable, it says "Circular dependencies". The reason I am wanting to assign value is to ensure the object Abc is alive even after user executes End statement. Is there a way I can do this?.

I have tried creating a separate function and adding its return value to the Abc, but it does not work.

braX
  • 11,506
  • 5
  • 20
  • 33
Samantha
  • 3
  • 1
  • You could just make a `GetAbc()` function that returns that object without the need for a `Const`. (Or try using `Object` instead of `Variant`) – braX May 23 '22 at 18:59
  • 2
    The value of a `Const` needs to be knowable at *compile time* (ie. before the code is actually run) – Tim Williams May 23 '22 at 20:22
  • @TimWilliams, yea I agree and I think that is where I am stuck. – Samantha May 23 '22 at 20:40

1 Answers1

0

A constant is exactly that, so try something like this:

Option Explicit
Private Abc As Object

Private Sub Workbook_Open()
   Set Abc = Session.GetRDEObject()
End sub

Public Function GetAbc() As Object
    Set GetAbc = Abc
End Function
Gustav
  • 53,498
  • 7
  • 29
  • 55
  • Thanks for sharing the code. But if the I make the Abc as private, user is not able to edit the object(or process data), it gives out an error "Object Required". My main goal is to keep the Abc object active after user clicks End. Please share if you have any tips. – Samantha May 23 '22 at 20:38
  • 1
    If the user clicks End after an error message, everything is lost - that's why it is labelled _End_. To prevent that, apply _error handling_. – Gustav May 24 '22 at 05:38