Link to home
Start Free TrialLog in
Avatar of sperodev
sperodev

asked on

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

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) ???

Avatar of sognoct
sognoct
Flag of Italy image

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
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sperodev
sperodev

ASKER

Thanks also to sognoct.