lothos12345 Posted March 8, 2005 Posted March 8, 2005 (edited) Late Binding I have a program in VB.NET that requires that Option Strict be activated and it is giving me a late binding error. With Option Strict off there is not a probelm, however the requirement is that Option Strict be On. The code is a follows: Private Sub ContextMenu1_clicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim sendnoteid As Integer Dim tParent As String Dim tchild As String Dim tnote As String Dim i As Integer Try ' get group and replace active row cell with menu option tnote = UltraGrid1.ActiveRow.Cells(2).Value.ToString tParent = sender.Parent.Text.ToString ******error tchild = sender.Text.ToString ******* error 'tchild = ContextMenu1.ToString For i = 0 To UBound(afindtext) If afindtext(i) = tParent & "*" & tchild Then UltraGrid1.ActiveRow.Cells(2).Value = Replace(tnote, areplacetext(i), tchild) i = UBound(afindtext) UltraGrid1.ActiveRow.Cells(0).Value = True ' if checkbox not checked then check it End If Next Catch ex As Exception Call errhandler(ex.Message, ex.StackTrace, ex.Source) End Try End Sub Not quite sure how to resolve this issue. As always any help would greatly be apprieciated. Edited March 8, 2005 by lothos12345 Quote
RobEmDee Posted March 8, 2005 Posted March 8, 2005 lothos: You need to cast sender to its actual type. I believe you use CType() in VB.NET. I have a program in VB.NET that requires that Option Strict be activated and it is giving me a late binding error. With Option Strict off there is not a probelm, however the requirement is that Option Strict be On. The code is a follows: Private Sub ContextMenu1_clicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim sendnoteid As Integer Dim tParent As String Dim tchild As String Dim tnote As String Dim i As Integer Try ' get group and replace active row cell with menu option tnote = UltraGrid1.ActiveRow.Cells(2).Value.ToString tParent = sender.Parent.Text.ToString ******error tchild = sender.Text.ToString ******* error 'tchild = ContextMenu1.ToString For i = 0 To UBound(afindtext) If afindtext(i) = tParent & "*" & tchild Then UltraGrid1.ActiveRow.Cells(2).Value = Replace(tnote, areplacetext(i), tchild) i = UBound(afindtext) UltraGrid1.ActiveRow.Cells(0).Value = True ' if checkbox not checked then check it End If Next Catch ex As Exception Call errhandler(ex.Message, ex.StackTrace, ex.Source) End Try End Sub Not quite sure how to resolve this issue. As always any help would greatly be apprieciated. Quote
Leaders snarfblam Posted March 9, 2005 Leaders Posted March 9, 2005 tParent = DirectCast(sender, MenuItem).Parent.Text.ToString 'sender is a menu item, right? Also CType will work equally well. DirectCast performs a little better but will not do many things such as converting strings to ints and the like, it will only, surprise, make a direct cast. Quote [sIGPIC]e[/sIGPIC]
lothos12345 Posted March 9, 2005 Author Posted March 9, 2005 Error It keeps telling me that "text" is not a member of "Parent". Please help. Quote
Leaders snarfblam Posted March 11, 2005 Leaders Posted March 11, 2005 First of all, I am assuming that the object raising the event is a menu item in a sub menu. If it is a top level menu item in the context menu then its parent will be a ContextMenu component, which doesn't have a .Text property. If it is a sub menu item, its parent will be another menu item. Secondly, with Option Strict On, you need to do a lot more casting and conversion. Sender is passed as an object, which does not have a .Text property. Since Sender actually points to a MenuItem, you can cast (CType/DirectCast) Sender to a MenuItem and gain access to all of its properties and methods. When you get the menu item's parent, it returns the parent as an instance of the "Menu" class. This, again, does not have a .Text property. But the "Menu" that you get from the .Parent property actually points to a MenuItem, so you can gain access to all of its properties and methods by casting. tParent = DirectCast(DirectCast(sender, MenuItem).Parent, MenuItem).Text.ToString 'sender must be cast to a MenuItem to access the .Parent property. 'DirectCast(sender, MenuItem).Parent must be cast to a MenuItem to access the .Text property. Also, since the .Text property returns a string, you do not need to use the .ToString method. Calling .ToString on a string will just return a copy of the string. To work with option strict on you need to pay close attention to types, and I recommend a good understanding of inheritance so that you know when you can cast from one class to another (i.e. MenuItem inherits from Menu, so If you have a Menu that you know points to a MenuItem, you can cast. MenuItem [and everything] also inherits from Object, so if you have an Object that you know points to a MenuItem, you can cast). Quote [sIGPIC]e[/sIGPIC]
lothos12345 Posted March 11, 2005 Author Posted March 11, 2005 Thanks Thanks for the help I really appreciate it. Quote
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.