我正在构建一个用于存储车辆的应用程序,我有一个带有按钮的主要表单,用于为每种类型的车辆(汽车,卡车,公共汽车)加载单独的表格,他们都从车辆类继承了他们的主要财产。我还有一个“舰队”级别,所有车辆都可以加入舰队。
我已经设法获得了第一辆工作车,但是我正在努力使用第二辆(卡车),我可以点击卡车形式加载的按钮,我可以将数据输入到txt框中,但是当我点击'关闭表单上的“按钮”没有任何反应!当我关闭应用程序时,我得到'NotImplementedExecption未处理'
任何想法,我错过了什么?提前干杯....
public partial class FrmHireCo : Form
/*
* This is the main form for the car hire system.
* It allows the adding to new cars to the system, and displaying them in the fleet list
*
*/
{
private Fleet myFleet = new Fleet();
//Fleet object used to store cars
public FrmHireCo()
{
//Default constructor
InitializeComponent();
}
private void updateFleetList()
{
lstFleet.Items.Clear();
foreach (Vehicle v in myFleet.fleet)
{
lstFleet.Items.Add(v);
}
}
private void btnAddCar_Click(object sender, EventArgs e)
{
//Add a new car
FrmCar carGui = new FrmCar(); //Form used to add new car
carGui.ShowDialog();
Car myCar = carGui.car; //Get new car from form
myFleet.addToFleet(myCar); //Add to fleet list
updateFleetList(); //Uodate fleet list
}
private void lstFleet_SelectedIndexChanged(object sender, EventArgs e)
{
/*
* This method is used to control the list box
* It is called when a row is selected by the user, it then displays frmCar
* with the car details
*/
if (lstFleet.SelectedIndex > -1)
{
int index = lstFleet.SelectedIndex;
Car myCar = myFleet.fleet.ElementAt(index);
FrmCar carGui = new FrmCar();
carGui.car = myCar;
carGui.Show();
}
}
private void FrmHireCo_Load(object sender, EventArgs e)
{
}
private void btnAddTruck_Click(object sender, EventArgs e)
{
FrmTruck truckGui = new FrmTruck(); //Form used to add new car
truckGui.ShowDialog();
Truck myTruck = truckGui.truck; //Get new car from form
myFleet.addToFleet(myTruck); //Add to fleet list
updateFleetList(); //Uodate fleet list
}
}
} }
这是我的主要形式,带有加载卡车形式的按钮等,这是我的舰队类。我想我错过了一些非常愚蠢的东西!
class Fleet
{
/*
* This class is used to hold a list of Car objects that make up the fleet:
* The car objects may be added through the addToFleet() method.
* The car objects may be deleted tgrough the deleteFromFleet() method
* Use the fleet property to access the list of car objects
*/
private List<Vehicle> theFleet = new List<Vehicle>(); //The list of car objects being stored
public List<Vehicle> fleet
/* The fleet property. Note that you can only read it
* use the addToFleet and deleteFromFleet to update it
*/
{
get
{
return theFleet;
}
}
public void deleteFromFleet(Vehicle aVehicle)
//Delete car from fleet
{
theFleet.Remove(aVehicle);
}
public void addToFleet(Vehicle aVehicle)
//Add car to fleet
{
theFleet.Add(aVehicle);
}
internal void addToFleet(Truck myTruck)
{
throw new NotImplementedException();
}
}
}
这是调试模式中的代码部分,它显示'NotImplementedException未处理。
internal void addToFleet(Truck myTruck)
{
throw new NotImplementedException();
}
}
}
答案 0 :(得分:1)
你最好只使用这样的代码:
myFleet.fleet.Add(myTruck);
比使用您自己的自定义添加/删除功能,无论如何只需调用这些功能。如果那是你的意图,你就没有隐藏它们。
至于你为什么会遇到这个问题......你应该查看重载决策并记住内部意味着同一个程序集中的任何东西都可以访问它,所以只要这两个类在同一个精确的程序集中,你就会继续遇到这个问题。
答案 1 :(得分:1)
当您向车队添加卡车时,您实际上是在调用internal void addToFleet(Truck myTruck)
而不是public void addToFleet(Vehicle aVehicle)
,因为编译器正在调用与您传入的类型相匹配的方法。
抛弃internal
方法。
答案 2 :(得分:1)
看起来在某些时候,Visual Studio会提示您“在'Fleet'中为'addToFleet'生成方法存根'”,你说是的。当Visual Studio为您生成方法时,它会将throw new NotImplementedException();
放在内部作为提示,您需要返回并实现该方法。因此,通常您只需删除抛出异常的行,并将其替换为方法中的任何代码。
但是,在你的情况下,我认为你根本不需要这种方法。我假设卡车继承了车辆;如果是这样,带有车辆的addToFleet的重载也将适用于卡车。因此,您只需删除整个internal void addToFleet(Truck myTruck)
方法。
答案 3 :(得分:0)
internal void addToFleet(Truck myTruck)
{
throw new NotImplementedException();
}
显然,您已经完成了代码中实现该功能的部分。只需将“throw new NotImplementedException”替换为您的代码。