someonesshadow
someonesshadow
CC#
Created by someonesshadow on 12/27/2022 in #help
❔ How to input a reference of a class into a function with an argument of an interface
` I want to save the result of whatever StartFight does to fighter1 and fighter 2, specifically the health and other things down the line
Hero player = new Hero(20,30,10, "Hero");
Enemy goblin = new Enemy(3,3,2, "Goblin");
BattleField plains = new BattleField();
plains.StartFight(player, goblin);

class BattleField
{
public void StartFight(IFighter fighter1, IFighter fighter2) {...}
}
class Hero : IFighter {...}
class Enemy : IFighter {...}
Hero player = new Hero(20,30,10, "Hero");
Enemy goblin = new Enemy(3,3,2, "Goblin");
BattleField plains = new BattleField();
plains.StartFight(player, goblin);

class BattleField
{
public void StartFight(IFighter fighter1, IFighter fighter2) {...}
}
class Hero : IFighter {...}
class Enemy : IFighter {...}
using out or ref throws out an error
Argument 1: cannot convert from 'out Test.Adventure.Hero' to 'out Test.Adventure.IFighter' csharp(CS1503)
Argument 1: cannot convert from 'ref Test.Adventure.Hero' to 'ref Test.Adventure.IFighter' csharp(CS1503)
Argument 1: cannot convert from 'out Test.Adventure.Hero' to 'out Test.Adventure.IFighter' csharp(CS1503)
Argument 1: cannot convert from 'ref Test.Adventure.Hero' to 'ref Test.Adventure.IFighter' csharp(CS1503)
any other solutions than just returning the IFighter arguments with a tuple then assigining values individually outside the method?
8 replies