Jarod Posted March 11, 2004 Posted March 11, 2004 Here is my problem ! I have created some custom controls, let's say : Class A Inherits Control End Class Class B Inherits Control End Class Then I have a form with several controls and some custom controls. For my custom controls, I want to call a specific function. I wannot easily do a call for each of my control (too many controls) so I do a simple loop: With Tools classe defined as : Class Tools Public Sub myFunction(ByVal myObj As baseClass) If TypeOf myObj Is baseClass Then 'Do something ElseIf TypeOf myObj Is A Then 'Do something else ElseIf TypeOf myObj Is B Then 'Do something else End If End Sub End Class It's highly dirty ! I don't want to do any cast, or use typeof... One better solution is to define my method in the baseClass, A & B class, and so use inheritance. But this would force me to test the type in my loop as I cannot add my method in the base class (Control class). That's why I would prefer a "Visitor class". So I want to modify my Tool class as : Class Tools Public Overloads Shared Sub FunctionCall(ByVal myObj As Control) 'Do Nothing Console.WriteLine("Call of FunctionCall(ByVal myObj As Control)") End Sub Public Overloads Shared Sub FunctionCall(ByVal myObj As baseClass) 'Do Something Console.WriteLine("Call of FunctionCall(ByVal myObj As baseClass)") End Sub Public Overloads Shared Sub FunctionCall(ByVal myObj As A) 'Do Something Console.WriteLine("Call of FunctionCall(ByVal myObj As A)") End Sub Public Overloads Shared Sub FunctionCall(ByVal myObj As B) 'Do Something Console.WriteLine("Call of FunctionCall(ByVal myObj As B)") End Sub End Class and do my loop simply like For Each myControl In Me.GroupBox1.Controls Tools.myFunction(myControl) Next But this doesn't work ! Even all the controls in my groupbox are of type A, they are stored in a member of static type Control. And the static type is use at compilation time to determine which method is call and so Public Overloads Shared Sub FunctionCall(ByVal myObj As Control) is called. Can we determine that a method must be called using the Dynamic type of the object and not the static type ? (so the "A version" will be called instead of the "Control Version") Do you see an another solution ? Thanks, Quote Jarod
Administrators PlausiblyDamp Posted March 11, 2004 Administrators Posted March 11, 2004 You could create a class that inherits from Control, implement the functions there and inherit ClassA and ClassB from this new class, or if there is no common code and you merely need the function to be present then you could define and implement an Interface. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Jarod Posted March 11, 2004 Author Posted March 11, 2004 I already have that ! A & B inherits from BaseClass that inherits from Control. I agree with you, I can define that method in BaseClass and override it as needed in A & B. BUT :) doing that resolve only part of my problem. If I do that, I still have to test the type before calling it (remind that I loop on the controls of the forms) For Each myControl In Me.GroupBox1.Controls if typeof myControl is BaseClass then Tools.myFunction(myControl) Next and I don't really like that ! Quote Jarod
Administrators PlausiblyDamp Posted March 11, 2004 Administrators Posted March 11, 2004 In that case as far as I'm aware you will have to do the check before calling the function. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.