execute a method name in shared classes

hello,

i have external classes that i use in two apps.

how can i store and execute methods depending the app i’m using ? for example

mySharedMethod :

if myApp = “xxx” then
windowAA.methodxxx
else if myApp = “yyy”
windowBB.methodyyy
end if

because i can’t compile if the method of the window is not present

thanks a lot !

You define a constant where you can access it:

Public Const kMaxVersion as Number = 0

and an Enumeration:

Public Enum Version normal Server End Enum

Then you can use the constant in a pragma:

#if kMAXVersion = Version.normal then If Globals.thePrefs.GetPrefString("Export_FormatMain") = "" Then globals.thePrefs.setPrefString("Export_FormatMain", "Valentina") #elseif kMAXVersion = Version.Server then If Globals.thePrefs.GetPrefString("Export_FormatMain") = "" Then globals.thePrefs.setPrefString("Export_FormatMain", "ValentinaServer") #else

If the conditional statement is working out just fine and it’s only the window that’s causing you trouble you don’t even need to use or define constants (though great advice from Beatrix). Just use the compiler conditional #if statements.

#if myGreatApp = "xxx" then windowA.Method #elseif myGreatApp = "yyy" then windowB.Method #endif

However, I would argue that because of what you’re doing the code shouldn’t be in a shared module. You’re going out of your way to find what app it is? That code should be local to the app! You may want to reconsider the design, there’s probably a better way to approach what you’re attempting.

Sounds like a callback type setup you have there. There are two ways to code around this.

Interfaces

  1. Create an class interface with a method called iMethodName() in your external classes.
  2. Implement this interface in both windows. This will add iMethodName to both windows.
  3. Pass in the interface into your shared classes which needs to know app XXX passes in windowAA, app YYY passes in windowBB
  4. Call passed in parameters method iMethodName() when you need to, not caring what it points to.

Delegates

  1. Create a delegate in the external classes. Defining any parameters needed to be called and whats returned.
  2. Add a parameter of same type as the delegate created in 1 into external class method that needs to call back to window.
  3. Add a method / function into windowAA and windowBB that matches the delegate defined in 1
  4. Pass the pointer to the delegate using AddressOf the function created in 3.
  5. Call invoke on the delegate passed into external classes to fire off the method in windowAA or windowBB without caring which.

Hope this helps. I am assuming class interfaces are visible from external classes etc. If not then delegates is the way to go.