Reputation: 21
I have two projects (MenuSystem and GameBrain) in one solution for a board game. Across projects i have enum EMenuNavigation which is placed in MenuSystem and link added to GameBrain.
I have a MenuItem class:
public class MenuItem
{
private string Title { get; set; }
public Func<EMenuNavigation>? MethodToRun { get; set; }
public override string ToString() => Title;
public MenuItem(string title, Func<EMenuNavigation>? methodToRun)
{
Title = title;
MethodToRun = methodToRun;
}
}
My function looks like this:
public static EMenuNavigation HvH()
{
var board = new Board(8);
var gameMenu = new Menu(EMenuLevel.Game, "> Game <");
gameMenu.MenuItems.Add(new MenuItem("Make A Move", board.MakeMove));
gameMenu.MenuItems.Add(new MenuItem("Cheat", null));
gameMenu.MenuItems.Add(new MenuItem("Save The Game", null));
board.ShowBoard();
return gameMenu.RunMenu();
}
My problem is here:
new MenuItem("Make A Move", board.MakeMove)
I get "Expected a method with 'EMenuNavigation MakeMove()' signature"
Method in Board class:
public class Board
{
public EMenuNavigation MakeMove()
{
var userMove = GetUserCoords("What do checker you want to move?");
Console.WriteLine(userMove);
return EMenuNavigation.Stay;
}
}
Problem: Why is "Expected a method with 'EMenuNavigation MakeMove()' signature" if i already have it in class Board and how i can solve it?
If i change
Func<EMenuNavigation>?
To
Func<int>?
Everything could work, but it will not be easy readable and hard to understand what number what means. If i try to change my
gameMenu.MenuItems.Add(new MenuItem("Make A Move", board.MakeMove));
To
gameMenu.MenuItems.Add(new MenuItem("Make A Move", board.MakeMove()));
I get another error and cannot solve it too.
Upvotes: 1
Views: 425
Reputation: 21
Lambda expression solves problem.
gameMenu.MenuItems.Add(new MenuItem("Make A Move", () => board.MakeMove() ));
Upvotes: 1