In C #, only one class instance can be generated.

Source: Internet
Author: User

Sometimes we encounter this situation: in a form, we click a button to generate a new form.CodeIf you click the button again, a form is generated. For each form, an instance of the form corresponds to it. If you wantProgramCan generate only one class instance? In fact, the above implementation can not generate a new form is relatively simple implementation, you can set the button attribute to unavailable after the form is generated: button1.enable = false;

However, what I want to achieve here is that you do not need to make the button unavailable to complete the method that the program can only generate one class instance:

Method 1:

If you only want one instance to operate on a class, you can define all the fields, attributes, and functions in the class as static, and define the constructors as private, in this way, only the class name can access the fields, attributes, and methods in the class, and the class cannot create an instance outside the class.

Public   Class Class2

{

Private Class2 () {}  

Public   Static Type variablename;

Public   Static Type functionname () {}  

}  


This method is not suitable for the form class in the winform program.

Method 2:

Set a Boolean variable to identify whether an instance has been created.

Public   Class Class2

{

Private Class2 ()

{

}

PrivateStatic  Bool Instance_flag = False ;

Public   Static Class2 getinstance ()

{

If ( ! Instance_flag)

{

Class2 C2=NewClass2 ();

Instance_flag=True;

ReturnC2;

}

Else

{

Console. writeline ("You have already create a instance");

Return Null;

}


}

}

If you have created an instanceInstance_flag = true, so that the program will know that an instance has been created and no new instance will be created.

Method 3:

Create a class instance in class2. The instance is static, and then assign values directly to the instance outside the class. You can also return the class instance through a function. However, this class instance will be generated during compilation. It will waste resources and the efficiency is not high. It can be generated only when instances are needed, the Code is as follows:

Public   Class Class2

{

Private Class2 ()

{

}

Private   Static Class2 C2 = Null ;

Public   Static Class2 getinstance ()

{

If (C2 = Null )

{

C2=NewClass2 ();

}

Return C2;

}

}


The static method can only access static domains. According to the above code, you can use the class name to call the getinstance () method when you need to use an instance. To return an instance.

The constructors cannot be used in the preceding methods, because they are all set to private, so is the purpose. Multiple instance generation is prohibited. Thank you!

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.