Why is nothing being passed to my overloaded function?

MyStackOverflows

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!

Sami Kuhmonen

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.

edited at
0

Comments

0 comments
Login to comment

Related

Why is my search function not being passed?

Why is a function being passed in?

Why aren't the values from my directive being passed into the function?

Flutter: Why is my function passed to `StatelessWidget` not is not being executed?

Flutter: Why is my custom function passed to a `StatelessWidget` object not being executed?

Parameters of my function in React child component are not being passed to the parent component's function. Why is this happening?

Why are my meteor settings not being passed to the application?

Why is my array being passed with an incorrect size?

Why is Context not being passed to my HOC

Why is my input not being passed into the if statements?

My onChange function is not being passed down to child

Why is my parameter not passed into the function?

Why don't I have to specify that the result of a fortran function is being passed by value to my C++ program?

My React function being passed down as props is not being invoked

Why is my function not being called?

Why are my props not being passed to my child component?

Why is only half my data being passed into my dictionary?

Why is the my function not being called in my SetInterval?

Why is my function in a function being regarded as a pointer?

Why is my variable not properly being passed in React Native?

Why isn't my prop logging or being passed in React Native?

Why are my hidden input not being passed in the model class?

Why is my INT variable being passed by reference?? C#

Why aren't my arguments being passed to the parameters?

What is the "instance" being passed to the to_representation function of my ListSerializer?

Why my RAM gets overloaded when I run javascript function?

Why can't the compiler find the overloaded version of my function?

why is function not being passed as props to onClick of another component

Why is my function proxy not being called in Node?