运行时错误消息 [英] run time error message

查看:80
本文介绍了运行时错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

公共局部类Fom1:表格
{
个人= new Personal();
SqlConnection conn =新的SqlConnection();

公共Fom1(字符串personName)
{
InitializeComponent();

但是当使用这个

Fom1 fl =新的Fom1();
fl.Close();


我收到错误消息
Godswill.Fom1''不包含带有``0''参数的构造函数

请关闭表单

public partial class Fom1 : Form
{
Personal person = new Personal();
SqlConnection conn = new SqlConnection();

public Fom1(string personName)
{
InitializeComponent();

but when use this

Fom1 fl = new Fom1();
fl.Close();


i get the error message
Godswill.Fom1'' does not contain a constructor that takes ''0''arguments

please how do i close the form

推荐答案

请问您是否有理由在此处使用"Fom1"作为表单实例的名称,而不是使用"'Form1? " Fom1有点让人眼花.乱.

错误消息非常清楚:Fom1类的构造函数从Form 需求继承一个参数,即字符串.并且,当您在代码中创建"Fom1"的实例时,不会传递任何参数.

如果您这样做:
Is there a reason for your using ''Fom1 as the name of the instance of the Form here rather than ''Form1 ? ''Fom1 is a bit confusing to the eye.

The error message is really clear: the constructor for your Fom1 class which inherits from Form demands one parameter, a string. And, when you create an instance of ''Fom1, in your code, you pass no parameters.

If you did this:
Fom1 fl = new Fom1("");
fl.Close();

您将没有问题.

您可以为类定义多个构造函数:假设我以这种方式定义了一个名为"Form2"的窗体:

You would not have a problem.

You can define multiple constructors for classes: suppose I define a Form, named ''Form2, this way:

public partial class Form2 : Form
{
    private string PersonName;
    private string EmployeeId;

    public Form2()
    {
        InitializeComponent();
    }

    public Form2(string personName): base()
    {
        PersonName = personName;
    }

    public Form2(string personName, string employeeId): this(personName)
    {
        EmployeeId = employeeId;
    }
}

然后,此代码...以其他某种形式执行...将创建零,一个或两个构造函数参数方面的Form2的所有三种可能变体:

Then, this code ... executed in some other Form ... will create all three possible variations of Form2 in terms of zero, one, or two, constructor arguments:

Form2 f2NOParams = new Form2();
f2NOParams.Text = "Made with no parameters in the Constructor";

Form2 f2ONEParams = new Form2("Some Person''s Name");
f2ONEParams.Text = "Made with one parameter in the Constructor";

Form2 f2TWOParams = new Form2("Another Person''s Name", "Employee XQ-45");
f2TWOParams.Text = "Made with two parameters in the Constructor";

// seeing is believing
f2NOParams.Show();
f2ONEParams.Show();
f2TWOParams.Show();

这些备用构造函数被引用作为构造函数重载".

请注意,重载"是如何在第二个示例中调用基(无构造函数参数),或者在第三个示例中使用特殊的表示法在构造函数参数列表的末尾调用具有一个构造函数参数的构造函数!
这就是为什么第三个示例不必在``employeeId''中显式分配传入参数的原因:它调用了第二个示例,而第二个示例调用了保证"InitializeComponent被调用"的基本构造函数. />
欢迎来到构造函数重载"的奇妙世界:)

These alternate constructors are referred to as "Constructor Overloads."

Note how the "overloads" invoke either the base (no constructor parameter) in the second example, or the constructor with one contstructor parameter in the third example using a special notation following the end of the constructor parameter list !

That''s why the third example does not have to explicitly assign the incoming parameter in ''employeeId: it calls the second example which does that, and the second example invokes the base constructor which guarantees that ''InitializeComponent is called.

Welcome to the wonderful world of "Constructor Overloads" :)


首先,请小心避免在任何时候都不正确的语句,尤其是在提问时.

您的标题显示为运行时错误消息",显然不是.这是编译器错误消息.

现在,让我告诉您,您的代码和帖子中没有任何内容可以证明您的构造函数接受字符串参数.即使您可以从技术上定义一个(或多个),在这种情况下使用它也是一种不良的编码风格.从技术上讲,您可以按照Bill的建议进行操作.我还想建议您不要使用任何硬编码的字符串(除了一些广泛使用的字符串(例如0、1或null)之外,通常不要使用立即常量).使用立即常量对维护非常不利.您甚至不应使用",而应使用string.Empty.

至于名称Fom1,这确实很糟糕.所有名称均应使用正确的英文拼写.顺便说一句,Form1这个名字也很糟糕.这两种情况都残酷地违反了(好的)Microsoft命名约定.特别是,不应使用缩写和数字.您可以说名称Form1是Microsoft自动生成的.所以呢?谁告诉您您可以保留自动生成的名字?为什么您认为您已经获得了重构引擎?所有名称都应更改为语义名称.

—SA
First of all, be careful to avoid any statements which are not true at any time, in particular, when asking question.

You title goes "run time error message" which apparently is not. This is a compiler error message.

Now, let me tell you that there is nothing in your code and in your post that would justify your constructor accepting a string parameter. Even though you can technically define one (or more), using it in this case would be a bad coding style. Technically, you can follow the advice by Bill. I also want to advise that you should not use any hard-coded strings (and no immediate constants in general except some widely used ones like 0, 1 or null). Using immediate constants in very bad for maintenancе. You should not even use "" but should use string.Empty instead.

As to the name Fom1, this is really bad. All names should be in correct English spelling. By the way the name Form1 is also very bad. Both cases brutally violate (good) Microsoft naming conventions. In particular, abbreviations and digits should not be used. You can say that the name Form1 is auto-generated by Microsoft. So what? Who told you that you can leave the name as they are auto-generated? Why do you think you are provided withth the refactoring engine? All names should be changed to semantic names.

—SA


这篇关于运行时错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆