This application shows total females, males and total number of students from an XML document when the "Count" button is pressed. The problem is the program throws an error when the "Count" button is pressed and im not sure why?. Second, im not sure that all three totals are correct. I am very new to programming and would like some assistance if possible.
namespace DebugSession
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label lblTotal;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label lblMales;
private System.Windows.Forms.Label lbl;
private System.Windows.Forms.Button btnCount;
private System.Windows.Forms.Label lblFemale;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void label1_Click(object sender, System.EventArgs e)
{
}
private void btnCount_Click(object sender, System.EventArgs e)
{
XmlDocument studentData = null;
XmlNodeList studentList = null;
TotalClass totalClass = null;
string gender = "";
studentData = new XmlDocument();
studentData.LoadXml("<root>"
+ "<STUDNT ID=\"7\" Gender=\"M\"></STUDNT>"
+ "<STUDNT ID=\"16\" Gender=\"F\"></STUDNT>"
+ "<STUDNT ID=\"22\" Gender=\"F\"></STUDNT>"
+ "<STUDNT ID=\"25\" Gender=\"M\"></STUDNT>"
+ "<STUDNT ID=\"27\" Gender=\"F\"></STUDNT>"
+ "<STUDNT ID=\"32\" Gender=\"M\"></STUDNT>"
+ "<STUDNT ID=\"35\" Gender=\"f\"></STUDNT>"
+ "<STUDNT ID=\"45\" Gender=\"M\"></STUDNT>"
+ "<STUDNT ID=\"4423453244\" Gender=\"F\"></STUDNT>"
+ "<STUDNT ID=\"44344\" Gender=\"F\"></STUDNT>"
+ "</root>");
studentList = studentData.SelectNodes("//STUDENT");
if(studentList != null && studentList.Count > 0)
{
foreach(XmlElement student in studentList)
{
gender = student.GetAttribute("Gender");
switch(gender)
{
case "F":
totalClass.Females++;
break;
default:
case "M":
totalClass.Males++;
break;
}
}// end loop
}
this.lblMales.Text = totalClass.Males.ToString();
this.lblFemale.Text = totalClass.Females.ToString();
this.lblTotal.Text = (totalClass.Females + totalClass.Males).ToString();
}//btnCount_Click
}
}
I also have a TotalClass
using System;
namespace DebugSession
{
/// <summary>
/// Summary description for TotalClass.
/// </summary>
public class TotalClass
{
/// <summary>
/// Gets and Sets number of males
/// </summary>
public int Males
{
set{this.males = value;}
get{return this.males;}
}
private int males = -1;
/// <summary>
/// Gets and Sets number of females
/// </summary>
public int Females
{
set{this.females = value;}
get{return this.females;}
}
private int females = -1;
}//end class
}`