How to avoid Late Binding

  • Thread starter Thread starter William Ryan
  • Start date Start date
W

William Ryan

In a nutshell, I need to get the name of the object that was just pressed
(in this case, the object will be a radioButton). I want to write one
handler for all four radio buttons and branch off depending on which one was
clicked..I can get it to work if I take off Option Strict and use late
Binding...but rather program an abacus than turn off Option Strict... here's
my code

Private Sub optFullTime_CheckedChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles optFullTime.CheckedChanged,
optContractor.CheckedChanged, optCurrent.CheckedChanged,
optFormerEmployees.CheckedChanged, optTemporary.CheckedChanged,
optPartTime.CheckedChanged

Select Case sender.GetType.Name ' Returns RadioButton

'If I turn off option strict I can use sender.Name and it works but I know
I'm overlooking something trivial. So what I need is to know the name of
the control that was pressed. What am I overlooking?

Case "optFullTime"

MessageBox.Show(e.GetType.Name)

Case "optPartTime"

MessageBox.Show(e.GetType.Name)

Case "optContractor"

MessageBox.Show(e.GetType.Name)

Case "optTemporary"

MessageBox.Show(e.GetType.Name)

End Select
 
William,
Use DirectCast to cast the sender object into a known type. You could use
either RadioButton or Control if you wanted just the name.

Something like:
Private Sub optFullTime_CheckedChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles optFullTime.CheckedChanged,
optContractor.CheckedChanged, optCurrent.CheckedChanged,
optFormerEmployees.CheckedChanged, optTemporary.CheckedChanged,
optPartTime.CheckedChanged

Dim opt As Control = DirectCast(sender, Control)
Select Case opt.Name

or
Select Case DirectCast(sender, RadioButton).Name

I normally use the first as invariable I need mutliple accesses to the
sender.

Hope this helps
Jay
 
I have been using ctype(sender,controltype).property for example. Is
DirectCast better than Ctype. What is the difference.
 
vMike,
The way I view them is:

CType = Convert type
DirectCast = Cast type

Convert type will use conversion routines to physically change an object
from one type (String) into an object of a different type (Integer). If the
object is already that type a simple cast is done, however extra checks are
made to see if a conversion can occur (read as extra overhead).

Dim i As Integer
Dim s As String
i = CType(s, Integer)

Cast type allows an assignment of an object of a specific type to that
specific type, where the object is currently in a variable of a compatible
type, by compatible type I mean a base type. Or the specific type is an
Interface, and the object implements the interface. DirectCast is important
when using Option Strict On. You are using Option Strict On! Correct!

Dim o As Object = New Form1()
Dim frm As Form1
frm = DirectCast(o, Form1)


The following articles explains, among other things, conversions vs casting:
http://msdn.microsoft.com/library/d...tml/vbtchmicrosoftvisualbasicnetinternals.asp

Normally I use DirectCast, unless I have to use CType. I have to use CType
when DirectCast causes a compile error. Or I am actually doing a conversion.

Hope this helps
Jay
 
Jay
Yes Option Strict is On for sure!! I think I will change my ctype's to
directcast's were possible. Looks like it will be a little less overhead.
Thanks for the information.
Mike
 
Back
Top