这个项目的范围只是一个控制台应用程序,我遇到了一个非常烦人的错误。当我尝试传递值时,我收到以下错误信息:
参数类型“lambda表达式”不能分配给参数类型“DungeonRunner.Ability”。
我想要传递参数的函数如下:
public void UseSpell(Ability ability)
{
var SpellToUse = this.CharClass.GetAbilities();
//Get all Abilites
this.CharClass.GetAbilities();
if (this.MP == 0)
{
Console.WriteLine("You cannot use advanced Abilities, drink a Mana Potion to restore Mana");
var UsedSpell = SpellToUse.First();
this.MP -= UsedSpell.ManaCost1; //This line is unnecessary because the first spell costs nothing
}
else if(this.MP >= 0)
{
var UsedSpell = SpellToUse.Find(abilityName => abilityName.SpellName1 == ability.SpellName1);
this.MP -= UsedSpell.ManaCost1;
}
我要参考的类:
namespace DungeonRunner
{
public class Ability
{
private int ManaCost;
private int SpellDamage;
private string SpellName;
public Ability(int manaCost, int spellDamage, string spellName)
{
ManaCost = manaCost;
SpellDamage = spellDamage;
SpellName = spellName;
}
public int ManaCost1
{
get => ManaCost;
set => ManaCost = value;
}
public int SpellDamage1
{
get => SpellDamage;
set => SpellDamage = value;
}
public string SpellName1
{
get => SpellName;
set => SpellName = value;
}
}
}
这是我要传递的值:
`MyCharacter.UseSpell(ability => ability.SpellName == spellname)`;
问题是:我如何优化这个问题,使错误消失。也许我需要更改参数,但我认为不需要。
MyCharacter.UseSpell(ability);
呢? - Mighty BadaboomUseSpell
,假设该方法通过迭代列表自行搜索正确的能力。另一方面,如果您向该方法提供特定的Ability
,则客户端必须在前面进行搜索。然而,您实际上想要什么并不清楚。UseSpell
应该期望一个Ability
还是搜索匹配的Ability
? - MakePeaceGreatAgain