NotImplementedException 未处理!卡住!
我正在构建一个应用程序来存储车辆,我有一个主表单,上面有按钮来加载每种类型的车辆(汽车、卡车、公共汽车)的单独表单,它们都从车辆类继承其主要属性。我还有一个“车队”类别,以便所有车辆都可以添加到车队中。
我已经设法获得第一辆工作车,但我正在努力处理第二辆(卡车)车,我可以单击卡车表单加载按钮,我可以将数据输入到文本框中,但当我单击“关闭”按钮时表格上没有任何反应!当我关闭应用程序时,我收到“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();
}
}
}
I am building an app to store vehicles, I have a main form with buttons on it to load individual forms for each type of vehicle (car,truck,bus) they all inherit their main property's from the vehicle class. I also have a 'fleet' class so that all the vehicles can be added to the fleet.
I have managed to get the first one working car, but I am struggling with the second (truck) one, I can click the button the truck form loads and I can input data into the txt boxes but when I click the 'Close' button on the form nothing happens! And when I close the app I get a 'NotImplementedExecption was unhandled'
Any ideas, what am I missing? Cheers in advance....
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
}
}
}
}
That is my main form with the buttons to load the truck form etc, here is my fleet class. I think I am missing something really silly!
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();
}
}
}
This is the part of code in debugging mode that brings up the 'NotImplementedException was unhandled.
internal void addToFleet(Truck myTruck)
{
throw new NotImplementedException();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(4)
看起来 Visual Studio 在某个时刻提示您“为‘Fleet’中的‘addToFleet’生成方法存根”,而您答应了。当 Visual Studio 为您生成方法时,它会将 throw new NotImplementedException();
放入其中,以提醒您需要返回并实现该方法。因此,通常您只需删除引发异常的行并将其替换为该方法中所属的任何代码。
但是,就您而言,我认为您根本不需要该方法。我假设 Truck 继承自 Vehicle;如果是这样,则需要车辆的 addToFleet 重载也适用于卡车。因此,您只需删除整个 internal void addToFleet(Truck myTruck)
方法即可。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您最好只使用这样的代码:
而不是使用您自己的自定义添加/删除函数,无论如何,它只是简单地调用这些函数。如果这是你的意图,你就没有隐藏它们。
至于为什么你会遇到这个问题......你应该查找重载解析并记住内部意味着同一程序集中的任何内容都可以访问它,因此只要这两个类位于同一个确切的程序集中,你将继续遇到这个问题。
You would be best off just using code like this:
than using your own custom add/remove functions which simply call those functions anyway. You have no hidden them if that was your intent.
As for why you are getting this problem... you should look up overload resolution and remember that internal means anything in the same assembly can access it, so as long as those two classes are in the same exact assembly, you will continue having this problem.