一个对象引用所需的非静态字段,方法或属性“成员” [英] An object reference is required for the nonstatic field, method, or property 'member'

查看:139
本文介绍了一个对象引用所需的非静态字段,方法或属性“成员”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

`我想换一个静态方法的文本框的文本。我怎么能做到这一点,考虑到我不能在静态方法使用本关键字。换句话说,我怎样才能使一个物体refrence到一个文本框text属性?



这是我的代码



 公共部分Form1类:表格
{
公共Form1中()
{
的InitializeComponent();
}

公众委托无效一个MyEventHandler(字符串为newValue);


公共类EventExample
{
私人字符串thevalue;

公共事件的ValueChanged一个MyEventHandler;

公共字符串VAL
{

{
this.thevalue =价值;
this.valuechanged(thevalue);

}

}

}

静态无效FUNC(字串[] args)
{
EventExample myevt =新EventExample();
myevt.valuechanged + =新一个MyEventHandler(最后);
myevt.val =结果;
}

公共委托串buttonget(INT X);



公共静态buttonget intostring =厂;

公共静态字符串厂(INT X)
{

串研究所= x.ToString();
返回研究所;

}

公共静态字符串结果= intostring(1);

静态无效的最后一个(字符串NEWVALUE)
{
Form1.textBox1.Text =结果; //这里是它说,它需要一个对象引用
}



私人无效的button1_Click问题(对象发件人,EventArgs五)
$ { b $ b intostring(1);

}`


解决方案

如果你想从一个静态方法内部改变的非静态对象的属性,则需要在几种方法之一获取对对象的引用:




  • 的对象传递给你的方法作为参数 - 这一个是最常见的。对象是你的方法的参数,并调用它

  • 方法/设置属性
  • 的对象是在静态字段设置 - 这一个是OK单线程程序,但是当你处理并发

  • 很容易出错
  • 对象可通过一个静态参考 - 这是第二的推广方法:对象可能是一个的的,您可能正在运行从中通过名称或一些其它ID等获得对象的静态注册表



在任何情况下,你的静态方法必须以考察其非静态的属性或调用其非静态方法获取对象的引用。


`I want to change a textbox text in a static method. how can I do that, considering that i cannot use "this" keyword in a static method. In another words, how can I make an object refrence to a the textbox text property?

this is my code

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public delegate void myeventhandler(string newValue);


    public class EventExample
    {
        private string thevalue;

        public event myeventhandler valuechanged;

        public string val
        {
            set
            {
                this.thevalue = value;
                this.valuechanged(thevalue);

            }

        }

    }

        static void func(string[] args)
        {
            EventExample myevt = new EventExample();
            myevt.valuechanged += new myeventhandler(last);
            myevt.val = result;
        }

  public  delegate string buttonget(int x);



    public static buttonget intostring = factory;

    public static string factory(int x)
    {

        string inst = x.ToString();
        return inst;

    }

  public static  string result = intostring(1);

  static void last(string newvalue)
  {
     Form1.textBox1.Text = result; // here is the problem it says it needs an object reference
  }



    private void button1_Click(object sender, EventArgs e)
    {
        intostring(1);

    }`

解决方案

If you want to change an attribute of a non-static object from inside a static method, you need to obtain a reference to that object in one of several ways:

  • The object is passed to your method as an argument - This one is most common. The object is an argument of your method, and you call methods/set properties on it
  • The object is set in a static field - This one is OK for single-threaded programs, but it is error-prone when you deal with concurrency
  • The object is available through a static reference - This is a generalization of the second way: the object may be a singleton, you may be running a static registry from which you get an object by name or some other ID, etc.

In any case, your static method must get a reference to the object in order to examine its non-static properties or call its non-static methods.

这篇关于一个对象引用所需的非静态字段,方法或属性“成员”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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