0

So I am getting this list from the database into DataLayer.cs:

public List<Character> ChooseChar(int userid)
    {
        List<Character> CharacterList = new List<Character>();

        var showCharacter = from c in dbConnection.Character
                            where c.UserID == userid
                            select c;

        foreach(var a in showCharacter)
        {
            CharacterList.Add(a);
        }
        return CharacterList;
    }

I have this in the BusinessLayer.cs:

public int userid { get; }

    public List<Character> ChooseChar(int userid)
    {
        return DataLayer.Instance.ChooseChar();
    }

I have this in Program.cs:

Console.WriteLine("This is the last of characters that are owned by you: ");
                List<Character> chars = BusinessLayer.Instance.ChooseChar();
                foreach(var character in JoinLists)
                {
                    if (character._number == chars.userid)
                    Console.WriteLine(character._type);
                }
                foreach (Character c in chars)
                {
                    Console.WriteLine(c.Health);
                }

I'm getting an error in line: if (character._number == chars.userid)

error: does not contain a definition for and no accessible extension method accepting a first argument of type could be found (are you missing a using directive or an assembly reference?)

I am trying to get the characters that chars.userid has and match it with an index from this:

List<Game> JoinLists = new List<Game>();
        JoinLists.Add(new Game(1, "Elf", 6));
        JoinLists.Add(new Game(2, "Angel", 10));
        JoinLists.Add(new Game(3, "Demon", 10));
        JoinLists.Add(new Game(4, "Wizard", 9));
        JoinLists.Add(new Game(5, "Undead", 7));
        JoinLists.Add(new Game(6, "Vampire", 9));
        JoinLists.Add(new Game(7, "Troll", 6));

    public int _number { get; }
    public string _type { get; }
    public int _strength;

    public Game(int charNumber, string charType, int strength)
    {
        this._number = charNumber;
        this._type = charType;
        this._strength = strength;
    }

    public override string ToString()
    {
        return $"{_type} {_strength}";
    }

Any ideas how please?

Sisir
  • 4,584
  • 4
  • 26
  • 37
Joshua
  • 97
  • 1
  • 7
  • 1
    How can this be related to MySQL and SSMS? SSMS is an IDE for SQL Server, but the above is C#, which can't be written in SSMS. Please take the time to ensure you tag correctly. If this has nothing to do with MySQL, or SSMS, then you shouldn't be tagging them. – Thom A Jan 13 '20 at 16:25
  • @Larnu by accident sorry – Joshua Jan 13 '20 at 16:26
  • This is the same error you reported in [your last question](https://stackoverflow.com/questions/59718329/how-do-i-print-1-column-from-a-list), just a different member and different class. –  Jan 13 '20 at 16:28
  • @Amy it's different because in that question I didn't need to use the data and business layer as I wasn't getting anything from the database – Joshua Jan 13 '20 at 16:31
  • The problem is still the same as that post though, the issue here has nothing to do with your data layer. This is because you are trying to access `chars.userid`, but chars is just a List, it has no such property. You want to compare against the actual items in the list, not the list itself. Try to take advantage of debugger and intellisense and these errors are easily identified. –  Jan 13 '20 at 16:32
  • @Joshua I understand what makes it different. A list of characters does not have a member `userid`, only individual characters do. Your code has it defined on `BusinessLayer`, yet that is not how you're trying to get it. –  Jan 13 '20 at 16:32
  • Does this answer your question? [Getting error: CS1061](https://stackoverflow.com/questions/2839946/getting-error-cs1061) – Bizhan Jan 13 '20 at 16:37

1 Answers1

0

In this line here:

List<Character> chars = BusinessLayer.Instance.ChooseChar();

You need to pass an integer to ChooseChar; although I assume that's a typo or else your code would not compile.

Regardless, chars is a List which does not have a property userid; that is a property of each Character object in the list.

In your if statement you need to re-use the same integer that you passed to ChooseChar() e.g.

int userid;
List<Character> chars = BusinessLayer.Instance.ChooseChar(userid);
foreach(var character in JoinLists)
{
    if (character._number == userid)
    Console.WriteLine(character._type);
}
Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35