473,509 Members | 2,963 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert Type.

How do i convert an object variable to a Button one.
I have the following code in VB wich works fine:

Sub Button1Click(sender As Object, e As System.EventArgs)
Dim btn as Button = Convert.ChangeType(sender,GetType(Button))
MessageBox.Show(btn.Text)
End Sub

How can i reproduce the same behaivour in C#?
Thanks.
May 26 '06 #1
4 2229
Button button = (Button)sender;
MessageBox.Show(button.Text);

Technically you are not "converting" as the object is already referencing a
button type. Rather you are "casting" it.

--
Andrew Robinson
http://blog.binaryocean.com
"Anibal" <no****@sinbasura.com> wrote in message
news:O0**************@TK2MSFTNGP02.phx.gbl...
How do i convert an object variable to a Button one.
I have the following code in VB wich works fine:

Sub Button1Click(sender As Object, e As System.EventArgs)
Dim btn as Button = Convert.ChangeType(sender,GetType(Button))
MessageBox.Show(btn.Text)
End Sub

How can i reproduce the same behaivour in C#?
Thanks.

May 26 '06 #2
Anibal wrote:
How do i convert an object variable to a Button one.
I have the following code in VB wich works fine:

Sub Button1Click(sender As Object, e As System.EventArgs)
Dim btn as Button = Convert.ChangeType(sender,GetType(Button))
MessageBox.Show(btn.Text)
End Sub

How can i reproduce the same behaivour in C#?
Thanks.


I don't know VB well but I think casting is what you want.

public void Button1Click(Object sender, EventArgs e) {
Button btn = (Button) sender; // casting from Object to Button.
MessageBox.Show(btn.Text);
}

I'm hopped up on cough medicine, so I may be inaccurate here, but I
think this is what you're trying to do.

jeremiah
May 26 '06 #3
Thank you very much.
"Andrew Robinson" <ne****@nospam.nospam> escribió en el mensaje
news:ea**************@TK2MSFTNGP04.phx.gbl...
Button button = (Button)sender;
MessageBox.Show(button.Text);

Technically you are not "converting" as the object is already referencing
a button type. Rather you are "casting" it.

--
Andrew Robinson
http://blog.binaryocean.com
"Anibal" <no****@sinbasura.com> wrote in message
news:O0**************@TK2MSFTNGP02.phx.gbl...
How do i convert an object variable to a Button one.
I have the following code in VB wich works fine:

Sub Button1Click(sender As Object, e As System.EventArgs)
Dim btn as Button = Convert.ChangeType(sender,GetType(Button))
MessageBox.Show(btn.Text)
End Sub

How can i reproduce the same behaivour in C#?
Thanks.


May 26 '06 #4
Anibal wrote:
How do i convert an object variable to a Button one.
I have the following code in VB wich works fine:

Sub Button1Click(sender As Object, e As System.EventArgs)
Dim btn as Button = Convert.ChangeType(sender,GetType(Button))
MessageBox.Show(btn.Text)
End Sub

How can i reproduce the same behaivour in C#?
Thanks.

You can also use the 'as' operator.

Button b = sender as Button;

However, this will not throw an invalidcastexception, rather it will
return null if the cast fails.

JB
May 26 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
7246
by: Lauren Quantrell | last post by:
I have a stored procedure using Convert where the exact same Convert string works in the SELECT portion of the procedure but fails in the WHERE portion. The entire SP is listed below....
1
5630
by: phancey | last post by:
I am trying to invoke a web service method dynamically. I have created a generic function that takes a method name, string of parameters and calls the web method using System.Reflection: ...
4
3601
by: Eric Lilja | last post by:
Hello, I've made a templated class Option (a child of the abstract base class OptionBase) that stores an option name (in the form someoption=) and the value belonging to that option. The value is...
4
17664
by: Ken Varn | last post by:
I have an unknown numeric Type object passed into a function. I want to run a conversion on a string to convert the string to that Type object and return an object of that type. Is there some way...
7
30751
by: Jim Bancroft | last post by:
Hi everyone, A basic one here, I think. I haven't found the pattern yet, but sometimes when I cast a variable to another type using the "C" style cast operator the compiler refuses to play...
3
8723
by: mrajanikrishna | last post by:
Hi Friends, I am accepting a number from the user entered in a textbox. I want to assign to a variable in my code and assignt this to that variable. double num1 = (double)txtNum1.text; ...
7
16620
by: groups | last post by:
This is my first foray into writing a generic method and maybe I've bitten off more than I can chew. My intent is to have a generic method that accepts a value name and that value will be...
19
14747
by: tshad | last post by:
I have a value in my sql table set to tinyint (can't set to bit). I am trying to move it into a boolean field in my program and have tried: isTrue = (int)dbReader and isTrue =...
9
17494
by: Marco Nef | last post by:
Hi there I'm looking for a template class that converts the template argument to a string, so something like the following should work: Convert<float>::Get() == "float"; Convert<3>::Get() ==...
0
10707
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
7136
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7344
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4730
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.