×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

Create a public class and declare a public static variable in it, see demo code inside.

Method 1.

Step 1, create a public class and a public static variable in the class. Which is tableName .

using System;

namespace global
{
public class global_class
{
public static string tableName="Demo";
public global_class()
{
//
// TODO: Add constructor logic here
//
}
}
}


In other winform/class files, call the variable like below:

global_class.tableName

You don't even need to create an instance of the "global_class" class.

Method 2,
If you are developing ASP.Net, you can put the global variables inside web.config file.


Happy programming...
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / help:在C# 有没有全局数据变量的概念? 我希望在各个窗体中都能使用到公共的全局变量,就好比在VB 里使用MODULE 一样,定义一个PUBLIC 的变量,那么在各个窗体中都能使用到?
    • One way is to create a base form which has public static variables, then all your forms are derived from your base form instead of Windows Form. HTH, Victor
      • I don't think that is a solution.
    • global variable -- can be accessed by all applications on the server; application variable -- can be accessed by all forms in the application; session variable -- can be accessed by all pages in one session.
    • Create a public class and declare a public static variable in it, see demo code inside.
      Method 1.

      Step 1, create a public class and a public static variable in the class. Which is tableName .

      using System;

      namespace global
      {
      public class global_class
      {
      public static string tableName="Demo";
      public global_class()
      {
      //
      // TODO: Add constructor logic here
      //
      }
      }
      }


      In other winform/class files, call the variable like below:

      global_class.tableName

      You don't even need to create an instance of the "global_class" class.

      Method 2,
      If you are developing ASP.Net, you can put the global variables inside web.config file.


      Happy programming...
    • do not use namespace, the class which is defined as public will be global. Of course, you can declare varables in the class. Variables in the class are global as well.