bjwade62 Posted October 31, 2006 Posted October 31, 2006 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. 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 Quote
Administrators PlausiblyDamp Posted October 31, 2006 Administrators Posted October 31, 2006 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. If DirectCast(eventSender, RadioButton).Checked Then SaveDepart = "Arch" End If Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
bjwade62 Posted October 31, 2006 Author Posted October 31, 2006 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 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. If DirectCast(eventSender, RadioButton).Checked Then SaveDepart = "Arch" End If Quote
Leaders snarfblam Posted October 31, 2006 Leaders Posted October 31, 2006 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. Quote [sIGPIC]e[/sIGPIC]
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.