1

I've created a textbox and want to reference it in a static methd. how can I do that? here;s my code

    private void Form1_Load(object sender, EventArgs e)
    {
        TextBox textbox2 = new TextBox();
        textbox2.Text = "A";
    }

    static void gettext() 
    {
        textbox2.Text = "B"; //here is my problem
    } 
Daniel Imms
  • 45,529
  • 17
  • 143
  • 160
user1853846
  • 55
  • 1
  • 3
  • 9

5 Answers5

7

You would need to pass it into the static method somehow, easiest option is to just expand the method signature to accept the textbox:

static void gettext(TextBox textBox) 
{
    textBox.Text = "B"; //here is my problem
} 
Lloyd
  • 28,624
  • 4
  • 82
  • 95
  • @user1853846 Don't forget to accept the answer you feel is the best (in this case, Lloyd's) – Nolonar Mar 11 '13 at 10:58
  • 1
    @user1853846 In case you don't know how to accept answers, or why you should do so: http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Nolonar Jun 06 '13 at 15:06
4

You should give your textbox as a parameter to the static method

static void gettext(TextBox textbox)
{
    textbox.Text = "B";
}
Nolonar
  • 5,747
  • 3
  • 35
  • 54
2

I'm not sure you understand what static means, static means that it belongs to the CLASS not an INSTANCE of the class. Possibly a better solution to your problem would be to create an instance method which set the text.

// private variable
private TextBox textbox2;

private void Form1_Load(object sender, EventArgs e)
{
    // refers to private instance variable
    textbox2 = new TextBox();
    textbox2.Text = "A";
}

private void gettext() 
{
    // refers to private instance variable
    textbox2.Text = "B";
} 

If you're having difficulty understanding static, odds are you don't need to use it. Static members are available to all instances of a class but don't belong to any of them, which means static methods cannot access private members.

Daniel Imms
  • 45,529
  • 17
  • 143
  • 160
  • 1
    Why did this get downvoted? It doesn't make use of `static` methods, but as far as I can see, this is a totally valid answer, am I missing something? – Nolonar Mar 11 '13 at 10:47
  • 1
    also not sure why (it is not directly answering the explicit question, yes, but it certainly gives what is likely the best and most useful answer, in my mind) +1 – Kevin Versfeld Mar 11 '13 at 10:52
2

You can do so

static void gettext(TextBox textbox2) 
{
    textbox2.Text = "B";
} 

And in code

private void Form1_Load(object sender, EventArgs e)
{
    YourClass.gettext(textbox2);
}
Alex
  • 8,617
  • 3
  • 40
  • 57
-1

You can create a static variable set on Load :

private static readonly TextBox _textBox = new TextBox();

private void Form1_Load(object sender, EventArgs e)
{
    _textBox.Text = "A";
}

static void gettext()  
{ 
    _textbox2.Text = "B";
} 
Toto
  • 7,351
  • 18
  • 49
  • 70
  • The textBox is readonly and never reassigned, only the Text property. If you prefer, you remove the readonly and create the TextBox on the Load method. – Toto Mar 11 '13 at 13:08