0

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.

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;
}
4

4 回答 4

3

您的方法getValidString有 2 个输入:string promptint maxLength

但是,您要发送 2 个字符串和 2 个整数(总共 4 个)("Customer Name", txtName.Text, 3, 30)

要么重载,要么getValidString像这样接受 4​​ 个输入

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

或只发送一个string和一个int

routine.getValidString("Customer Phone Number", 30);
于 2013-08-28T17:04:53.950 回答
1

您的方法仅使用两个参数定义public static string getValidString(string prompt, int maxLength)。但是你用 4 来调用它:sError = routine.getValidString("Customer Name", txtName.Text, 3, 30);

将缺少的参数添加到 getValidString:public static string getValidString(string prompt, int maxLength, int foo, int bar)

或者

或者从 sError 行中删除它们:sError = routine.getValidString("Customer Name", txtName.Text);

于 2013-08-28T17:03:57.393 回答
1

它只是说没有getValidString哪个版本接受您试图传递的参数。查看方法的定义。您可以通过将鼠标悬停在它上面来做到这一点。如果有过载,就会有小箭头在它们之间循环。该信息将说明哪些类型以及它们被接受的顺序。

哦,我刚刚注意到您的定义在上面列出。如您所见,它只接受一个字符串和一个 int。你试图用参数列表来调用它;字符串,字符串,整数,整数。您需要更改方法以接受所有这些参数或更改调用代码以使用字符串和 int 调用它。

于 2013-08-28T17:04:05.880 回答
1

您的函数签名定义如下:

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

它采用 2 个参数:prompt(a string) 和maxLength(an int)。

当您稍后调用它时,您将向它传递四个参数:

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

由于没有getValidString()与您传递的参数匹配的签名(string, string, int, int),因此 C# 编译器出现错误。

于 2013-08-28T17:05:07.460 回答