我试图早些时候获得一些帮助,但我认为我没有提供足够的信息,尽管我很感激所有的建议。
目标只是将 Object Room 的新实例添加到数组并打印到列表框。当用户尝试输入已经存在的房间名称时,它应该简单地显示在数组中已经存在的房间的规范中。
我不断收到空引用异常。
这是我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Room_Class
{
public partial class Form1 : Form
{
Room[] roomArray = new Room[20];
int count = 0;
public Form1()
{
InitializeComponent();
}
private void btnAddRm_Click(object sender, EventArgs e)
{
double length, width, height;
if (VerifyRoomName() == true)
{
if (txtRmLen.Text == "" || txtRmWid.Text == "" || txtRmHt.Text == "")
{
for (int i = 0; i < roomArray.Length; i++)
{
if (txtRmName.Text == roomArray[i].getRoomName())
{
txtRmName.Text = roomArray[i].getRoomName();
txtRmLen.Text = roomArray[i].getLength().ToString();
txtRmHt.Text = roomArray[i].getCeilingHeight().ToString();
txtRmWid.Text = roomArray[i].getWidth().ToString();
}
else
{
roomArray[count] = new Room(roomName);
count++;
}
}
}
else
{
try
{
length = double.Parse(txtRmLen.Text);
width = double.Parse(txtRmWid.Text);
height = double.Parse(txtRmHt.Text);
for (int i = 0; i < roomArray.Length; i++)
{
if (txtRmName.Text == roomArray[i].getRoomName())
{
txtRmName.Text = roomArray[i].getRoomName();
txtRmLen.Text = roomArray[i].getLength().ToString();
txtRmHt.Text = roomArray[i].getCeilingHeight().ToString();
txtRmWid.Text = roomArray[i].getWidth().ToString();
}
else
{
roomArray[count] = new Room(roomName, length, width, height);
count++;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n\nPlease Enter Valid Values", "Error!");
}
}
PrintList();
}
}
private void PrintList()
{
double paintTotal = 0, feetTotal = 0;
string RoomName;
lstRoomList.Items.Clear();
for (int i = 0; i < count; i++)
{
RoomName = roomArray[i].getRoomName() + "\t" + roomArray[i].SquareFeet().ToString("n1") + "\t" + roomArray[i].GallonsPaint().ToString("n1");
lstRoomList.Items.Add(RoomName);
paintTotal += roomArray[i].GallonsPaint();
feetTotal += roomArray[i].SquareFeet();
lblTtlGallons.Text = paintTotal.ToString("n1");
lblTtlSqFt.Text = feetTotal.ToString("n1");
}
}
private bool VerifyRoomName()
{
if (roomName == "")
{
MessageBox.Show("Please Enter a Room Name", "Error!");
return false;
}
else
return true;
}
}
}