按书上说的,谁弄过??
我CLASS1
using System; using System.Data; using System.IO; using System.Xml;
namespace PollC { /// <summary> /// Class1 的摘要说明。 /// </summary> class Class1 { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main(string[] args) { Poll myPoll = Poll.Current(); //Console.WriteLine(myPoll.Question); DisplayPoll(myPoll); myPoll.Vote(2); Console.WriteLine("Vote registered against " + myPoll.Answers[2]); DisplayPoll(myPoll); // // TOD 在此处添加代码以启动应用程序 // } private static void DisplayPoll(Poll aPoll) { Console.WriteLine("========Poll========"); Console.WriteLine(aPoll.Question); for(int i=1;i<=(aPoll.AnswerCount);i++) { Console.WriteLine(aPoll.Answers[i] + ":"); Console.WriteLine(aPoll.Votes[i] + "votes"); } Console.WriteLine("===================="); } } }
创建了类Poll.cs
using System; using System.Data; using System.IO; using System.Xml;
namespace Polling { /// <summary> /// Poll 的摘要说明。 /// </summary> public class Poll { private int[] myVotes; private string[] myAnswers; private string myQuestion; private int myAnswerCount; private string myFilename;
public static Poll Current() { Poll myPoll = new Poll(@"C:\Inetpub\wwwroot\polls.xml"); return myPoll; } private Poll(string filename) { myFilename = filename; DataSet myDataSet = new DataSet(); FileStream fsReadXml = new FileStream(filename,FileMode.Open); XmlTextReader myXmlReader = new System.Xml.XmlTextReader(fsReadXml); myDataSet.ReadXml(myXmlReader); myXmlReader.Close(); myAnswerCount = myDataSet.Tables[1].Rows.Count; myQuestion = myDataSet.Tables[0].Rows[0].ItemArray[1].ToString(); myAnswers = new string[myAnswerCount+1]; string answer; for(int i=0;i<myAnswerCount;i++) { answer = myDataSet.Tables[1].Rows[i].ItemArray[0].ToString(); myAnswers[i+1]=answer; } myVotes = new int[myAnswerCount+1]; string votes; for(int i=0;i<myAnswerCount;i++) { votes = myDataSet.Tables[1].Rows[i].ItemArray[1].ToString(); myVotes[i+1] = int.Parse(votes); } } public void Vote(int answer) { if(answer == 0 || answer>myAnswerCount) { throw(new Exception("Invalid choice of answe.")); } myVotes[answer]++; DataSet myDataSet = new DataSet(); FileStream fsReadXml = new FileStream(myFilename,System.IO.FileMode.Open); XmlTextReader myXmlReader = new System.Xml.XmlTextReader(fsReadXml); myDataSet.ReadXml(myXmlReader); myXmlReader.Close(); string votes = myDataSet.Tables[1].Rows[answer-1].ItemArray[1].ToString(); int votesInt = int.Parse(votes); DataRow myVotesRow = myDataSet.Tables[1].Rows[answer-1]; myVotesRow["Votes"] = (votesInt+1).ToString(); StreamWriter myStream = new StreamWriter(myFilename); myDataSet.WriteXml(myStream,XmlWriteMode.IgnoreSchema); myStream.Close(); } public int AnswerCount { get { return myAnswerCount; } } public string Question { get { return myQuestion; } } public string[] Answers { get { return myAnswers; } } public int[] Votes { get { return myVotes; } } public Poll() { // // TOD 在此处添加构造函数逻辑 // } } }
运行的时候不能通过
提示:
C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\PollC\Class1.cs(29): 找不到类型或命名空间名称“Poll”(是否缺少 using 指令或程序集引用?)
谁懂的告诉一下~~~~~~~~