Some Ideas about global variables (suitable for beginners C # Friends)

Source: Internet
Author: User

First of all, I am not a computer professional, but I only like to add work needs. Sometimes I write some smallProgramRecently, I encountered some minor problems when I used C # For a small program.

1. Global Variables

The global variable does not exist in C #, but it is still used in actual programs. Three solutions are considered.

A) declare a class, declare some static variables in the class, and use this method to save some "global variables ".CodeAs follows:

// Use this sysname to save the program name
Public   Static   String Sysname =   " Information System " ;

 

B) hashtable is used to save "global variables". hashtable has a one-to-one correspondence between key and value. I also use other methods for reference. data can be obtained on the form with the assigned value at that time, but cannot be obtained on other forms, but I do not know where the problem is, and the debugging has never been successful. please also discover the problem.

 

Code

Public   Static Hashtable globalvariable =   New Hashtable ();
Public   Static   Object Getvalue ( Object Akey)
{
Return Globalvariable [akey];
}
Public   Static   Void Setvalue ( Object Akey, Object Avalue)
{
Globalvariable [akey] = Avalue;
}
Public   Static   Void Remove ( Object Akey)
{
Globalvariable. Remove (akey );
}

 

 

C) The final solution is to use dictionary. After reading the introduction from many masters in the garden, I said that the efficiency is relatively high. But I didn't test it. I have limited strength and I will give you some advice! Paste the code. Please take a look.

 

Code

// Setvalue and getvalue are used to assign values and values. This scheme is passed during debugging, but I don't know if there are any vulnerabilities.
Public   Static Dictionary < Int , String > Globaldictionary =   New Dictionary < Int , String > ();

Public   Static   String Getvalue ( Int Tkey)
{
If (Globaldictionary. containskey (tkey ))
{
Return Globaldictionary [tkey];
}
Else
{
Return   " Error " ;
}

}
Public   Static   Int Setvalue ( Int Tkey, String Tstring)
{
If (Globaldictionary. containskey (tkey ))
{
Return   0 ;
}
Else
{
Globaldictionary. Add (tkey, tstring );
Return   1 ;
}
}

 

Please give me some advice!

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.