using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace vote
{
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lbltitle;
protected System.Web.UI.WebControls.RadioButtonList RadioButtonList1;
protected System.Web.UI.WebControls.Button btnvote;
protected System.Web.UI.WebControls.Label lbl1;
protected System.Web.UI.WebControls.Label lbl2;
protected System.Web.UI.WebControls.Label lbl3;
protected System.Web.UI.WebControls.Label lbl4;
protected System.Web.UI.WebControls.Button btnresult;
int [] vote =new int[4];
float x=0;
private void Page_Load(object sender, System.EventArgs e)
{
this.vote=(int [])Application["b"];
// 在此处放置用户代码以初始化页面
} private void btnvote_Click(object sender, System.EventArgs e)
{
if(Session["a"]==null)
{
if(RadioButtonList1.SelectedIndex==-1)
{
Response.Write("<script langguage='javascript'>alert ('您没选中任何项');</script>");
}
if(this.RadioButtonList1.SelectedIndex==0)
{
vote[0]=vote[0]+1;
}
if(this.RadioButtonList1.SelectedIndex==1) {
vote[1]=vote[1]+1;
}
if(this.RadioButtonList1.SelectedIndex==2)
{
vote[2]=vote[2]+1;
}
if(this.RadioButtonList1.SelectedIndex==3)
{
vote[3]=vote[3]+1;
}
Application["b"]=vote;
Session["a"]=1;
}
else
{
Response.Write("<script language='javascript'>alert('您已经投过票了!!!');</script>");
}
} private void btnresult_Click(object sender, System.EventArgs e)
{
x=Convert.ToSingle(vote[0])+Convert.ToSingle(vote[1])+Convert.ToSingle(vote[2])+Convert.ToSingle(vote[3]);
double a,b,c,d;
a=Convert.ToSingle(vote[0])/x;
b=Convert.ToSingle(vote[1])/x;
c=Convert.ToSingle(vote[2])/x;
d=Convert.ToSingle(vote[3])/x;
this.lbl1.Text=(a*100).ToString()+"%";
this.lbl2.Text=(b*100).ToString()+"%";
this.lbl3.Text=(c*100).ToString()+"%";
this.lbl4.Text=(d*100).ToString()+"%";
this.lbl1.Visible=true;
this.lbl2.Visible=true;
this.lbl3.Visible=true;
this.lbl4.Visible=true;
}
}
} 做的是投票选举,投票是怎么会说"未将对象引用设置到对象的实例" vote[0]=vote[0]+1;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace vote
{
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lbltitle;
protected System.Web.UI.WebControls.RadioButtonList RadioButtonList1;
protected System.Web.UI.WebControls.Button btnvote;
protected System.Web.UI.WebControls.Label lbl1;
protected System.Web.UI.WebControls.Label lbl2;
protected System.Web.UI.WebControls.Label lbl3;
protected System.Web.UI.WebControls.Label lbl4;
protected System.Web.UI.WebControls.Button btnresult;
int [] vote =new int[4];
float x=0;
private void Page_Load(object sender, System.EventArgs e)
{
this.vote=(int [])Application["b"];
// 在此处放置用户代码以初始化页面
} private void btnvote_Click(object sender, System.EventArgs e)
{
if(Session["a"]==null)
{
if(RadioButtonList1.SelectedIndex==-1)
{
Response.Write("<script langguage='javascript'>alert ('您没选中任何项');</script>");
}
if(this.RadioButtonList1.SelectedIndex==0)
{
vote[0]=vote[0]+1;
}
if(this.RadioButtonList1.SelectedIndex==1) {
vote[1]=vote[1]+1;
}
if(this.RadioButtonList1.SelectedIndex==2)
{
vote[2]=vote[2]+1;
}
if(this.RadioButtonList1.SelectedIndex==3)
{
vote[3]=vote[3]+1;
}
Application["b"]=vote;
Session["a"]=1;
}
else
{
Response.Write("<script language='javascript'>alert('您已经投过票了!!!');</script>");
}
} private void btnresult_Click(object sender, System.EventArgs e)
{
x=Convert.ToSingle(vote[0])+Convert.ToSingle(vote[1])+Convert.ToSingle(vote[2])+Convert.ToSingle(vote[3]);
double a,b,c,d;
a=Convert.ToSingle(vote[0])/x;
b=Convert.ToSingle(vote[1])/x;
c=Convert.ToSingle(vote[2])/x;
d=Convert.ToSingle(vote[3])/x;
this.lbl1.Text=(a*100).ToString()+"%";
this.lbl2.Text=(b*100).ToString()+"%";
this.lbl3.Text=(c*100).ToString()+"%";
this.lbl4.Text=(d*100).ToString()+"%";
this.lbl1.Visible=true;
this.lbl2.Visible=true;
this.lbl3.Visible=true;
this.lbl4.Visible=true;
}
}
} 做的是投票选举,投票是怎么会说"未将对象引用设置到对象的实例" vote[0]=vote[0]+1;
if(this.RadioButtonList1.SelectedIndex==0)
{
vote[0]=vote[0]+1;
}
if(this.RadioButtonList1.SelectedIndex==1) {
vote[1]=vote[1]+1;
}
if(this.RadioButtonList1.SelectedIndex==2)
{
vote[2]=vote[2]+1;
}
if(this.RadioButtonList1.SelectedIndex==3)
{
vote[3]=vote[3]+1;
}
==
换成
int index = this.RadioButtonList1.SelectedIndex;
vote[index]++;
不更好?
把代码换成C#格式
便于观看[/align]