Cannot convert from int to system.collections.generic.icomparer<int>
New to C# Just wondering what the error is, can't understand the error. No need to correct, just want to know the mistake. ERROR: The best overloaded method match for 'Systems.Collections.Generic.List.BinarySearch(int,System.Collections.Generic.Icomparer)' has some invalid arguments Argument1: Cannot convert from system.collections.generic.list to int Argument2: Cannot convert from int to system.collections.generic.icomparer
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;enter code here
using System.Windows.Forms;
namespace LotoNumbers
{
public partial class FrmLotto : Form
{
// declare class level variables
List<int> player = new List<int>(7);
List<int> computer = new List<int>(6);
public FrmLotto()
{
InitializeComponent();
}
// event method to generate player's numbers
private void btnPlayer_Click(object sender, EventArgs e)
{
// declare and initalize local variables
string display = "";
// reset the winning number label before generating the player's numbers
lblComputerNumber.Text = "00 00 00 00 00 00";
// generate unique random numbers
GenerateRandomNumbers(player);
player.Sort();
// build display string
display = BuildDisplayString(player);
// display winning number in label
lblPlayerNumber.Text = display;
}
// method to generate computer's random numbers (the 'winning numbers')
// and determine how many winning numbers
private void btnComputer_Click(object sender, EventArgs e)
{
// declare and initalize local variables
int winCount = 0;
string display = "";
// generate unique random numbers
GenerateRandomNumbers(computer);
// sort the array in ascending order
computer.Sort();
// build display string
display = BuildDisplayString(computer);
// display winning number in label
lblComputerNumber.Text = display;
// determine if this number matches any of the players numbers
winCount = CompareTwoList(player, computer);
// display the total winning numbers
lblMatching.Text = winCount.ToString("D2");
}
private void GenerateRandomNumbers(List<int> numberList)
{
Random lucky = new Random();
// generate unique random numbers
for (int index = 0; index < numberList.Capacity; index++)
{
int temp = lucky.Next(1, 50);
if (numberList.IndexOf(temp) < 0)
{
numberList.Add(temp);
}
else
{
index--;
}
}
}
private string BuildDisplayString(List<int> numberList)
{
// declare method variable
string display = " ";
// loop through the array and build a display string
foreach (int number in numberList)
display += number.ToString("D2") + " ";
// return display string
return display;
}
private int CompareTwoList(List<int> list1, List<int> list2)
{
// declare method variable
int numberMatching = 0;
// loop through each element in the first array looking for a match in the second array
foreach (int value in list1)
{
if (player.BinarySearch(list2, value) >= 0)
numberMatching++; // a matching value is found
}
return numberMatching;
}
}
}
It looks like you want:
if (list2.BinarySearch(value) >= 0)
numberMatching++; // a matching value is found
You are calling List<int>.BinarySearch(int, IComparer<int>) at the moment. Since value
is an int
you are getting a type error.
Note you can also do:
int numberMatching = list2.Intersect(list1).Count();
There is no overload for List<int>.BinarySearch()
which accepts a List<int>
and int
as the parameters. See MSDN
You need to use player.BinarySearch(value)
上一篇: 在C#中将字符串转换为字节数组