社区
C#
帖子详情
C#怎样定义和使用全局变量?
ZhuHai
2003-08-23 12:56:23
C#怎样定义和使用全局变量?
...全文
822
17
打赏
收藏
C#怎样定义和使用全局变量?
C#怎样定义和使用全局变量?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
17 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
greenhill1
2003-08-24
打赏
举报
回复
form1 中定义:public static int i;
form2 中调用:form1.i
HanJingJingHan
2003-08-24
打赏
举报
回复
OK
CBJ2049011
2003-08-24
打赏
举报
回复
已经没有全局函数和变量的概念了
你可以定义一个类,类中的变量和 方法都使用静态定义来解决你的问题
面向对象了!
spiketang
2003-08-23
打赏
举报
回复
然后用static 来说明变量!
如public static int(long) XXX;
spiketang
2003-08-23
打赏
举报
回复
C#本身是没有全局变量的,但是你可以封装一个类!
Stevetan81
2003-08-23
打赏
举报
回复
在 class级 设:
public static int X;
cppTrier
2003-08-23
打赏
举报
回复
C#是没有C++那样的全局变量的,任何东西都必定是包含于某一个类里的,但是用public static的变量,可以基本起到全局变量的作用。
cnhgj
2003-08-23
打赏
举报
回复
C#里没有全局变量,只有共享变量
public static string xx;
zhao_sh
2003-08-23
打赏
举报
回复
写一个类库,在这个类库中写一个类,类中的成员变量用static属性如:
public class GlobelTest
{
public static string aa;
public static int itest=10;
}
这样在其它地方就可用用类名直接引用它了
JCC0128
2003-08-23
打赏
举报
回复
同意雪狼
下面是我刚写的一个窗体单元代码,连接数据库的
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient ;
namespace AdoDemo2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.DataGrid dataGrid2;
private System.Windows.Forms.Button button2;
private SqlConnection Conn ;
private DataSet ds ;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.dataGrid2 = new System.Windows.Forms.DataGrid();
this.button2 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 8);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "ShowTable";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(24, 48);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(224, 224);
this.dataGrid1.TabIndex = 1;
//
// dataGrid2
//
this.dataGrid2.DataMember = "";
this.dataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid2.Location = new System.Drawing.Point(264, 48);
this.dataGrid2.Name = "dataGrid2";
this.dataGrid2.Size = new System.Drawing.Size(296, 224);
this.dataGrid2.TabIndex = 2;
//
// button2
//
this.button2.Location = new System.Drawing.Point(144, 8);
this.button2.Name = "button2";
this.button2.TabIndex = 3;
this.button2.Text = "commit";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(592, 349);
this.Controls.Add(this.button2);
this.Controls.Add(this.dataGrid2);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
Conn = DBServer.ConnectDB("JCC", "NorthWind","sa", "" ) ;
}
private void button1_Click(object sender, System.EventArgs e)
{
ds= new DataSet() ;
//建表aClass,字段
DataTable aClass = new DataTable("aClass") ;
DataColumn dc = aClass.Columns.Add("ClassID", typeof(string));
dc.Caption ="aaaID" ;
//aClass.Columns["ClassID"]. = "班级ID" ;
aClass.Columns.Add("ClassName", typeof(string));
aClass.Columns["ClassName"].Caption = "班级ID" ;
ds.Tables.Add( aClass) ;
//建表aStudent,字段
DataTable aStudent = new DataTable("aStudent" ) ;
aStudent.Columns.Add("StudentID", typeof(string));
aStudent.Columns["StudentID"].Caption = "学生ID" ;
aStudent.Columns.Add("StudentName", typeof(string));
aStudent.Columns["StudentName"].Caption = "学生姓名" ;
aStudent.Columns.Add("ClassID", typeof(string));
aStudent.Columns["ClassID"].Caption = "班级ID" ;
ds.Tables.Add( aStudent) ;
//建外键
ForeignKeyConstraint fk = new ForeignKeyConstraint(fk,
aClass.Columns["ClassID"], aStudent.Columns["ClassID"]) ;
fk.UpdateRule = Rule.Cascade ;
aClass.Constraints.Add(fk) ;
//填充表aClass
string CmdText = "SELECT ClassID, ClassName FROM aClass " ;
DBServer.FillDataSet(Conn, CmdText, aClass);
//dataGrid1.DataSource = aClass ;
//填充表aStudent
CmdText = " SELECT StudentID, StudentName , ClassID FROM aStudent";
DBServer.FillDataSet(Conn, CmdText, aStudent);
//dataGrid2.DataSource = aStudent ;
dataGrid1.SetDataBinding( aClass,"") ;
//this.BindingContext
}
private void button2_Click(object sender, System.EventArgs e)
{
}
}
public class DBServer
{
public static SqlConnection ConnectDB( string pstr_ServerName , string pstr_DBName ,
string pstr_UserName ,string pstr_Password)
{
string source ="server=" + pstr_ServerName +";"+
"uid = "+ pstr_UserName +";" +"pwd= "+pstr_Password+";"+
"database ="+pstr_DBName ;
return (new SqlConnection(source)) ;
}
public static void ExecSQLNonQuery( SqlConnection psc_Conn , string pstr_CmdText )
{
SqlCommand aSqlCommand = new SqlCommand( pstr_CmdText, psc_Conn );
aSqlCommand.ExecuteNonQuery() ;
}
public static object ExecSQLScalarQuery( SqlConnection psc_Conn , string pstr_CmdText )
{
SqlCommand aSqlCommand = new SqlCommand( pstr_CmdText, psc_Conn );
return (aSqlCommand.ExecuteScalar()) ;
}
public static void FillDataSet( SqlConnection psc_Conn, string pstr_CmdText , DataTable pdt_DataTable )
{
SqlDataAdapter Da = new SqlDataAdapter(pstr_CmdText, psc_Conn ) ;
Da.Fill(pdt_DataTable ) ;
}
}
}
qqq123
2003-08-23
打赏
举报
回复
public class GlobalValues
{
public static int Value1=0;
public static readonly int Value2=0;
public const int Value3=0;
//......
}
//使用方法
GlobalValues.Value1=1;
int k=GlobalValues.Value2;
int j=GlobalValues.Value3;
雪狼1234567
2003-08-23
打赏
举报
回复
定义成public static,
如public static string m;
m就是全局变量,在其它的模块里也可访问,不用new 一个这个类的实例
pretender1982
2003-08-23
打赏
举报
回复
同一二楼的
Nicholasqpl
2003-08-23
打赏
举报
回复
class AAA
{
JensiaTsang
2003-08-23
打赏
举报
回复
楼上的不错,然后创建对像,用对像来调用。
public class classname
{
...
}
classname AName=new classname();
write:AName.someStr:="xxx"
read: lStr:=AName.someStr
jiezhi
2003-08-23
打赏
举报
回复
public classs ...
{
private string s;
public string someStr
{
get{return s;}
set{s=value;}
}
}
shuilong
2003-08-23
打赏
举报
回复
c#里没有全局变量
怎么定义呢
Unity3D教程:成员变量和
全局变量
1
Unity3D教程:成员变量和
全局变量
在函数外面
定义
的变量叫做成员变量,它们能够通过Unity的检查器进行访问,存储在成员变量中的值将自动地保存在项目中。}
使用
C#
中static静态变量的用法实例
本文实例讲述了
C#
中static静态变量的用法。分享给大家供大家参考。具体如下:
使用
static 修饰符声明属于类型本身而不是属于特定对象的静态成员static修饰符可用于类、字段、方法、属性、运算符、事件和构造函数,但不能用于索引器、析构函数或类以外的类型 静态
全局变量
定义
:在
全局变量
前,加上关键字 static 该变量就被
定义
成为了一个静态
全局变量
。 特点: ① 、该变量在全局数据区分配内存。 ② 、初始化:如果不显式初始化,那么将被隐式初始化为0。 静态局部变量
定义
:在局部变量前加上static关键字时,就
定义
了静态局部变量。 特点: ① 、该变量在全局数据区分配内存。
C#
零基础入门课程
零基础
C#
入门课程旨在帮助编程小白从零开始掌握
C#
语言的基础知识和编程技能。课程主要包括以下几个方面:
C#
语言简介与开发环境搭建:介绍
C#
语言的特点、应用场景和优势,并教授如何搭建
C#
开发环境,选择和
使用
常用工具。
C#
基础语法:详细讲解
C#
的基本语法规则和代码结构,包括变量和数据类型的声明和
使用
、运算符的种类和应用、控制语句的编写等。面向对象编程:深入介绍面向对象编程的基本概念,如类、对象、继承、多态、封装和抽象等,并教授如何
定义
类和创建对象。常用类库和框架:介绍.NET Framework的强大功能和各种类库,以及如何在
C#
编程中利用这些类库和框架提升编程效率。此外,课程还会提供一些实践项目,让学员通过实际操作来巩固所学知识,提高编程能力。总之,零基础
C#
入门课程是一门全面、系统的
C#
编程入门教程,适合对
C#
编程感兴趣的初学者学习。通过本课程的学习,学员将掌握
C#
编程的基本知识和技能,为后续的进阶学习和项目开发打下坚实的基础。
c#
winform间传递参数的方法
C#
中没有了像VB.Net中的
全局变量
,那么我们如何实现在不同的页面间传递参数呢? 下面举例说明如何实现这一功能. 1.新建一个项目. 2.在该工程中添加一个窗体Form1. 3.在该窗体中
定义
静态型字符串变量myTestStr1: public static string myTestStr1=""; 4.在该窗体的构造函数中对该变量进行赋值,并为该窗体类添加属性GetStrValue.
C#
Form间的传值
C#
Form间传值: 因为不太熟悉窗体间传值,所以自己找了此资料,因为CSND上的很多前辈都发表了类似的贴子,我只是总结了一下,实现出来而已.
C#
Form间传值现在我所知道的大概分这么几种吧: 1)
定义
一个
全局变量
2)用属性来实现 3)用构造函数实现传值 4)用委托来实现 5)设置form控件的属性来实现 源码很短,都实现了基本传值,希望对困惑在Form传值的初学者们有帮助. 部分实现参照了[TJVictor的专栏]的一篇关于传值的文章.忘链接了,在此感谢TJVictor.
C#
110,534
社区成员
642,576
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章