0

So I have this code in Game.cs:

public string _type { get; }
public int _strength;

public Game(string charType, int strength) {
  this._type = charType;
  this._strength = strength;
}
public override string ToString() {
  return $"{_type} {_strength}";
}

And I have this code in Program.cs:

List<Game> JoinLists = new List<Game>();
JoinLists.Add(new Game("Elf", 6));
JoinLists.Add(new Game("Angel", 10));
JoinLists.Add(new Game("Demon", 10));
JoinLists.Add(new Game("Wizard", 9));
JoinLists.Add(new Game("Undead", 7));
JoinLists.Add(new Game("Vampire", 9));
JoinLists.Add(new Game("Troll", 6));
Console.WriteLine("This is the last of characters that are owned by you: ");
List<Character> chars = BusinessLayer.Instance.GetAllCharacters();

foreach(var character in JoinLists) 
{
    Console.WriteLine(character.charType);
    foreach (Character c in chars) 
    {
        Console.WriteLine(c.Health);
    }
}

I just want to print the charType. Any way I can do that please?

I get this error:

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

Joshua
  • 97
  • 1
  • 7
  • Hello! The assembly reference error you are getting sounds pretty important. Please include the error, as it is likely related to build configurations and not the actual code that you provided. – h0r53 Jan 13 '20 at 14:13
  • What is the entire error message? What assembly are you missing? –  Jan 13 '20 at 14:15
  • @h0r53 Ok I put the error in the post – Joshua Jan 13 '20 at 14:16
  • Are Game and Program in the same project? What namespace is each one in? –  Jan 13 '20 at 14:16
  • @Amy yes they are. I am able to print the list entirely in program I just need to print the chartype – Joshua Jan 13 '20 at 14:17
  • To be able to get the chracter type you'd have to expose it like `public string Type {get;}` then use that instead of `_type` (which I assume is private). – juharr Jan 13 '20 at 14:18
  • That error message means the compiler is failing to build the type `Game`. Did u create the full class for `Game`? – Gerald Chifanzwa Jan 13 '20 at 14:19
  • Is the first code snippet enclosed in `public class Game { ... }` or just stand alone? – Olivier Jacot-Descombes Jan 13 '20 at 14:21
  • 2
    The code you provided seems incomplete. For one, you reference `character.charType` but the `Game` class stores that data as `_type`, not `charType`, which is an argument in your constructor. Please provide all relevant code. – h0r53 Jan 13 '20 at 14:21
  • Does this answer your question: https://stackoverflow.com/questions/20066556/does-not-contain-a-definition-for-and-no-extension-method-accepting-a-first-argu – sam Jan 13 '20 at 14:23
  • @OlivierJacot-Descombes yes it is enclosed in public class Game – Joshua Jan 13 '20 at 14:26
  • @gerryc.inc The error indicates the OP is trying to use a member of `Game` that it doesn't have, not that they cannot build the `Game` type. –  Jan 13 '20 at 14:27

1 Answers1

2

Assuming the code you've provided is complete, there is an error on the following line:

Console.WriteLine(character.charType);

As the Game class is defined, the desired data is stored as the variable _type, not charType. Thus, change the line above to the following is see if that resolves your error.

Console.WriteLine(character._type);
h0r53
  • 3,034
  • 2
  • 16
  • 25