Best way to achieve this

Hey there,

I am wondering if delegates would be the best way to achieve what I am attempting to achieve? I am not exactly sure how I would use it though.

What I am trying to achieve is varying a method to execute depending on a window.

The best way which I feel comfortable with achieving this is by simply using a method that calls a specific method depending on the window. It seems a little inefficient to have the value redetermined every time, then again it is a pretty easy method to do and probably does not hamper the performance too much.

I was wondering if there was some way to store methods depending on a certain value (a bit like interfaces). How can I improve this code?

Sub CallVariableMethod As MyClass
select case me.window
case isa Window1
return myClassInstance.Parent
case isa Window2
return myClassInstance
case isa Window3
return myClassInstance2
end select
End Sub

Sub MyMethod
TextField1.Text = CallVariableMethod.Text
End Sub

Thanks

Events are intended for this.

Best way to achieve this is by starting to give your thread a meaningful title :stuck_out_tongue_winking_eye:

As Tim said make a custom window class that becomes the super to your different windows, give it a custom event, and in each of your different windows add the code you want executed for that window.

That is one of the differences in subclassing between methods and events. A method is declared and implemented in the super and shared between all subclasses. An event is declared in the super but implemented in the subclasses.

But since you make subclasses of a single Window, you can override the methods of the super class and implement this as a method.

How would I use the event? What would trigger the event and what I would I do with the event?

Thanks

[quote=129869:@Oliver Scott-Brown]What I am trying to achieve is varying a method to execute depending on a window.

The best way which I feel comfortable with achieving this is by simply using a method that calls a specific method depending on the window. It seems a little inefficient to have the value redetermined every time, then again it is a pretty easy method to do and probably does not hamper the performance too much.
[/quote]

I have no idea what you do in your method that requires a different treatment according to the window, but why go through a select case, when you can simply use the Open event of the said window with the needed parameters calling a global method ? You can even pass the window itself like mymethod(self).

It’s really hard to answer that question without knowing more about what you’re doing. In general, you would put an instance of your class on the window and implement the custom event in each window. You trigger the event from the code in your class whenever it’s appropriate. To use your example above, you would add a method named Text to your class. That method would call the event and return the result.

MyClass:

function Text() as String
   dim s as string
   s = GetTheText
   return s
end

Window1:

Sub MyMethod
   TextField1.Text = MyClass1.Text
End

Event GetTheText() as String
   return MyClassInstance.parent.text
End

Window2:

Event GetTheText() as String
   return MyClassInstance.Text

Window3:

Event GetTheText() as String
   return MyClassInstance2.Text

[quote=130041:@Tim Hare]It’s really hard to answer that question without knowing more about what you’re doing. In general, you would put an instance of your class on the window and implement the custom event in each window. You trigger the event from the code in your class whenever it’s appropriate. To use your example above, you would add a method named Text to your class. That method would call the event and return the result.

MyClass:

function Text() as String
   dim s as string
   s = GetTheText
   return s
end

Window1:

Sub MyMethod
   TextField1.Text = MyClass1.Text
End

Event GetTheText() as String
   return MyClassInstance.parent.text
End

Window2:

Event GetTheText() as String
   return MyClassInstance.Text

Window3:

Event GetTheText() as String return MyClassInstance2.Text [/quote]
Wouldn’t I have to use an interface to call GetTheText? I want to call the method from a class which subclasses textfield? It does not look like you can implement events as part of the interface directly so wouldn’t it be easier to just use a method instead of an event? Maybe there is some misunderstanding. Excuse my poor explanation. Thanks

So you want a TextField to call some method that will return a string, and that method will be implemented differently on each window? If so, then use an Interface on the window. Say you define a class Interface named InfoProvider (for example) with one method

Function ReturnSomeText() as String

Have each window implement the interface and add that method to each window with whatever code you need. Then in your TextField subclass, when you need to get that information

if Window IsA InfoProvider then
   me.Text = InfoProvider(Window).ReturnSomeText
end