Solved

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

Posted on 2007-12-05
3
599 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

807 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