开发者

Change label text in Static void C#

开发者 https://www.devze.com 2023-02-16 21:20 出处:网络
I want change the text of a label on one form to the text of a button on another form when I press the button.

I want change the text of a label on one form to the text of a button on another form when I press the button.

To do this I have created this on the form where the label is

public static void changeText(string text)
{
     L1.text = text;
}

this code is on the form with the button

mainForm.cha开发者_运维百科ngeText(this.Text);

It then gives the error : An object reference is required for the non-static field, method, or property...

This may seem like a stupid question but I am still new to C# so please help me.


About static and non-static members

There are two kinds of type members: non-static and static. Non-static members are also called instance members, because they appear in the object instances of the type. Static members are bound to the type itself, and not its object instances, so you can use them without actually instantiating the type.

Consider the following:

class MyClass
{
      // static member: can NOT reference 'this', as it is not in the context of an object instance of the type
      public static bool IsTrue()
      {
           return true;
      }

      // constructor: this runs whenever the type is instantiated
      public MyClass()
      {

      }

      // instance member: can access to 'this', which references the context object instance of the type
      public int GetNumber()
      {
           return 42;
      }
}

You can use the above type as following:

if(MyClass.IsTrue()) // static call
{
    var myObject = new MyClass(); // constructor call
    int result = myObject.GetNumber(); // instance member call
    Console.WriteLine(result);
}

On to your specific problem...

If you're perfectly sure that you need that logic inside a static method, you'll need to get an object instance of the form you wish to change. Unfortunately singletons don't work very well, because the VS designer needs to create an object instance of your Form, which obviously violates the singleton pattern.

What you can still use, is (in case of a Windows Forms application): Application.OpenForms. This returns a read-only collection that contains all currently open forms of the application. You can use this to find the object instance of the form you want to change, and then perform that change.

Be advised that if this is a multi-threaded situation (i.e. the static method runs in a different thread than the GUI thread), you'll have to use some sort of synchronization mechanism, such as InvokeRequired and Invoke().


L1 is not static, so you cant have a static function interacting with it. Having a static let you able to write something like MainForm.changeText(...), but in this case what is L1 ? I think we can say:

  1. You dont need a function to change the label text, the property Text is already written
  2. If there is some logic needed to mangle the tet before you can:

Consider if the function you need is so general that can apply to many labels across your app, in this case an extension method would be good. In other case if you want a function in the main form to set the text somewhere, and this place can change, or the text need some mangling, a member function would be good, and probably a DataBinding would be better.


You don't want to use a static method for this since L1 is a member of the mainForm class.


The error means your static function is accessing a non-static variable (control L1).

Static functions can only access to static variables. You can change L1 to static variable to make it work.

0

精彩评论

暂无评论...
验证码 换一张
取 消