simple C# beginner question:
-
how do I convert from object to control (the same as CType in VB)
private void button_click(object sender, EventArgs e) { MessageBox.Show(sender.Text); }
I think I should convert the sender to a button. in VB i don't have to it, it converts it automatically...Shimi
-
how do I convert from object to control (the same as CType in VB)
private void button_click(object sender, EventArgs e) { MessageBox.Show(sender.Text); }
I think I should convert the sender to a button. in VB i don't have to it, it converts it automatically...Shimi
What about like that?
(Button)sender.Text
Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)
-
What about like that?
(Button)sender.Text
Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)
Michael Sync wrote:
(Button)sender.Text
True. But I would say a line of check by having a call to
.GetType()
to prevent falling prey toSystem.InvalidCastException
.Vasudevan Deepak Kumar Personal Homepage
Tech Gossips
A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis Levinson -
Michael Sync wrote:
(Button)sender.Text
True. But I would say a line of check by having a call to
.GetType()
to prevent falling prey toSystem.InvalidCastException
.Vasudevan Deepak Kumar Personal Homepage
Tech Gossips
A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis LevinsonYeah.. You are right. Thanks.
Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)
-
how do I convert from object to control (the same as CType in VB)
private void button_click(object sender, EventArgs e) { MessageBox.Show(sender.Text); }
I think I should convert the sender to a button. in VB i don't have to it, it converts it automatically...Shimi
VB does this because it's rubbish. C# doesn't do this because it's strongly typed, one of the ways that it's NOT rubbish. The correct way to do this is to use the 'as' keyword to see if the object is indeed a control, but if you're sure it is, just cast it MessageBox.Show(((Control)sender).Text); that's what CType does.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
What about like that?
(Button)sender.Text
Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)
ummm....
Button button;
if(sender is Button)
{
button = (Button)sender;
}...granted, "Button" is actually System.Windows.Controls.Button or System.Web.UI.WebControls.Button.....or whatever button is contained in the namespace that you are using.
"I need build Skynet. Plz send code"
-
VB does this because it's rubbish. C# doesn't do this because it's strongly typed, one of the ways that it's NOT rubbish. The correct way to do this is to use the 'as' keyword to see if the object is indeed a control, but if you're sure it is, just cast it MessageBox.Show(((Control)sender).Text); that's what CType does.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
I don't think we should insult to VB developer..
Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)