Access non-static data in the static method)

Source: Internet
Author: User
If you create a new form with a lable, id = LBL added, and you want to use the method to change the value displayed by LBL, you can do this: Public   Void Changetext ()
{
LBL. Text="Changed";
}

When changetext () is called, LBL is displayed as "changed ".
If you want to change changetext () to a static method,Public Static VoidChangetext ()
{
LBL. Text="Changed";
}

It must not pass compilation,Because static methods and data are only related to the same type and are irrelevant to class instances, they exist before class instantiation. Static methods can only be static data in the category class or other static methods, other non-static data and methods cannot be accessed. The reason is very simple. The static method does not know the memory address of the instance Member, so it cannot be accessed.
Is there any way to access instance members through static methods? For example, in the above method, use the static method to access the instance's lable?
The answer is yes.
The reason why the static method cannot be used to access instance members is that they do not know the memory address of the instance members. If they do, they can access the instance members.
For example: Public   Class Form1: system. Windows. Forms. Form
{
Private System. Windows. Forms. Label label1;
Private System. Windows. Forms. Button;
Private   Static   Void Mychange (form1 FM)
{
FM. label1.text="Changed";
}
Private   Void Button_click ( Object Sender, system. eventargs E)
{
Mychange (This);
}

}

Call the mychange method and pass it to its reference form to achieve the goal.

I remember someone asked csdn a question: Create a subclass and declare the class as static in the main class, such as private static someclass = new someclass ();
Then, use the built-in method of someclass to change statusbar. Text in the main class. The above method should be fine. Public   Class Someclass
{
Private Form1 FM;
Public Form1 FM
{
Set{FM=Value ;}
}
Public   Void Change ()
{
FM. statusbar. Text="Changed";
}
}

In the main class:

Public   Class Form1: system. Windows. Forms. Form
{
Private   Static Someclass = New Someclass ();
Private   Void Button_click ( Object Sender, system. eventargs E)
{
Someclass. fm=This;
Someclass. Change ();

}

}

If you point the value of statusbar. Text to a static string,
Public static string stext = NULL;
Bar. Text = stext;
You can also change bar. Text by updating the stext value, but you need to refresh bar. Text = stext from time to time. It is better than the above method.

The value of this method can be used in the following situations:
A form is created with vs, and the form has a ListBox to display information. For the main class, you can create the following methods: Private ListBox Lb;
// .
Private   Void Showmessage ( String Message)
{
LB. Items. Add (Message );
LB. selectedindex=LB. Items. Count-1;
}

For other classes, use
Throw new exception (something );
Suitable.
If it is created in the Form Project and the error information is displayed in ListBox, is it more user-friendly? The error information should be displayed in a non-form1 class to ListBox, you can pass the reference of ListBox with attributes and create showmessage (). This is feasible, but the same showmessage () must be created twice. It does not conform to the programming principles and should not be repeated. Code You can create a static method in form1: /**/ ///   <Summary>
/// Display Information, static method, convenient for other classes
///   </Summary>
///   <Param name = "FM"> </param>
///   <Param name = "message"> </param>
Public   Static   Void Showmessage (form1 FM, String Message)
{
FM. lb. Items. Add (Message );
FM. lb. selectedindex=FM. lb. Items. Count-1;
}

This is convenient for other classes. To change the information related to ListBox, you can modify showmessage () only in form1.

If you are benevolent or wise, express your comments.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.