| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1258 人关注过本帖
标题:值类型和引用类型
只看楼主 加入收藏
hl0071
Rank: 1
等 级:新手上路
帖 子:125
专家分:0
注 册:2005-11-11
收藏
得分:0 

下面是我以前发过的一个帖子,但是没弄明白,如下,不知道是不是和传值和传引用有关,就那个"判断是否已经选课"字段:




<<<<<
这两天在做一个选课系统的程序用SQL SERVER,在数据库里面有一个 " 判断是否已经选课" 的字段,在用户注册时就为之赋值为 "1",在学生选课后,会为之赋值 "2",然后在声明一个方法exit()来判断 "判断是否已经选课" 字段是否为"1", 来判断用户是否已经选课,选过课的就不能再选了,但是好像在学生选课后,方法好像检测到字段发生了改变,所以无论选没选过课,都不能再选了,不知道怎么样才能在选课让方法检测不到字段的改变,下面是我的代码,请高手帮忙啊,

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data ;
using System.Data .SqlClient ;

namespace WindowsApplication2
{
/// <summary>
/// Form5 的摘要说明。
/// </summary>
public class Form5 : System.Windows.Forms.Form
{
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.CheckBox checkBox2;
private System.Windows.Forms.CheckBox checkBox3;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.CheckBox checkBox4;
private System.Windows.Forms.CheckBox checkBox5;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
public int a=0;

private string resource()
{

string strbox="";
if(checkBox1.Checked )
{
a=a+1;
strbox+=checkBox1.Text+"\0";
}
else
{
if (strbox=="")
strbox="";
}
if(checkBox2.Checked )
{
a=a+1;
strbox+=checkBox2.Text+"\0";
}
else
{
if (strbox=="")
strbox="";
}
if(checkBox3.Checked )
{
a=a+1;
strbox+=checkBox3.Text+"\0";
}
else
{
if (strbox=="")
strbox="";
}
if(checkBox4.Checked )
{
a=a+1;
strbox+=checkBox4.Text+"\0";
}
else
{
if (strbox=="")
strbox="";
}
if(checkBox5.Checked )
{
a=a+1;
strbox+=checkBox5.Text+"\0";
}
else
{
if (strbox=="")
strbox="";
}

return strbox;

}
private bool exit()
{
SqlCommand cmd;
SqlDataReader dr;
Form1 form1=new Form1 ();
string strsql="data source= ; initial catalog=注册用户;user id=sa;password=1234;";
string str;
str="select * from 注册信息 where 帐号='"+form1.Userid +"'";
cmd=new SqlCommand();
cmd.Connection =new SqlConnection (strsql);
cmd.Connection .Open ();
cmd.CommandText =str;
dr=cmd.ExecuteReader();
dr.Read();

if(dr["判断是否已经选课"].ToString ()=="1")
{
return true;
cmd.Connection.Close ();
dr.Close ();
}
else
{
return false;
}


}


/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form5()
{
//
// 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.checkBox1 = new System.Windows.Forms.CheckBox();
this.checkBox2 = new System.Windows.Forms.CheckBox();
this.checkBox3 = new System.Windows.Forms.CheckBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.checkBox5 = new System.Windows.Forms.CheckBox();
this.checkBox4 = new System.Windows.Forms.CheckBox();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(40, 24);
this.checkBox1.Name = "checkBox1";
this.checkBox1.TabIndex = 0;
this.checkBox1.Text = "网页设计";
this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
//
// checkBox2
//
this.checkBox2.Location = new System.Drawing.Point(40, 72);
this.checkBox2.Name = "checkBox2";
this.checkBox2.TabIndex = 1;
this.checkBox2.Text = "ADCsee";
//
// checkBox3
//
this.checkBox3.Location = new System.Drawing.Point(40, 120);
this.checkBox3.Name = "checkBox3";
this.checkBox3.TabIndex = 2;
this.checkBox3.Text = "Flash";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.checkBox5);
this.groupBox1.Controls.Add(this.checkBox4);
this.groupBox1.Controls.Add(this.checkBox3);
this.groupBox1.Controls.Add(this.checkBox2);
this.groupBox1.Controls.Add(this.checkBox1);
this.groupBox1.Location = new System.Drawing.Point(112, 24);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(216, 264);
this.groupBox1.TabIndex = 3;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "请选择课程";
//
// checkBox5
//
this.checkBox5.Location = new System.Drawing.Point(40, 208);
this.checkBox5.Name = "checkBox5";
this.checkBox5.TabIndex = 4;
this.checkBox5.Text = "3Dmax";
//
// checkBox4
//
this.checkBox4.Location = new System.Drawing.Point(40, 160);
this.checkBox4.Name = "checkBox4";
this.checkBox4.TabIndex = 3;
this.checkBox4.Text = "photoshop";
//
// label1
//
this.label1.ForeColor = System.Drawing.Color.Red;
this.label1.Location = new System.Drawing.Point(344, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(96, 72);
this.label1.TabIndex = 4;
this.label1.Text = "注意!每位学生最多只能选择3门!!";
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 320);
this.button1.Name = "button1";
this.button1.TabIndex = 5;
this.button1.Text = "确定";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(264, 320);
this.button2.Name = "button2";
this.button2.TabIndex = 6;
this.button2.Text = "退出";
//
// Form5
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(568, 366);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Controls.Add(this.groupBox1);
this.Name = "Form5";
this.Text = "选课";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form5_Closing);
this.Load += new System.EventHandler(this.Form5_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
{

}

private void button1_Click(object sender, System.EventArgs e)
{
if(exit()==true)
{
SqlCommand cmd;
try
{


string strsql;
string str;
int row;
Form1 form1=new Form1 ();
strsql="data source= ; initial catalog=注册用户;user id=sa;password=1234;";

string str1 =resource();
if (a==0||a>3)
{
MessageBox.Show ("每位同学最多只能选三门课程","提示");
a=0;
return;
}
else
{

str="update 注册信息 set\n";
str+="所选择的课程='"+str1+"\n'";
str+="where 帐号='"+form1.Userid+"'" ;
}
cmd=new SqlCommand ( );
cmd.Connection =new SqlConnection (strsql);
cmd.Connection .Open ();
cmd.CommandText =str;
row=cmd.ExecuteNonQuery ();
if (row==1)
{
MessageBox.Show ("成功");
cmd.Connection .Close ();

}
else
{
MessageBox.Show ("失败");
return ;
}
}


catch(Exception ex)
{
MessageBox.Show (ex.Message );
}
}
else
{
MessageBox.Show ("你已经选过课程,不能再更改!","提示");
}
}

private void Form5_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
SqlCommand cmd;
string strsql;
string str;
Form1 form1=new Form1 ();
strsql="data source= ; initial catalog=注册用户;user id=sa;password=1234;";
str="update 注册信息 set\n";
str+="判断是否已经选课='"+2+"\n'";
str+="where 帐号='"+form1.Userid+"'" ;
cmd=new SqlCommand ( );
cmd.Connection =new SqlConnection (strsql);
cmd.Connection .Open ();
cmd.CommandText =str;
cmd.ExecuteNonQuery ();
cmd.Connection .Close ();

}

private void Form5_Load(object sender, System.EventArgs e)
{

}
}
}

>>>>>>>>>


一切都是快乐的
2006-07-19 13:21
noshow
Rank: 2
等 级:新手上路
威 望:4
帖 子:1127
专家分:0
注 册:2006-4-21
收藏
得分:0 

这两天在做一个选课系统的程序用SQL SERVER,
在数据库里面有一个 " 判断是否已经选课" 的字段,
在用户注册时就为之赋值为 "1",//初始化
在学生选课后,会为之赋值 "2",//判断后更新值
然后在声明一个方法exit()来判断//
"判断是否已经选课" 字段是否为"1",
来判断用户是否已经选课,
选过课的就不能再选了,//也就是说如果选过课的 那么就是说值为 2 就不能选了
但是好像在学生选课后,
方法好像检测到字段发生了改变,所以无论选没选过课,都不能再选了,//这里当然就不能选了呀!
不知道怎么样才能在选课让方法检测不到字段的改变,//?这句话我一直没理解
下面是我的代码,


首先,我觉得楼上的逻辑思路表达的不是很清楚
如果表达清楚了不用看那么多复杂的代码了
另外提醒楼上的注意
exit() 这个方法名最好不要用~

此号自封于2006年11月30日
2006-07-19 14:14
chenjin145
Rank: 1
等 级:禁止访问
帖 子:3922
专家分:0
注 册:2006-7-12
收藏
得分:0 
read判斷下有沒值 不然沒值的話讀取會拋出異常

return 後才寫這倆句?
cmd.Connection.Close ();
dr.Close ();

無論別人有沒選課 form5一關 就被你該為2了


[url=javascript:alert(1);] [div]fdgfdgfdg\" on\"[/div] [/url]
2006-07-19 14:19
hl0071
Rank: 1
等 级:新手上路
帖 子:125
专家分:0
注 册:2005-11-11
收藏
得分:0 

不好意思,由于我试了很多方法都不行,就把它放那了,所以很乱,那如果把Form5_Closing这个事件删除掉,然后在button1_Click事件中的
else
{
str="update 注册信息 set\n";
str+="所选择的课程='"+str1+"\n'";

后添加:
str+="判断是否已经选课='"+2.ToString+'\n'";
这样还是不行,结果一样.


一切都是快乐的
2006-07-19 15:28
hl0071
Rank: 1
等 级:新手上路
帖 子:125
专家分:0
注 册:2005-11-11
收藏
得分:0 

接12楼,是不是出在代码的执行有先后的问题上.


一切都是快乐的
2006-07-19 15:31
noshow
Rank: 2
等 级:新手上路
威 望:4
帖 子:1127
专家分:0
注 册:2006-4-21
收藏
得分:0 
楼上的 你的问题我看明白了
但是我没弄懂你这个功能的总体流程
可能我比较懒没没好好分析你的程序
你有空再介绍一下你这个程序设计思路吧~

此号自封于2006年11月30日
2006-07-20 08:49
chenjin145
Rank: 1
等 级:禁止访问
帖 子:3922
专家分:0
注 册:2006-7-12
收藏
得分:0 
建議重寫

[url=javascript:alert(1);] [div]fdgfdgfdg\" on\"[/div] [/url]
2006-07-20 09:24
快速回复:值类型和引用类型
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012843 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved