Execute code if method is available

I’m selling some code. So I want to take everything out of the existing code that is private like my SendGrid password. I can check if a method is available. But can anyone remind me how to use this with a pragma?

Or is there a better solution?

Existing code:

if HasMethod(Globals, "GetMore") then socket1.username = Globals.GetMore

[code]Private Function HasMethod(theObject as Object, methodName as String) As Boolean

'check if a class has a specific Method

dim theMethods(-1) as MethodInfo = theObject.Methods
for Each currentMethod as Introspection.MethodInfo in theMethods
dim theName as String = currentMethod.Name
if theName = methodName then Return true
next
End Function[/code]

You could make a constant somewhere that is called kHasMyMethod as Boolean, and set that to False if not. Then in your code, just

#if kHasMyMethod //call method here #else // do something generic #end if

Thanks, Greg. That was what I was looking for. But that only works for constants and not with a property or a method, doesn’t it?

Right.

If the method isn’t available then this code won’t compile as Globals.GetMore doesn’t exist. Instead return the MethodInfo and call it with that

[code]Private Function HasMethod(theObject as Object, methodName as String) As Introspection.MethodInfo

dim theMethods(-1) as MethodInfo = theObject.Methods
for Each currentMethod as Introspection.MethodInfo in theMethods
dim theName as String = currentMethod.Name
if theName = methodName then Return currentMethod
next

End Function

dim m As Introspection.MethodInfo = HasMethod(Globals, “GetMore”)

if m <> nil then socket1.username = m.Invoke(Globals)[/code]

Thanks Will!