first poster here. To start things off, I'm using .NET 4.8 and C#. I'm having some issues with an overloaded function that doesn't seem to be getting any parameter input. Essentially what my functions are trying to do is take either a single object or a list of objects (all of a custom class 'Card') and determine if one of them has the same faction as the object calling the function. However, when I debug the the program, the first function works perfectly and as expected, but the second function doesn't seem to be getting any input at all; it gets an object with length 0. 'Factions' is a namespace-wide public enum; I don't think there are any problems there.
Class and function definition:
public class Card
{
/* Constructors, variables, and other functions left out for simplicity */
public bool SameFaction(Card card)
{
if (card.Faction.Equals(this.Faction))
return true;
return false;
}
public bool SameFaction(List<Card> hand)
{
foreach (Card card in hand)
{
if (card.Faction.Equals(this.Faction))
{
return true;
}
}
return false;
}
}
Implementation:
Card card1 = new Card(Factions.faction1);
List<Card> listOfCards = new List<Card>();
{
new Card(Factions.faction1);
new Card(Factions.faction2);
new Card(Factions.faction3);
new Card(Factions.faction4);
};
card1.SameFaction(new Card(Factions.faction1)); // Returns true
card1.SameFaction(listOfCards); // Returns false, and when debugging, shows input as being an object of length 0
Basically I'm wondering what I'm doing wrong, and also if there's a better way of doing this. I'm self-taught, so any and all suggestions are helpful. Thanks!
The problem is a single ;
too many
List<Card> listOfCards = new List<Card>();
{
new Card(Factions.faction1);
new Card(Factions.faction2);
new Card(Factions.faction3);
new Card(Factions.faction4);
};
This is two things, not one, since the first line ends in ;
. It creates an empty list and then there’s a block of code which creates objects without putting them anywhere.
This is probably what you meant:
List<Card> listOfCards = new List<Card>
{
new Card(Factions.faction1),
new Card(Factions.faction2),
new Card(Factions.faction3),
new Card(Factions.faction4)
};
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments