适用于';..的最佳过载方法';有一些无效的参数

本文关键字:参数 无效 最佳 适用于 方法 | 更新日期: 2023-09-27 18:29:45

我创建了一个播放器类,并试图使用我的播放器类为菜单驱动的播放器程序创建和插入一个播放器,但我保留了错误:"Assignment_7.Program.GetInsertIndex(int Assignment_7.Player,ref int)"的最佳重载方法在我的InsertPlayer和ProcessCreate方法中有一些无效参数

如有任何帮助,我们将不胜感激。

 static void ProcessCreate(Int32 number, String firstName, String lastName, Int32 goals,
        Int32 assists, Player[] players, ref Int32 playerCount, Int32 MAXPLAYERS)
    {
        if (playerCount < MAXPLAYERS)
        {
            Console.WriteLine("'nCreate Player: please enter the player's number");
            number = Int32.Parse(Console.ReadLine());
            if (GetPlayerIndex(number, firstName, lastName, goals, assists, players, playerCount) == -1)
            {
                Console.WriteLine("'nCreate Player: please enter the player's First Name");
                 firstName = Console.ReadLine();
                 Console.WriteLine("'nCreate Player: please enter the player's First Name");
                 lastName = Console.ReadLine();
                Console.WriteLine("'nCreate Player: please enter the player's goals");
                goals = Int32.Parse(Console.ReadLine());
                Console.WriteLine("'nCreate Player: please enter the player's goals");
                assists = Int32.Parse(Console.ReadLine());
                InsertPlayer(number, firstName, lastName, goals, assists, players, ref playerCount);
                Console.WriteLine("'nCreate Player: Number - {0}, First Name - {1},LastName - {2} Goals - {3}, Assists {4} created successfully", number,firstName, lastName, goals, assists);
                Console.WriteLine();
            }
            else
                Console.WriteLine("'nCreate Player: the player number already exists");
        }
        else
            Console.WriteLine("'nCreate Player: the player roster is already full");
    }

    //Inserts the player at the correct location in the tables based on order of ascending player number
    //Unless the insert location is at the end, this requires shifting existing players down in order to make room 
    //Inserts the player at the correct location in the tables based on order of ascending player number
    //Unless the insert location is at the end, this requires shifting existing players down in order to make room 
    static Int32 InsertPlayer(Int32 number, String firstName, String lastName, Int32 goals,
        Int32 assists, Player[] players, ref Int32 playerCount)
    {
        Int32 insertIndex, shiftCount;
   ---> insertIndex = GetInsertIndex(number, players, playerCount); <--- Error
        for (shiftCount = playerCount; shiftCount > insertIndex; shiftCount--)
            players[shiftCount] = players[shiftCount - 1];
        players[insertIndex] = new Player(firstName, lastName, number, goals, assists);
        playerCount++;
        return insertIndex;
    }


    //Returns the index of the first player number in the table that is greater
    //than the player number to be inserted
    static Int32 GetInsertIndex(Int32 number,Player[] players,
        ref Int32 playerCount)
    {
        Int32 index = 0;
        bool found = false;
        while (index < playerCount && found == false)
            if (players[index].Number > number)
                found = true;
              else
                index++;
        return index;
    }
    //Returns the index of the player number in the table 
    //or -1 if the number is not found
    static Int32 GetPlayerIndex(Int32 number, String firstName, String lastName, Int32 goals,
        Int32 assists, Player[] players, ref Int32 playerCount)
    {
        Int32 index = 0;
        bool found = false;
        while (index < playerCount && found == false)
             if (players[index].Number == number)
                found = true;
              else
                index++;
        if (found == false)
            index = -1;
        return index;
    }

适用于';..的最佳过载方法';有一些无效的参数

根据MSDN,声明和调用都必须使用ref

若要使用ref参数,方法定义和调用方法都必须显式使用ref关键字。。。

所以,在你的情况下,你应该更换

insertIndex = GetInsertIndex(number, players, playerCount);

带有

insertIndex = GetInsertIndex(number, players, ref playerCount);

方法GetInsertIndex需要最后一个带有ref修饰符的参数,但您是按值传递的。您应该在传递参数之前添加ref

insertIndex = GetInsertIndex(number, players, ref playerCount);
//                                      here  ^^^

当您在方法参数中使用ref时,必须在调用中使用它。然后更换

insertIndex = GetInsertIndex(number, players, playerCount);

带有

insertIndex = GetInsertIndex(number, players, ref playerCount);