Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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,

Jarod
  • Administrators
Posted
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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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 !

Jarod

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...