【问题标题】:Delegate using (methodes)使用(方法)委托
【发布时间】:2019-08-15 15:32:36
【问题描述】:

我想与代表一起工作,但我不知道如何正确使用它们。 我想知道它们的用途。

namespace Delegates_Events_Excptions
{
    public class Program
    {
        static void Main(string[] args)
        {

            PlayerStats.ScoreDelegate scoreDelegate = new PlayerStats.ScoreDelegate(PlayerStats.OnGameOver(allPlayerStats));
            Console.WriteLine(scoreDelegate);
            Console.ReadKey();
        }

        /// <summary>
        ///  Here in this tutorial you get to know the difference between using delegates and not
        ///  You safe a lot of Time and code when u do so
        ///  You can use it when you use two or more methods that do the same with different values
        /// </summary>
        public class PlayerStats
        {
            public string name;
            public int health;
            public int dmg;
            public int gold;

            public delegate int ScoreDelegate(PlayerStats stats);

            public static void OnGameOver(PlayerStats[] allPlayerStats)
            {
                int playerNameMostHealth = GetPlayerNameTopScore(allPlayerStats, stats => stats.health) ;
                int playerNameMostGold = GetPlayerNameTopScore(allPlayerStats, stats => stats.gold);
                int playerNameMostDmg = GetPlayerNameTopScore(allPlayerStats, stats => stats.dmg);
            }

            public static int GetPlayerNameTopScore(PlayerStats[] allPlayerStats, ScoreDelegate scoreCalculator)
            {
                string name = "";
                int bestScore = 0;

                foreach (PlayerStats stats in allPlayerStats)
                {
                    int score = scoreCalculator(stats);

                    if (score > bestScore)
                    {
                        bestScore = score;
                        name = stats.name;
                    }
                }

                return bestScore;
            }
        }
}

我收到错误:CS7036 没有任何参数表明“Program.PlayerStats.OnGameOver(Program.PlayerStats[])”中的形式参数“allPlayerStats”符合。
我已经谢谢你了:)

【问题讨论】:

    标签: c# visual-studio methods delegates overloading


    【解决方案1】:

    方法void OnGameOver(PlayerStats[] allPlayerStats)与委托int ScoreDelegate(PlayerStats stats);不兼容,因为参数类型不同。

    你需要改变这个

    public delegate int ScoreDelegate(PlayerStats stats);
    

    public delegate int ScoreDelegate(PlayerStats[] stats);
    

    这修复了错误CS7036

    接下来你需要做的事情:

    1. 定义allPlayerStats
    2. 将属性healthgolddmg 添加到类PlayerStats

    【讨论】:

    • 谢谢,我摆脱了CS7036,但是在这种情况下我怎么定义allPlayerStats呢?
    • 不客气。请点赞我的回答。然后我可以帮你解决下一个问题。
    • 我不能upyote但我没有足够的积分......但是当我有的时候我会的
    【解决方案2】:

    我收到错误:CS7036 没有任何参数表明“Program.PlayerStats.OnGameOver(Program.PlayerStats[])”中的形式参数“allPlayerStats”符合要求。

    是的,没错。线

    PlayerStats.ScoreDelegate scoreDelegate = new PlayerStats.ScoreDelegate(PlayerStats.OnGameOver(allPlayerStats));
    

    无效有几个原因。首先,在这个范围内没有allPlayerStats 这样的变量;其次,PlayerStats.OnGameOver(allPlayerStats) 不是一个函数,所以你不能用它做一个ScoreDelegate

    你想用这条线完成什么?由于我不知道您要完成什么,我只能猜测您需要做什么才能完成它。

    但我猜测你想要:

    • 修改函数PlayerStats.OnGameOver,让它返回一些东西。
    • 删除变量scoreDelegate,因为它没有任何用途。
    • 调用Console.WriteLine,结果值为PlayerStats.OnGameOver

    【讨论】:

    • 首先感谢您的回答,但我希望它返回一些是的^^
    猜你喜欢
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    • 2011-07-23
    • 1970-01-01
    • 2011-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多