Click here to Skip to main content
15,910,009 members
Please Sign up or sign in to vote.
2.60/5 (2 votes)
See more:
So i have a Textbox1 on Form1 and i am wanting to pass it into a class to be processed and then spouted back out into the form. I know it will have to be in a get and set, but i get confused what needs to go in get and set and what bit stays in the Form1 and what goes in the class. Any suggestions

P.S i think the TextBox1 will have to go in the set but not sure. I have had a look round online for this but no one has came up with a decent method for this.
Posted
Comments
BillWoodruff 9-Dec-13 14:48pm    
You need to describe the relationship between the Form that hosts the TextBox and the Class: is the Class in a different Namespace, in another application, etc.

Is the Class static ? If not, what creates the instance of the Class you need to expose the TextBox on the Form to ?

Why would you pass the textbox around, and not just the string that it contains? Let the UI do the work with the textbox, and let the classes take care of working directly with the data, not the UI.

Just pass the Textbox.Text to the class, not the entire textbox.
 
Share this answer
 
Comments
BillWoodruff 9-Dec-13 15:43pm    
imho it's clear in this case that there is a two way interaction, and the Text in the TextBox will be altered in whatever the Class is.
Sergey Alexandrovich Kryukov 9-Dec-13 16:09pm    
Sure. This is sometimes really hard to answer to the question demonstrating so low level of understanding, so, my 5.
—SA
What you will be passing to the class is not textbox itself but only reference to it.

If you want to pass it as class property, you can define property as shown below. I named it MyTextBox:

C#
TextBox m_TextBox;  //Local reference to passed TextBox
public TextBox MyTextBox
{
   set 
{
    m_TextBox = value;
}

private get 
{
    return m_TexBox;
}


}


Your original TextBox stays wherever it was. You just passed reference to it. In you class you have acess to all members of the original TextBox instance by using MyTextBox property getter.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900