❔ 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?
6 Replies
Anton
Anton2y ago
your fighters are reference types, it's going to be saved anyway
someonesshadow
someonesshadowOP2y ago
oh. misred the results, ah Seems like “in” doesnt work, but the work around should be simple Thanks!
Anton
Anton2y ago
what doesn't work
someonesshadow
someonesshadowOP2y ago
in
Anton
Anton2y ago
what do you need that for
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server