简体   繁体   English

C#“不包含带有'1'参数的构造函数”

[英]C# “does not contain a constructor that takes '1' arguments”

I've posted something like this a bit ago and it helped out but i had issues afterwards and couldn't do anything. 我前几天发布了类似的内容,它有所帮助,但事后我遇到了问题,无能为力。 SO I'M BACK! 所以我回来了!

Time time1;
private void btnNewTime_Click(object sender, EventArgs e)
        {
            Time time1 = new Time(Convert.ToInt32(txtHour.Text.Trim(), Convert.ToInt32(txtMin.Text)));


        }

and in the time class: 在时间课中:

        public Time()
        {
            hour = 12;
            minute = 00;
        }//end of Time

        public Time(int Hour, int Minute)
        {
            hour = Hour;
            minute = Minute;
        }//end of Time

It's suppose to go into the parameterized constructor (the second one) But i get the error: 假定要进入参数化的构造函数(第二个),但出现错误:

"does not contain a constructor that takes '1' arguments" “不包含接受“ 1”参数的构造函数”

这是一个错字,一个错误的括号。

Time time1 = new Time(Convert.ToInt32(txtHour.Text.Trim()), Convert.ToInt32(txtMin.Text));
Time time1 = new Time
    (
      Convert.ToInt32(txtHour.Text.Trim()), 
      Convert.ToInt32(txtMin.Text)
    );

Looks like you're missing a parenthesis after the first trim to close the Convert.ToInt32 (Also lose one of the last parenthesis on the end). 似乎在第一个修整以关闭Convert.ToInt32之后缺少括号(也丢失了最后一个括号)。

And, your first construct of Time can be: 而且, Time的第一个构造可以是:

public Time()
  :this(12,0)
{
}

Or, if you have VS2010/.NET4 you can now use optional parameters : 或者,如果您拥有VS2010 / .NET4,则现在可以使用可选参数

public Time(int Hour = 12, int Minute = 0)
{
  hour = Hour;
  minute = Minute;
}

You are only providing 1 argument to the constructor: 您仅向构造函数提供1个参数:

Time time1 = new Time(Convert.ToInt32(txtHour.Text.Trim(), Convert.ToInt32(txtMin.Text)));

You need to close the first arguments 2nd set of parenthesis: 您需要关闭第二个括号的第一个参数:

Time time1 = new Time(Convert.ToInt32(txtHour.Text.Trim()), Convert.ToInt32(txtMin.Text));

它的错字:

Time time1 = new Time(Convert.ToInt32(txtHour.Text.Trim()), Convert.ToInt32(txtMin.Text));  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM