AWatkins
asked on
Class in dll
I have a dll where I am currently using properties to input the values. I would like to give the user the option to do both via properties and on function call.
eg:
Dim reg as RegistryEntry
reg.variable = 1
reg.go
or
reg.go(1)
The question is, how do I tell the function to look at the private variables set by the properties as well as it's own? Can/Should the variable in the function be the same name as the property variable?
eg:
Dim reg as RegistryEntry
reg.variable = 1
reg.go
or
reg.go(1)
The question is, how do I tell the function to look at the private variables set by the properties as well as it's own? Can/Should the variable in the function be the same name as the property variable?
Using your example, the Reg.Go call would be something like:
Public Function Go(Optional ByVal MyVar As Integer = -1) ' Some value that should not / can not be passed
Dim LocalMyVar As Integer
If MyVar <> -1 Then
LocalMyVar = MyVar
Else
LocalMyVar = ModuleMyVar
End If
' rest of your code.... use LocalMyVar as the value
Public Function Go(Optional ByVal MyVar As Integer = -1) ' Some value that should not / can not be passed
Dim LocalMyVar As Integer
If MyVar <> -1 Then
LocalMyVar = MyVar
Else
LocalMyVar = ModuleMyVar
End If
' rest of your code.... use LocalMyVar as the value
You can create an optional parameter for you function. But you need to make sure that you have SOME value to work with, if not then you need to raise an error...
So basically:
Dim mvarVariable As Variant
Public Property Get Variable() As Variant
Variable = mvarVariable
End Property
Public Property Let Variable(ByVal vNewValue As Variant)
mvarVariable = vNewValue
End Property
Public Function go(Optional nInput As Variant = 0) As Integer
If nInput = 0 Then
nInput = mvarVariable
End If
If nInput = 0 Then
Err.Raise vbError + 1, , "You need to either set VARIABLE <> 0 or supply an Input Parameter"
Exit Function
End If
MsgBox nInput
End Function
So basically:
Dim mvarVariable As Variant
Public Property Get Variable() As Variant
Variable = mvarVariable
End Property
Public Property Let Variable(ByVal vNewValue As Variant)
mvarVariable = vNewValue
End Property
Public Function go(Optional nInput As Variant = 0) As Integer
If nInput = 0 Then
nInput = mvarVariable
End If
If nInput = 0 Then
Err.Raise vbError + 1, , "You need to either set VARIABLE <> 0 or supply an Input Parameter"
Exit Function
End If
MsgBox nInput
End Function
They names cannot be the same or you will get an error. This shouldn't be a problem if you use standard naming conventions.
reg should be m_reg
So now, the class can use:
'they call this when they want to set the reg property
public property set reg(byval v_oRegistryEntry as RegistryEntry)
set m_reg = v_oRegistryEntry
end property
'they call this when they want to use the existing registry entry
public property get reg() as RegistryEntry
if m_reg is nothing then
set m_reg = new RegistryEntry
endif
set reg = m_reg
end property
Internally you can set m_reg all you want and if you simply want to gt that value, use the property get. When you want to pass in a new value, use the set.
reg should be m_reg
So now, the class can use:
'they call this when they want to set the reg property
public property set reg(byval v_oRegistryEntry as RegistryEntry)
set m_reg = v_oRegistryEntry
end property
'they call this when they want to use the existing registry entry
public property get reg() as RegistryEntry
if m_reg is nothing then
set m_reg = new RegistryEntry
endif
set reg = m_reg
end property
Internally you can set m_reg all you want and if you simply want to gt that value, use the property get. When you want to pass in a new value, use the set.
ASKER
So, I have to handle it separately? I have to write an if statement in the beginning of all my functions saying if the function had a value passed into it, use it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the quick response. I thought there would be a way to not write your own if statements, as this is very tedious, but I suppose the answer is you do have to.
Thanks Again,
AWatkins
Thanks Again,
AWatkins
There are a couple of options here.
1) When using an optional parameter of a simple type, you can pass a default value in
2) if you are talking about module-level variables, you can set them to some default on the class initialize. That way they always have a value and there may not be a need to test in each prop get
1) When using an optional parameter of a simple type, you can pass a default value in
2) if you are talking about module-level variables, you can set them to some default on the class initialize. That way they always have a value and there may not be a need to test in each prop get
you would otherwise make them Public
regards
chris