Late Binding Issue
-
Can Somebody help me here :-) I have written an application for two different motion controllers - The code for each controller is placed in it's own module with identical subs Depending on the controller selected it will execute the appropriate code Here is the problem .. after setting the project to OPTION STRICT = ON I get the late binding not allowed message and I just can not get it fixed :-) Below is a sample code that should help understand my issue easier (I l know it's an easy thing - I just can not figure it out) It's important for me that the Variable [SelectedController] that holds the object to the selected module is accessible throughout the whole project ! PS the code below doesn’t work = The variable [SelectedController] is not accessible outside the subs ! Where I try to call the actual subroutine is where I get the late binding error Example : SelectedController.test() Thanks Georg Public Module DECLARE_VARAIBLES Public SelectedController As Object ' selected controller End Module Public Class Form1 Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim SelectedController As New ControllerA Display_Controller() End Sub Public Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim SelectedController As New ControllerB Display_Controller() End Sub Private Sub Display_Controller() '************** ' HERE IS WHERE I USUALLY RUN INTO MY LATE BINDING ISSUE SelectedController.test() End Sub End Class Public Class ControllerA Public Sub test() MsgBox("running Controller A") End Sub End class Public Class ControllerB Public Sub test() MsgBox("running Controller B") End Sub End Class
-
Can Somebody help me here :-) I have written an application for two different motion controllers - The code for each controller is placed in it's own module with identical subs Depending on the controller selected it will execute the appropriate code Here is the problem .. after setting the project to OPTION STRICT = ON I get the late binding not allowed message and I just can not get it fixed :-) Below is a sample code that should help understand my issue easier (I l know it's an easy thing - I just can not figure it out) It's important for me that the Variable [SelectedController] that holds the object to the selected module is accessible throughout the whole project ! PS the code below doesn’t work = The variable [SelectedController] is not accessible outside the subs ! Where I try to call the actual subroutine is where I get the late binding error Example : SelectedController.test() Thanks Georg Public Module DECLARE_VARAIBLES Public SelectedController As Object ' selected controller End Module Public Class Form1 Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim SelectedController As New ControllerA Display_Controller() End Sub Public Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim SelectedController As New ControllerB Display_Controller() End Sub Private Sub Display_Controller() '************** ' HERE IS WHERE I USUALLY RUN INTO MY LATE BINDING ISSUE SelectedController.test() End Sub End Class Public Class ControllerA Public Sub test() MsgBox("running Controller A") End Sub End class Public Class ControllerB Public Sub test() MsgBox("running Controller B") End Sub End Class
This is the normal - and desired - result if you try to invoke a method "test" on an boxed object ( indeed you are calling object.test() ). If you have some classes that each implements the same methods you should use Interfaces like this: Somewhere outside a class define:
Public Interface ITest Sub test() End Interface
and then in your classes (ControllerA and ControllerB):Public Class ControllerA Implements ITest Public Sub test() MsgBox("running Controller A") End Sub End Class
(ControllerB similar) And finally in your Module:Public Module DECLARE_VARAIBLES Public SelectedController As ITest ' selected controller End Module
-> you will get IntelliSense help and the code will work ;) -
This is the normal - and desired - result if you try to invoke a method "test" on an boxed object ( indeed you are calling object.test() ). If you have some classes that each implements the same methods you should use Interfaces like this: Somewhere outside a class define:
Public Interface ITest Sub test() End Interface
and then in your classes (ControllerA and ControllerB):Public Class ControllerA Implements ITest Public Sub test() MsgBox("running Controller A") End Sub End Class
(ControllerB similar) And finally in your Module:Public Module DECLARE_VARAIBLES Public SelectedController As ITest ' selected controller End Module
-> you will get IntelliSense help and the code will work ;)I'm not sure if I'm getting this ... Am i correct in that Interfaces are intended to be used for code that's exactly the same ? Public Interface ITest Sub test() End Interface Itest will be the same in both controllerA & controllerB If this is how it works that that's NOT what I need I have a user Interface that will call the subs inside to different classes, ControllerA or ControllerB (The SUB NAMES Inside the classes are identical but the code inside the subs will never be!) I believe it would make my live a lot easier (and Cleaner to maintain ) if I could just call the "same" subs out of my user interface Sub Jog_X() SelectedController.jogging ' where SelectedControler.Jog_X will have to point to 'A) ControllerA.Jog_X 'or 'B) ControlerB.Jog_X end sub Georg