I prefer to type the entire code so you can see the deeper level of how I proceeded, and where the error is. You can implement the code in your Visual Studio (for instance) and see quickly
The problem is that in the Mainform class i have an UpdataGuru method:
private void UpdateGUI()
{
string[] strSeatInfoStrings = new String[15];
lstReservations.Items.Clear();
m_seatMngr.GetSeatInfoStrings((SeatManager.DisplayOptions)cmbDisplayOption.SelectedIndex, out strSeatInfoStrings);
lstReservations.Items.AddRange(strSeatInfoStrings);
lblNumOfReservedSeats.Text = "" + m_seatMngr.GetNumReserved();
lblNumberOfVacant.Text = "" + m_seatMngr.GetNumVacant();
}
And right where the code is "lstReservations.Items.AddRange(strSeatInfoStrings);"
I get an error saying "ArgumentNullException was unhandled" i cant fix it and really need help so I copy the code to you who are more intelligent and more knowledgeable programmer. because I am pretty new to this stuff
Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Assignment4
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
MainForm.Designer
namespace Assignment4
{
partial class MainForm
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.btnOK = new System.Windows.Forms.Button();
this.lab1 = new System.Windows.Forms.Label();
this.lab2 = new System.Windows.Forms.Label();
this.lab4 = new System.Windows.Forms.Label();
this.rbtnReserve = new System.Windows.Forms.RadioButton();
this.rbtnCancel = new System.Windows.Forms.RadioButton();
this.grpInput = new System.Windows.Forms.GroupBox();
this.lblPrice = new System.Windows.Forms.Label();
this.lblName = new System.Windows.Forms.Label();
this.txtPrice = new System.Windows.Forms.TextBox();
this.txtName = new System.Windows.Forms.TextBox();
this.grpOutput = new System.Windows.Forms.GroupBox();
this.lblNumberOfVacant = new System.Windows.Forms.Label();
this.lblNumOfReservedSeats = new System.Windows.Forms.Label();
this.lblNumOfSeats = new System.Windows.Forms.Label();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.cmbDisplayOption = new System.Windows.Forms.ComboBox();
this.lstReservations = new System.Windows.Forms.ListBox();
this.grpInput.SuspendLayout();
this.grpOutput.SuspendLayout();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
this.btnOK.Location = new System.Drawing.Point(27, 91);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(122, 26);
this.btnOK.TabIndex = 0;
this.btnOK.Text = "Reserv / Cancel";
this.btnOK.UseVisualStyleBackColor = true;
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
this.lab1.AutoSize = true;
this.lab1.Cursor = System.Windows.Forms.Cursors.No;
this.lab1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.lab1.Location = new System.Drawing.Point(24, 170);
this.lab1.Name = "lab1";
this.lab1.Size = new System.Drawing.Size(89, 13);
this.lab1.TabIndex = 1;
this.lab1.Text = "Number of Seats:";
this.lab2.AutoSize = true;
this.lab2.Location = new System.Drawing.Point(24, 196);
this.lab2.Name = "lab2";
this.lab2.Size = new System.Drawing.Size(108, 13);
this.lab2.TabIndex = 1;
this.lab2.Text = "Number of Reserved:";
this.lab4.AutoSize = true;
this.lab4.Location = new System.Drawing.Point(24, 223);
this.lab4.Name = "lab4";
this.lab4.Size = new System.Drawing.Size(96, 13);
this.lab4.TabIndex = 1;
this.lab4.Text = "Number of Vacant:";
this.rbtnReserve.AutoSize = true;
this.rbtnReserve.Location = new System.Drawing.Point(15, 14);
this.rbtnReserve.Name = "rbtnReserve";
this.rbtnReserve.Size = new System.Drawing.Size(65, 17);
this.rbtnReserve.TabIndex = 2;
this.rbtnReserve.TabStop = true;
this.rbtnReserve.Text = "Reserve";
this.rbtnReserve.UseVisualStyleBackColor = true;
this.rbtnCancel.AutoSize = true;
this.rbtnCancel.Location = new System.Drawing.Point(108, 14);
this.rbtnCancel.Name = "rbtnCancel";
this.rbtnCancel.Size = new System.Drawing.Size(98, 17);
this.rbtnCancel.TabIndex = 2;
this.rbtnCancel.TabStop = true;
this.rbtnCancel.Text = "Cancel Reserv.";
this.rbtnCancel.UseVisualStyleBackColor = true;
this.grpInput.Controls.Add(this.lblPrice);
this.grpInput.Controls.Add(this.lblName);
this.grpInput.Controls.Add(this.txtPrice);
this.grpInput.Controls.Add(this.rbtnReserve);
this.grpInput.Controls.Add(this.txtName);
this.grpInput.Controls.Add(this.rbtnCancel);
this.grpInput.Location = new System.Drawing.Point(12, 7);
this.grpInput.Name = "grpInput";
this.grpInput.Size = new System.Drawing.Size(212, 121);
this.grpInput.TabIndex = 4;
this.grpInput.TabStop = false;
this.grpInput.Text = "Booking Input";
this.lblPrice.AutoSize = true;
this.lblPrice.Location = new System.Drawing.Point(159, 42);
this.lblPrice.Name = "lblPrice";
this.lblPrice.Size = new System.Drawing.Size(31, 13);
this.lblPrice.TabIndex = 5;
this.lblPrice.Text = "Price";
this.lblName.AutoSize = true;
this.lblName.Location = new System.Drawing.Point(55, 42);
this.lblName.Name = "lblName";
this.lblName.Size = new System.Drawing.Size(35, 13);
this.lblName.TabIndex = 4;
this.lblName.Text = "Name";
this.txtPrice.Location = new System.Drawing.Point(150, 58);
this.txtPrice.Name = "txtPrice";
this.txtPrice.Size = new System.Drawing.Size(56, 20);
this.txtPrice.TabIndex = 3;
this.txtPrice.TabStop = false;
this.txtName.Location = new System.Drawing.Point(15, 58);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(122, 20);
this.txtName.TabIndex = 3;
this.grpOutput.Controls.Add(this.lblNumberOfVacant);
this.grpOutput.Controls.Add(this.lblNumOfReservedSeats);
this.grpOutput.Controls.Add(this.lblNumOfSeats);
this.grpOutput.Location = new System.Drawing.Point(12, 149);
this.grpOutput.Name = "grpOutput";
this.grpOutput.Size = new System.Drawing.Size(212, 151);
this.grpOutput.TabIndex = 4;
this.grpOutput.TabStop = false;
this.grpOutput.Text = "Output Data";
this.lblNumberOfVacant.AutoSize = true;
this.lblNumberOfVacant.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblNumberOfVacant.Location = new System.Drawing.Point(150, 74);
this.lblNumberOfVacant.Name = "lblNumberOfVacant";
this.lblNumberOfVacant.Size = new System.Drawing.Size(27, 15);
this.lblNumberOfVacant.TabIndex = 2;
this.lblNumberOfVacant.Text = "240";
this.lblNumOfReservedSeats.AutoSize = true;
this.lblNumOfReservedSeats.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblNumOfReservedSeats.Location = new System.Drawing.Point(150, 47);
this.lblNumOfReservedSeats.Name = "lblNumOfReservedSeats";
this.lblNumOfReservedSeats.Size = new System.Drawing.Size(27, 15);
this.lblNumOfReservedSeats.TabIndex = 1;
this.lblNumOfReservedSeats.Text = " ";
this.lblNumOfSeats.AutoSize = true;
this.lblNumOfSeats.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblNumOfSeats.Location = new System.Drawing.Point(150, 20);
this.lblNumOfSeats.Name = "lblNumOfSeats";
this.lblNumOfSeats.Size = new System.Drawing.Size(27, 15);
this.lblNumOfSeats.TabIndex = 0;
this.lblNumOfSeats.Text = "240";
this.groupBox1.Controls.Add(this.cmbDisplayOption);
this.groupBox1.Controls.Add(this.lstReservations);
this.groupBox1.Location = new System.Drawing.Point(246, 22);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(360, 270);
this.groupBox1.TabIndex = 4;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Booking Input";
this.cmbDisplayOption.AccessibleDescription = "";
this.cmbDisplayOption.AccessibleName = "";
this.cmbDisplayOption.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmbDisplayOption.FormattingEnabled = true;
this.cmbDisplayOption.Location = new System.Drawing.Point(233, 19);
this.cmbDisplayOption.Name = "cmbDisplayOption";
this.cmbDisplayOption.Size = new System.Drawing.Size(121, 21);
this.cmbDisplayOption.TabIndex = 1;
this.cmbDisplayOption.Tag = "";
this.cmbDisplayOption.SelectedIndexChanged += new System.EventHandler(this.cmbDisplayOption_SelectedIndexChanged);
this.lstReservations.FormattingEnabled = true;
this.lstReservations.Items.AddRange(new object[] {
"lstReservations"});
this.lstReservations.Location = new System.Drawing.Point(6, 53);
this.lstReservations.Name = "lstReservations";
this.lstReservations.Size = new System.Drawing.Size(354, 212);
this.lstReservations.TabIndex = 0;
this.lstReservations.SelectedIndexChanged += new System.EventHandler(this.lbSeats_SelectedIndexChanged);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(618, 304);
this.Controls.Add(this.lab4);
this.Controls.Add(this.lab2);
this.Controls.Add(this.lab1);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.grpOutput);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.grpInput);
this.Name = "MainForm";
this.Text = "CBC Cinema Booking System";
this.Load += new System.EventHandler(this.MainForm_Load);
this.grpInput.ResumeLayout(false);
this.grpInput.PerformLayout();
this.grpOutput.ResumeLayout(false);
this.grpOutput.PerformLayout();
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.Label lab2;
private System.Windows.Forms.Label lab4;
private System.Windows.Forms.RadioButton rbtnReserve;
private System.Windows.Forms.RadioButton rbtnCancel;
private System.Windows.Forms.GroupBox grpInput;
private System.Windows.Forms.GroupBox grpOutput;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox txtPrice;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.Label lblPrice;
private System.Windows.Forms.Label lblName;
private System.Windows.Forms.Label lab1;
private System.Windows.Forms.Label lblNumOfSeats;
private System.Windows.Forms.Label lblNumberOfVacant;
private System.Windows.Forms.Label lblNumOfReservedSeats;
private System.Windows.Forms.ListBox lstReservations;
private System.Windows.Forms.ComboBox cmbDisplayOption;
}
}
MainForm
using System;
using System.Windows.Forms;
namespace Assignment4
{
public partial class MainForm : Form
{
private const int m_numOfSeats = 60;
private SeatManager m_seatMngr;
public MainForm()
{
InitializeComponent();
m_seatMngr = new SeatManager(m_numOfSeats);
InitializeGUI();
}
private void InitializeGUI()
{
cmbDisplayOption.Items.Add("All Seats");
cmbDisplayOption.Items.Add("Vacant Seat");
cmbDisplayOption.Items.Add("Reserved Seat");
cmbDisplayOption.SelectedIndex = 0;
UpdateGUI();
rbtnReserve.Checked = true;
lstReservations.Items.Clear();
txtName.Text = string.Empty;
}
private bool CheckSelectedIndex()
{
for (int i = 0; i < lstReservations.Items.Count; i++)
lstReservations.SelectedIndex = i;
if (lstReservations.SelectedIndex < 0)
{
MessageBox.Show("Item must be selected");
lstReservations.SelectedIndex = -1;
return false;
}
else
{
return true;
}
}
private bool ReadAndValidateName(out string name)
{
name = txtName.Text;
if (string.IsNullOrEmpty(name))
{
MessageBox.Show("There is no name value");
txtName.Focus();
return false;
}
else
{
return true;
}
}
private bool ReadAndValidatePrice(out double price)
{
int minValue = 0;
const int maxValue = 1000;
price = Double.Parse(txtPrice.Text);
if(price < minValue)
{
MessageBox.Show("The value in the price box is under 0");
txtPrice.Focus();
return false;
}
else if (price > maxValue)
{
MessageBox.Show("The value in the price box is over max limit");
txtPrice.Focus();
return false;
}
else
return true;
}
private bool ReadAndValidateInput(out string name, out double price)
{
ReadAndValidateName(out name);
ReadAndValidatePrice(out price);
if (ReadAndValidateName(out name) && ReadAndValidatePrice(out price))
{
ReadAndValidateName(out name);
ReadAndValidatePrice(out price);
return true;
}
else
return false;
}
private void ReserveOrCancelSeat(SeatManager.DisplayOptions choice)
{
if (CheckSelectedIndex() == false)
{
MessageBox.Show("a seat must be selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (rbtnReserve.Checked == true)
{
string name = null;
double price = 0.0;
if (ReadAndValidateInput(out name, out price) == true)
{
if (!m_seatMngr.ReserveSeat(name, price, lstReservations.SelectedIndex))
{
if (MessageBox.Show("The seat is already reserved, continue with reservation?", "Seat already reserved", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
if (!m_seatMngr.CancelSeat(lstReservations.SelectedIndex))
{
throw new Exception("The seat could not be canceled.");
}
if (!m_seatMngr.ReserveSeat(name, price, lstReservations.SelectedIndex))
{
throw new Exception("The seat could not be reserved.");
}
}
}
}
}
}
private void UpdateGUI()
{
string[] strSeatInfoStrings = new String[15];
lstReservations.Items.Clear();
m_seatMngr.GetSeatInfoStrings((SeatManager.DisplayOptions)cmbDisplayOption.SelectedIndex, out strSeatInfoStrings);
lstReservations.Items.AddRange(strSeatInfoStrings);
lblNumOfReservedSeats.Text = "" + m_seatMngr.GetNumReserved();
lblNumberOfVacant.Text = "" + m_seatMngr.GetNumVacant();
}
private void cmbDisplayOption_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateGUI();
}
private void lbSeats_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void MainForm_Load(object sender, EventArgs e)
{
}
private void btnOK_Click(object sender, EventArgs e)
{
ReserveOrCancelSeat(SeatManager.DisplayOptions.ReservedSeats);
}
}
}
SeatManager:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Assignment4
{
class SeatManager
{
private readonly int m_totNumOfSeats;
private double[] m_priceList = new double[0];
private string[] m_nameList = new string[0];
public enum DisplayOptions
{
AllSeats,
VacantSeats,
ReservedSeats
}
public SeatManager(int maxNumberofSeats)
{
m_totNumOfSeats = maxNumberofSeats;
m_nameList = new string[m_totNumOfSeats];
m_priceList = new double[m_totNumOfSeats];
}
private bool CheckIndex(int index)
{
if (index >= 0 && index < m_totNumOfSeats)
{
return true;
}
else
{
return false;
}
}
public int GetNumReserved()
{
int numOfResv = 0;
foreach (string seat in m_nameList)
{
if (seat != null)
numOfResv++;
}
return numOfResv;
}
public int GetNumVacant()
{
int notReserved = 60;
foreach (string seat in m_nameList)
{
if (seat != null)
notReserved--;
}
return notReserved;
}
public int GetNumOfSeats(DisplayOptions choice)
{
if (choice == DisplayOptions.ReservedSeats)
{
return GetNumReserved();
}
if (choice == DisplayOptions.VacantSeats)
{
return GetNumVacant();
}
else
{
return m_totNumOfSeats;
}
}
public bool ReserveSeat(String name, double price, int index)
{
if (m_nameList[index] == null)
{
m_nameList[index] = name;
m_priceList[index] = price;
return true;
}
else
{
return false;
}
}
public bool CancelSeat(int index)
{
if (m_nameList[index] == null)
{
return false;
}
else
{
m_nameList[index] = string.Empty;
m_priceList[index] = 0.0;
return true;
}
}
public string GetSeatInfoAt(int index)
{
for (index = 0; index < m_nameList.Length; index++)
{
return m_nameList[index];
}
return m_nameList[index];
}
public int GetSeatInfoStrings(DisplayOptions choice, out string[] strSeatInfoStrings)
{
strSeatInfoStrings = null;
int count = GetNumOfSeats(choice);
if ((count <= 0))
return 0;
string strOut = "Vacant";
strSeatInfoStrings = new string[count];
string priset = "";
string namnet;
int i = 0;
for (int index = 0; index <= m_totNumOfSeats - 1; index++)
{
if (m_nameList[index] != null)
{
strOut = "Reserverad";
}
else
{
strOut = "Vacans";
}
if (choice == DisplayOptions.VacantSeats)
{
priset = m_priceList[index].ToString();
namnet = m_nameList[index];
}
else if (choice == DisplayOptions.ReservedSeats)
{
priset = m_nameList[index].ToString();
namnet = m_nameList[index];
}
else
{
priset = m_priceList[index].ToString();
namnet = m_nameList[index];
}
if (i < strSeatInfoStrings.Length)
strSeatInfoStrings[index++] = string.Format("{0,5}{1, 35}{2, 35}{3,40}", index, strOut, namnet, priset);
}
return i;
}
}
}