Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 4.0 aka C# 2010 > BOOK: Beginning Visual C# 2010
|
BOOK: Beginning Visual C# 2010
This is the forum to discuss the Wrox book Beginning Visual C# 2010 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, ; ISBN: 9780470502266
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2010 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 24th, 2012, 02:31 PM
Authorized User
 
Join Date: Jul 2012
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Default How do I go from generic 'object' to more specific object?

I have a TextBox named textbox_input which calls the following function when focus is lost:

Code:
private void textbox_input_Leave(object sender, EventArgs e) {
    textbox_debug.Text += "GetType: " + sender.GetType().ToString();
    textbox_debug.Text += "ToString: " + sender.ToString();
    //MyOtherObject.testFormControl (sender);
}
Typing "Hello" into the TextBox and leaving prints out:

Quote:
GetType: System.Windows.Forms.TextBox
ToString: System.Windows.Forms.TextBox, Text: Hello
Now what I would to do is call the (commented out) testFormControl method located in MyOtherObject. This method has the following prototype:

Code:
public void testFormControl (System.Windows.Forms.TextBox to_test)
However when I uncomment the line above I get the error:

Quote:
Argument 1: cannot convert from 'object' to 'System.Windows.Forms.TextBox'
So it seems to me that I have this generic 'object' type that references the actual TextBox object, how do I de-reference, if you will, so that my testFormControl() method can receive the actual TextBox object?
 
Old July 24th, 2012, 03:50 PM
Authorized User
 
Join Date: Jul 2012
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Default Found the answer

I found the answer to my problem. Seems like a screwy way of doing things but it works.

The fix looks like this:

Code:
private void textbox_input_Leave(object sender, EventArgs e) {
    textbox_debug.Text += "GetType: " + sender.GetType().ToString();
    textbox_debug.Text += "ToString: " + sender.ToString();
    if (sender is System.Windows.Forms.TextBox)
                MyOtherObject.testFormControl (sender as System.Windows.Forms.TextBox);
}
The if statement isn't really necessary since I know that it was a TextBox that got me there, but I added it for completeness of this solution. Obviously the textbox_debug.Text lines are just for my own debugging and have no place in your code. :)

Credit: http://stackoverflow.com/questions/1...-forms-textbox





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chap 14 - pg 517 - Item 9 (Object reference not set to an instance of an object. ) jkoyle BOOK: Beginning ASP.NET 4 : in C# and VB 5 August 25th, 2011 08:50 AM
Chap 13 pg448: Object reference not set to an instance of an object tomche BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 15 August 8th, 2009 06:14 AM
How do I connect to a specific object? DarrenMelling VB How-To 3 November 29th, 2006 09:53 AM
Serializing Object Graph - Assigning Object to Jag venkat.kl C# 0 August 28th, 2006 10:39 AM
create a Line object ,Box object in CR at Runtime? thanhnt Pro VB 6 1 May 16th, 2005 06:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.