Solved

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

Posted on 2007-12-05
3
602 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 86

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

751 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