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!