Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing VBA Variable by reference to class function

Tags:

vba

I have a custom class with various Get/Let properties

Property Get pGasIP() As Double
    pGasIP = zpGasIP
End Property

Property Let pGasIP(ByVal NewValue As Double)
    zpGasIP = NewValue
End Property

Along with functions that reference these variables:

Public Sub CalcProduction(Index As Long, life As Double)

zoGasProdStartGross = TypeCurve("cum", -Base + 1, pGasTimeRestricted, _
pGasIP, pGasDi, pGasDiHyp, pGasBFactor, 0.06, pGasQab, pCurtailment)

End Sub

I am trying to iterate on the variables within this function by passing a reference to the desired variable in a Breakeven function as follows (iterative variable refers to the class property that I would like to reference, ObjFunction refers to an output value that I would like to reference and change)

Public Sub Breakeven(ByRef iterativeVariable As Variant, ByRef ObjFunction as variant)
Dim DesiredValue as Double, CurrentDistance as Double

'set up incremental calculation
    IterativeVariable = IterativeVariable + .001
    CurrentDistance = DesiredValue - ObjFunction

'calculate new value with incremented variable 
    CalcProduction 600, 600
    CurrentDistane = DesiredValue - ObjFunction

When I try to pass the object properties in a normal module, the values of the corresponding class properties are being passed rather than the reference. Is there a way to pass object properties into a subroutine that can modify/calculate them?

Thanks!

Jordan

EDIT:

I am trying to pass the following argument in a normal module:

Object.Breakeven Object.GasIP, Object.GasEUR

I want the actual references to the properties passed in (so I can modify them), but this nomenclature passes in the value of the properties instead.

like image 565
JSS Avatar asked Feb 13 '26 15:02

JSS


1 Answers

It's hackish and probably glacially slow, but you can manipulate object properties on the fly using CallByName. Here's an example:

Sub modismod(o1 As Object, prop1$, o2 As Object, prop2$)
    Dim i1&, i2&
    i1 = CallByName(o1, prop1, VbGet)
    i2 = CallByName(o2, prop2, VbGet)
    i1 = i1 + 1
    i2 = i2 - 1
    CallByName o1, prop1, VbLet, i1
    CallByName o2, prop2, VbLet, i2
End Sub
like image 105
Chel Avatar answered Feb 15 '26 16:02

Chel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!