Solved

why "CType(sender,....", instead of the object, itself?

Posted on 2007-12-05
3
590 Views
Last Modified: 2010-04-21
I am studying for the MCAD 70-306 exam, using Gunderloy. I notice that he often has code such as the following:

Private Sub dudColor_SelectedItemChanged(  ByVal sender As System.Object,  ByVal e As ystem.EventArgs) _
 Handles dudColor.SelectedItemChanged
    ' Typecast the object to DomainUpDown
    Dim dudCol As DomainUpDown =  CType(sender, DomainUpDown)
    ' Change color of lblsampleText to selected color
    lblSampleText.ForeColor = Color.FromName(dudCol.Text)
End Sub


My question is, since you have access to dudColor, why bother with

    Dim dudCol As DomainUpDown =  CType(sender, DomainUpDown) ???

Why can't you, or shouldn't you,  just write:

 lblSampleText.ForeColor = Color.FromName(dudColor.Text) ???

0
Comment
Question by:sperodev
3 Comments
 
LVL 9

Expert Comment

by:sognoct
ID: 20414075
because the sender is a non typed Object, so you MUST tell the compiler that it is of a specific kind for using its methods.
This function can handle more than one kind of sender, so the nature of the sender can be different and also its methods
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 20414656
If "dudCol" is the ONLY NumericUpDown being handled by this method then there is no point in using CType() to cast the "sender" parameter to a locally declared variable.

As sognoct has pointed out, however, it is possible that the method could hanlde multiple controls.  What if other NumericUpDown controls have been "wire up" using AddHandler()?  By using generic code as Gunderloy has shown, the method will correctly respond no matter which control was the "source" of the event.

I personally think it is a bad idea though to give the local variable the exact same name as the design-time name of the control.  That often leads to confusion.  Instead, use a generic name:
Private Sub dudColor_SelectedItemChanged(  ByVal sender As System.Object,  ByVal e As ystem.EventArgs) _

 Handles dudColor.SelectedItemChanged

    ' Typecast the object to DomainUpDown

    Dim dud As DomainUpDown =  CType(sender, DomainUpDown)

    ' Change color of lblsampleText to selected color

    lblSampleText.ForeColor = Color.FromName(dud.Text)

End Sub

Open in new window

0
 

Author Closing Comment

by:sperodev
ID: 31412937
Thanks also to sognoct.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now