简体   繁体   English

在C#类中使用通用方法

[英]Using a generic method in a C# class

Here is my current code: 这是我当前的代码:

namespace WindowsFormsApp1
{
public partial class Form1 : Form 
{
    public Random random = new Random();
    public int[] randomInt = new int[20];
    public double[] randomDouble = new double[20];

    public string searchKey;
    public int intOrDouble; // 0 if int, 1 if double

    public static int Search<T>(T[] inputArray, T key) where T : IComparable<T>
    {
        for (int i = 0; i < 20; i++)
        {
            if (inputArray[i].CompareTo(key) == 0)
            {
                return i;
            }
        }
        return -1;
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void randomIntGeneration_Click(object sender, EventArgs e)
    {
        randomNumbersTextBox.Clear(); // empty the textbox
        intOrDouble = 0; // this is for knowing which parameter to send to search method

        // generate 20 random integers and display them in the textbox
        for (int i = 0; i < 20; ++i)
        {
            randomInt[i] = random.Next(0, 100);
            randomNumbersTextBox.Text += randomInt[i].ToString() + "   ";
        }
    }

    private void randomDoubleGenerator_Click(object sender, EventArgs e)
    {
        randomNumbersTextBox.Clear(); // empty the textbox
        intOrDouble = 1; // this is for knowing which parameter to send to search method

        // generate 20 random doubles and display them in the textbox
        for (int i = 0; i < 20; ++i)
        {
            randomDouble[i] = random.NextDouble() + random.Next(0, 100);
            randomNumbersTextBox.Text += randomDouble[i].ToString() + "   ";
        }
    }

    private void searchArrayButton_Click(object sender, EventArgs e)
    {
        searchKey = searchKeyTextBox.Text;
        if(intOrDouble == 0) // int array passed
        {
            resultsTextBox.Text = Search(randomInt, searchKey).ToString();
        }
        else
        {
            resultsTextBox.Text = Search(randomDouble, searchKey).ToString();
        }
    }
}

} }

What i am trying to do is use this generic method. 我正在尝试使用的是这种通用方法。 The GUI allows the user to generate a random array of either ints or doubles. GUI允许用户生成int或double的随机数组。 I then want to use the Search method in the searchArrayButton_Click control to display whether or not the "searchKey" value entered is in the array. 然后,我想在searchArrayButton_Click控件中使用Search方法来显示输入的“ searchKey”值是否在数组中。 The error I am getting is "The type arguments for method 'Form1.Search(T[], T)' cannot be inferred from the usage. Try specifying the type arguments explicitly." 我收到的错误是“无法从用法中推断出方法'Form1.Search(T [],T)的类型参数。尝试显式指定类型参数。” They appear toward the bottom of the code when I try to call Search twice in the searchArrayButton_Click control. 当我尝试在searchArrayButton_Click控件中两次调用Search时,它们会显示在代码的底部。

You're trying to search an array of int or double values for a string . 您正试图在一个intdouble值数组中搜索一个string As a result, your call to Search has two arguments with two different types. 因此,您对Search的调用包含两个具有两种不同类型的参数。 That doesn't match the function signature. 与功能签名不匹配。

You need to convert whatever is in that textbox into the value you are searching for, either an int or a double. 您需要将文本框中的内容转换为要搜索的值,即int或double。

if(intOrDouble == 0) // int array passed
{
    resultsTextBox.Text = Search(randomInt, int.Parse(searchKey)).ToString();
}
else
{
    resultsTextBox.Text = Search(randomDouble, double.Parse(searchKey)).ToString();
}

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

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