NotImplementedException 未处理!卡住!

发布于 10-05 22:04 字数 3566 浏览 4 评论 0原文

我正在构建一个应用程序来存储车辆,我有一个主表单,上面有按钮来加载每种类型的车辆(汽车、卡车、公共汽车)的单独表单,它们都从车辆类继承其主要属性。我还有一个“车队”类别,以便所有车辆都可以添加到车队中。

我已经设法获得第一辆工作车,但我正在努力处理第二辆(卡车)车,我可以单击卡车表单加载按钮,我可以将数据输入到文本框中,但当我单击“关闭”按钮时表格上没有任何反应!当我关闭应用程序时,我收到“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

喜爱纠缠2024-10-12 22:04:28

您最好只使用这样的代码:

myFleet.fleet.Add(myTruck);

而不是使用您自己的自定义添加/删除函数,无论如何,它只是简单地调用这些函数。如果这是你的意图,你就没有隐藏它们。

至于为什么你会遇到这个问题......你应该查找重载解析并记住内部意味着同一程序集中的任何内容都可以访问它,因此只要这两个类位于同一个确切的程序集中,你将继续遇到这个问题。

You would be best off just using code like this:

myFleet.fleet.Add(myTruck);

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.

温馨耳语2024-10-12 22:04:28

当您向车队添加卡车时,您实际上是在调用 internal void addToFleet(Truck myTruck) 而不是 public void addToFleet(Vehicle aVehicle),因为编译器正在调用该方法与您传入的类型相匹配。

放弃 internal 方法。

When you add a truck to your fleet, you're actually calling internal void addToFleet(Truck myTruck) not public void addToFleet(Vehicle aVehicle) because the compiler is calling the method that matches the type that you're passing in.

Ditch the internal method.

那伤。2024-10-12 22:04:28

看起来 Visual Studio 在某个时刻提示您“为‘Fleet’中的‘addToFleet’生成方法存根”,而您答应了。当 Visual Studio 为您生成方法时,它会将 throw new NotImplementedException(); 放入其中,以提醒您需要返回并实现该方法。因此,通常您只需删除引发异常的行并将其替换为该方法中所属的任何代码。

但是,就您而言,我认为您根本不需要该方法。我假设 Truck 继承自 Vehicle;如果是这样,则需要车辆的 addToFleet 重载也适用于卡车。因此,您只需删除整个 internal void addToFleet(Truck myTruck) 方法即可。

It looks like at some point Visual Studio prompted you to "Generate method stub for 'addToFleet' in 'Fleet'", and you said yes. When Visual Studio generates a method for you, it puts throw new NotImplementedException(); inside as a reminder that you need to go back and implement the method. So normally you would just delete the line that throws the exception and replace it with whatever code belongs in the method.

However, in your case I don't think you need that method at all. I assume that Truck inherits from Vehicle; if so, the overload of addToFleet that takes a Vehicle will work for Trucks as well. So you can just delete the entire internal void addToFleet(Truck myTruck) method.

暮凉2024-10-12 22:04:28
internal void addToFleet(Truck myTruck) 
{ 
    throw new NotImplementedException(); 
} 

显然,您已经掩盖了实现该函数的代码部分。只需将“抛出新的 NotImplementedException”替换为您的代码即可。

internal void addToFleet(Truck myTruck) 
{ 
    throw new NotImplementedException(); 
} 

Apparently, you've glossed right over the part of your code where the function is implemented. Simply replace "throw new NotImplementedException" with your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文