全局变量的定义问题

zqsntws 2006-02-03 09:26:50
我想在用户登陆系统时纪录用户名,在别的窗体也可以使用该信息,不知定义在哪里合适?我是做了一个类来启动登陆窗体,如果合法就卸载该窗体,运行主窗体,该主窗体也是在这个类里实例化的。
...全文
324 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zqsntws 2006-02-03
  • 打赏
  • 举报
回复
我也不想用全局变量,可我这是cs系统,不是bs的,哪来的application呢
syeerzy 2006-02-03
  • 打赏
  • 举报
回复
首先建议不要用全局变量, 如果假设在一台机器上运行2个相同的用户或者多个用户同时进入系统,你一个不小心就出问题了.

如果你觉得肯定不会有多个用户同时进系统,那么用Application["...."]来保存你要的信息也可以,但是如果这些信息不是在大多数位置都是只读的,你维护起来可能会比较累.


除非没有好的方法,否则不要用全局变量,加大程序的耦合性.
我一直在寻找 2006-02-03
  • 打赏
  • 举报
回复
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication2
{
public class PublicValue
{
public static string UseName;
public static string UsePassword;
}

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

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.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(120, 88);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 24);
this.button1.TabIndex = 0;
this.button1.Text = "确定";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(29, 17);
this.label1.TabIndex = 1;
this.label1.Text = "名称";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(16, 48);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(29, 17);
this.label2.TabIndex = 2;
this.label2.Text = "密码";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(88, 16);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 3;
this.textBox1.Text = "name";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(88, 48);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 4;
this.textBox2.Text = "password";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
PublicValue.UseName=textBox1.Text;
PublicValue.UsePassword=textBox2.Text;
Form2 s;
s=new Form2();
s.Show();
}


}


/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form2()
{
//
// 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.textBox2 = new System.Windows.Forms.TextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 120);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 24);
this.button1.TabIndex = 0;
this.button1.Text = "提取";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(88, 56);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 8;
this.textBox2.Text = "";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(88, 24);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 7;
this.textBox1.Text = "";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(16, 56);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(29, 17);
this.label2.TabIndex = 6;
this.label2.Text = "密码";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(16, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(29, 17);
this.label1.TabIndex = 5;
this.label1.Text = "名称";
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);

}
#endregion

private void button1_Click(object sender, System.EventArgs e)
{
textBox1.Text=PublicValue.UseName ;
textBox2.Text=PublicValue.UsePassword;
}
}
}
wuyi8808 2006-02-03
  • 打赏
  • 举报
回复
Login类就是你说的启动类,Pub类是一个静态类,用来保存一些全局变量的,如果你的C#编译器是2.0版的,可在class Pub前面加上static关键字:

static class Pub // static关键字仅适用于C# 2.0
{
public static string UserName;
}
zqsntws 2006-02-03
  • 打赏
  • 举报
回复
wuyi8808(空间/IV):你这个Pub是启动类的名吗?Login类定义在哪?能写的详细些吗?多谢
wuyi8808 2006-02-03
  • 打赏
  • 举报
回复
class Pub
{
public static string UserName;
}

class Login : Form
{
TextBox userName;

void Log()
{
Pub.UserName = this.userName.Text;
}
}
hawk234 2006-02-03
  • 打赏
  • 举报
回复
帮up
marvelstack 2006-02-03
  • 打赏
  • 举报
回复
System.Windows.Forms.Application类。
当然,你也可以使用singleton模式来实现,
http://blog.csdn.net/zhzuo/archive/2004/07/16/42685.aspx

110,570

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧