Late Binding Problem

lothos12345

Junior Contributor
Joined
May 2, 2002
Messages
294
Location
Texas
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.
 
Last edited:
lothos:
You need to cast sender to its actual type. I believe you use CType() in VB.NET.


lothos12345 said:
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.
 
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.
 
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.

Visual Basic:
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).
 
Back
Top