简体   繁体   English

我出现错误“方法'getValidString'的重载不带参数”。 这是什么意思,我该如何解决?

[英]I have the error “No overload for method 'getValidString' takes for arguments”. What does this mean and how do i fix it?

this is for my college course and the deadline is in 2 days and i still have to finish the program test the code and also evaluate before then. 这是我的大学课程,截止日期为2天,我仍然必须完成程序测试代码并在此之前进行评估。

Below is the routine code. 下面是例程代码。

public static string getValidString(string prompt, int maxLength)
{
    bool valid = false; 
    //The paramter valid is set as a boolean and is set to false. 
    //It is set as a boolean because it is a small data type and 
    //only needs to have true or false
    string tempData = "";
    do // Do while loop is repetition and carries on doing it until the condition becomes true
    {
        Console.Write(prompt + " ?");
        tempData = Console.ReadLine();
        if (tempData == "")
            displayMessage("You have not entered any data, please try again");
        else if (tempData.Length < 3)
            displayMessage("You have not entered text longer than 3, please try again");
        else if (tempData.Any(c => char.IsDigit(c)))
            displayMessage("You have not entered text, please try again");
        else if (tempData.Length > maxLength)
            displayMessage("Name too long it must be 20 or less, please try again");
        else
            valid = true;
    }
    while (valid == false);
    return tempData;
}

And the code in my program which has the error attached is below. 下面是我程序中附带错误的代码。 How do I fix this? 我该如何解决? Thank you 谢谢

private void btncustCont_Click(object sender, EventArgs e)
{
    txtName.Text = custName[numCust];
    txtPhoneNumber.Text = custPhone[numCust];
    string sError = "";
    sError = routine.getValidString("Customer Name", txtName.Text, 3, 30);
    sError += routine.getValidString("Customer Phone Number", txtPhoneNumber.Text, 3, 30);
    if (sError != "")
    MessageBox.Show(sError, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

    if (sError == "")
        grpPrint.Enabled = true;
}

Your method getValidString have 2 inputs : string prompt and int maxLength 您的方法getValidString具有2个输入: string promptint maxLength

However, you're sending 2 strings and 2 ints (for a total of 4) ("Customer Name", txtName.Text, 3, 30) 但是,您要发送2个字符串和2个整数(总共4个) ("Customer Name", txtName.Text, 3, 30)

Either overload getValidString to take 4 input like this 要么重载getValidString接受这样的4个输入

public static string getValidString(string prompt, string name, int length, int maxLength)

or send only one string and an int : 或仅发送一个string和一个int

routine.getValidString("Customer Phone Number", 30);

Your method is defined with only two parameters public static string getValidString(string prompt, int maxLength) . 您的方法仅由两个参数定义: public static string getValidString(string prompt, int maxLength) But you're calling it with 4: sError = routine.getValidString("Customer Name", txtName.Text, 3, 30); 但是您使用4来调用它: sError = routine.getValidString("Customer Name", txtName.Text, 3, 30);

Either add the missing parameters to the getValidString: public static string getValidString(string prompt, int maxLength, int foo, int bar) 将缺少的参数添加到getValidString中: public static string getValidString(string prompt, int maxLength, int foo, int bar)

or 要么

Or remove them from the sError line: sError = routine.getValidString("Customer Name", txtName.Text); 或从sError行中将其删除: sError = routine.getValidString("Customer Name", txtName.Text);

It's simply saying there is no version of getValidString which accepts the arguments you're attempting to pass it. 只是说没有getValidString版本接受您要传递的参数。 Review the definition(s) of the method. 查看方法的定义。 You can do this by mousing over it. 您可以将鼠标悬停在上面。 If there are overloads there will be little arrows to cycle through them. 如果有过载,将没有箭头可以循环通过它们。 The info will tell which types, and in what order they're accepted. 该信息将告诉您接受哪些类型以及接受顺序。

Oh I just noticed your definition is listed above. 哦,我刚刚注意到您的定义已在上面列出。 As you can see it only accepts a string and an int. 如您所见,它仅接受字符串和整数。 You're trying to call it with the argument list; 您正在尝试使用参数列表来调用它; string, string, int, int. 字符串,字符串,整数,整数。 You need to change the method to accept all of those arguments or change the calling code to call it with a string and an int. 您需要更改方法以接受所有这些参数,或者更改调用代码以使用字符串和int对其进行调用。

Your function signature is defined as the following: 您的功能签名定义如下:

public static string getValidString(string prompt, int maxLength) { /* CODE */ }

It is taking 2 arguments: prompt (a string ) and maxLength (an int ). 它采用2个参数: prompt (一个string )和maxLength (一个int )。

When you are calling it later, you are passing four arguments to it: 以后调用它时,您要传递四个参数:

sError = routine.getValidString("Customer Name", txtName.Text, 3, 30);

Since there is no signature for getValidString() that matches the arguments you are passing ( string , string , int , int ), the C# compiler is rising an error. 由于没有与您传递的参数( stringstringintint )匹配的getValidString()签名,因此C#编译器出现错误。

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

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