如何在静态方法中引用非静态对象 [英] how to reference a non static object in a static method

查看:61
本文介绍了如何在静态方法中引用非静态对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个文本框,并希望在静态方法中引用它.我怎样才能做到这一点? 这是我的代码

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
    } 

推荐答案

您将需要以某种方式将其传递给静态方法,最简单的选择是仅扩展方法签名以接受文本框:

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
} 

这篇关于如何在静态方法中引用非静态对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆