Refer object as a common class.

  • Thread starter Thread starter Mr. X.
  • Start date Start date
M

Mr. X.

Hello.
If I have the code : sender As System.Object
and sender is Button class.
How can I refer sender as a button ?

Thanks :)
 
Hello.
If I have the code : sender As System.Object
and sender is Button class.
How can I refer sender as a button ?

Thanks :)

Dim b As Button = TryCast(sender, Button)

If b IsNot Nothing Then
' do cool stuff with the button specific stuff
Else
MessageBox.Show ("sender is not a button!")
End If
 
Am 02.03.2010 23:42, schrieb Mr. X.:
If I have the code : sender As System.Object
and sender is Button class.
How can I refer sender as a button ?

\\\
Dim SourceButton As Button = DirectCast(sender, Button)
....
///
 
Thanks.
What is the difference between tryCast and directCast ?

Thanks :)

DirectCast and CType will throw an exception if they can not
cast/convert the object to the target type. TryCast will not throw an
exception, but return nothing.
 
Am 04.03.2010 22:51, schrieb Mr. X.:
Thanks.
What is the difference between tryCast and directCast ?

Have a look at your keyboard. In the top left, do you see the
"Esc" key there? On it's right, there's a key that reads "F1".
If you press it, you will get a lot of documentation. This
feature is even case sensitive. So, if you press it while
the caret is placed on one of these keywords, you will
get help about them.

;-)

Ok ok, more helpful: (look at the table)
http://msdn.microsoft.com/en-us/library/zyy863x8(VS.80).aspx
 
Back
Top