Late Binding vs. Early Binding

bjwade62

Centurion
Joined
Oct 31, 2003
Messages
104
Recently I turned option strict on in my new applications. I understand that when its on implicit conversion are not allowed. I also understand that this is a good thing. However I'm getting a few errors that I need to cleanup. Also a good thing. The problem I'm having is that I don't have a good grip on the definitions of Late and Early binding. I have the following event that has a late binding error in the eventSender.Checked portion of my code. Can someone explain this to me in layman's terms? I'd really appreciate it. I've looked it up and still don't get it.

Visual Basic:
    Private Sub OptArch_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles OptArch.CheckedChanged
        If eventSender.Checked Then
            SaveDepart = "Arch"
        End If
    End Sub
 
The parameter eventSender is declared as Object - this means it could contain anything, therefore the compiler cannot validate that the eventSender.Checked property exists and if it does what data type it supports.

You will need to cast eventSender to the correct object type e.g.
Visual Basic:
If DirectCast(eventSender, RadioButton).Checked Then
    SaveDepart = "Arch"
End If
 
Ok thanks. I get that object could contain anything. So the DirectCast is telling the compiler that eventSender is a RadioButton? Is that correct?

Thanks,
Bernie

PlausiblyDamp said:
The parameter eventSender is declared as Object - this means it could contain anything, therefore the compiler cannot validate that the eventSender.Checked property exists and if it does what data type it supports.

You will need to cast eventSender to the correct object type e.g.
Visual Basic:
If DirectCast(eventSender, RadioButton).Checked Then
    SaveDepart = "Arch"
End If
 
Using option strict means that you need to think alot more in terms of types. Every reference (whether it is stored in a variable or the result of a function or expression) has a type, and every object has a type. eventSender is a reference and its type is object, so the compiler doesn't know anything about it except that it is an object (event though it is actually a RadioButton). DirectCast(eventSender, RadioButton) is actually an expression that converts the type of the reference, so we start with a reference to eventSender as an object and end up with a reference to eventSender as a RadioButton.

You are doing more than telling the compiler that eventSender is a RadioButton. When the program is running the .Net runtime will actually verify that eventSender is indeed a RadioButton and then change the reference to eventSender from a reference to an object to a reference to a RadioButton.

If eventSender isn't a RadioButton an error will occur when the DirectCast is executed. The VB feature that allows you to use methods on an object when you don't know the type, late binding, actually requires VB to determine the type of the object and search that type's functions for the function that has the same name and the closest match for parameter types before the function can be called, and that is done each time the function is called. In other words, late binding is slow on top of error-prone.
 
Back
Top