Option strict disallows late binding?

qadeer37

Well-known member
Joined
May 1, 2010
Messages
63
Location
Hyderabad,pakistan
Programming Experience
1-3
Public Class Form1

Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click, btn2.Click, btn3.Click

txtResult.Text = txtResult.Text + CType(sender.text, String)

End Sub
End Class

Error due to sender.
I don't want to turn off option strict.
 
sender parameter is type Object, Object class doesn't have a Text property. You have to cast sender to type Control or the more specific type Button, then you can access its Text property. Text property already returns String type values so you don't need to cast/convert that.
Also, to avoid unintensional side effects I recommend using & operator to add strings. Short for txt = txt & moretxt is txt &= moretxt using the &= operator.
VB.NET:
txtResult.Text &= CType(sender, Button).Text
If txtResult is a TextBox control then you can use its AppendText method:
VB.NET:
txtResult.AppendText(CType(sender, Button).Text)
 
Back
Top