Simple C# question

  • Thread starter Thread starter Timothy Taylor
  • Start date Start date
T

Timothy Taylor

Hello,

Once again, I am new to C#, but I have a simple question...

I noticed that Ctype, Cint, Cstr etc... is not supported in C# as in VB.NET

The only thing I could find is Convert.to... but that has no ToButton
method. I want to convert a sender object to a button so i can get/set it's
properties. In VB.NET i would just say

Dim btnSender as button = Ctype(sender, button)

what would be the equivalent of that?

Thanks a lot and please feel free to give me any more useful VB.NET to C#
newbie tips

-Tim
 
Button button = (Button)sender;
or
Button button = sender as Button;

Case sensitive!
 
on top of what alex said I would add some more comment

Button button = (Button) sender;
would thrown a ClassCastException if sender is not effectively a Button

Button button = sender as Button;
would create a null value (no button at all) if sender is not effectively a
button, so you will get NullReferenceException later on when you will try to
use it and it was not correct
 
Ok, thanks a lot guys!!

You guys are very helpful...

What about Select Case statements

C# doesn't seem to like it when i type

Select Case

why not?

Thanks,

-Tim
 
Um, get a book.

Timothy Taylor said:
Ok, thanks a lot guys!!

You guys are very helpful...

What about Select Case statements

C# doesn't seem to like it when i type

Select Case

why not?

Thanks,

-Tim
effectively try
 
To answer the question, you use a switch statement:

switch (myVariable)
{
case "abc":
....
break;

case "def":
....
break;

default:
....
break;
}

- Mark
 
Back
Top