Class in dll

AWatkins used Ask the Experts™
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.


Dim reg as RegistryEntry

reg.variable = 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?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Private means that they are only accessible by the class/module they are declared in!

you would otherwise make them Public


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
   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
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

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
     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.


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?
This is correct.  You must decide if you are using the value passed, or the value set by your PROPERTY LET statement, and an If or Case is the only way to do this.


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,

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial