Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel Spreadsheet - Send email if certain value is selected. 14 85
Sending a email via excel using vba 6 71
vb6 connector to mongodb 2 45
How to Add / Edit Windows Menu 4 49
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

932 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