Home > Mobile >  can't fix this error. CS1579 foreach statement cannot operate on variables of type 'Charac
can't fix this error. CS1579 foreach statement cannot operate on variables of type 'Charac

Time:11-10

New and learning c# don't understand and can't fix this error. CS1579 foreach statement cannot operate on variables of type 'Character' because 'Character' does not contain a public instance or extension definition for 'GetEnumerator'

my character code is this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleTestRpg.Items.Interfaces;

namespace ConsoleTestRpg.Enties.Models
{
    public class Character
    {
        public string? Name;
        public int Level;
        public Attributes Attributes;
        public int Gold;
        public string? Background;
        public int Inventoryweight;
        public int Exp;
        public int MaxExp;
        public List<string>? AdventuresPlayed;
        public bool IsAlive;
        public int ArmorClass;
        public List<IItem>? Inventory;
        public int HitPoints;
        public int MaxHitPoints;
        public int MagicPoints;
        public int MaxMagicPoints;
        public CharacterClass Class;

        public int Count()
        {
            throw new NotImplementedException();
        }
    }

    public class Attributes
    {
        public int Strength;
        public int MaxStrength;
        public int Constitution;
        public int MaxConstitution;
        public int MaxDexterity;
        public int Dexterity;
        public int Intelligence;
        public int MaxIntelligence;
        public int Wisdom;
        public int MaxWisdom;
        public int Charisma;
        public int MaxCharisma;

    }
     public enum CharacterClass
    {
        Fighter,
        Thief,
        Mage,
        Cleric
    }
}

the code in question is:

using ConsoleTestRpg.Adventures;
using ConsoleTestRpg.Adventures.interfaces;
using ConsoleTestRpg.Enties.Models;
using ConsoleTestRpg.Entites.Interfaces;

namespace ConsoleTestRpg.Game
{
    public class GameService
    {
        private IAdventureService adventureService;
        private CharacterService characterService;

        public GameService(IAdventureService AdventureService, ICharacterService CharacterService)
        {
            adventureService = AdventureService;
            characterService = CharacterService;
        }
        public bool StartGame(Adventure adventure = null)
        {

            try
            {
                if (adventure == null)
                {
                    adventure = adventureService.GetInitialAdventure();
                }
                

                Console.Clear();
                Console.WriteLine();

                //Create Title Banner
                for (int i = 0; i <= adventure.Title.Length   3; i  )
                {
                    Console.Write("*");
                    if(i == adventure.Title.Length   3)
                    {
                        Console.Write("\n");
                    }
                }
                Console.WriteLine($"| {adventure.Title} |");
                for (int i = 0; i <= adventure.Title.Length   3; i  )
                {
                    Console.Write("*");
                    if (i == adventure.Title.Length   3)
                    {
                        Console.Write("\n");
                    }
                }

                Console.BackgroundColor = ConsoleColor.Black;
                Console.ForegroundColor = ConsoleColor.DarkBlue;
                Console.WriteLine($"\n{adventure.Description.ToUpper()}");

                Console.BackgroundColor = ConsoleColor.Red;
                Console.ForegroundColor = ConsoleColor.Yellow;

                var charactersInRange = characterService.GetCharactersInRange(adventure.MinimumLevel, adventure.MaxLevel);

                if (charactersInRange.Count() == 0)
                {
                    Console.WriteLine("No characters in range of adventure");
                    return false;
                }
                else
                {
                    Console.WriteLine("WHO DOTH WISH TO CHANCE DEATH?");
                    var characterCount = 0;
                    foreach (var character in charactersInRange)
                    {
                        Console.WriteLine($"#{characterCount} {character.Name} Level - {character.Level} {character.Class}");
                        characterCount  ;
                    }
                }

                return true;

            }
            catch (Exception ex)
            {
                Console.WriteLine($"Something went wrong {ex.Message}");
                return true;

I've looked online and the docs... but can't figure it out. maybe I'm just not understanding what I am looking at.

the GetcharacterInRange code:

 public List<Character> GetCharactersInRange(int minLevel = 0, int 
maxLevel = 20)
    {
        var basePath = $"{AppDomain.CurrentDomain.BaseDirectory}characters";
        var charactersInRange = new List<Character>();

        try
        {
            var directory = new DirectoryInfo(basePath);
            foreach (var file in directory.GetFiles($"*.json"))
            {
                using (StreamReader fi = File.OpenText(file.FullName))
                {
                    var potentialCharacterInRange = JsonConvert.DeserializeObject<Character>(fi.ReadToEnd());
                    if (potentialCharacterInRange.IsAlive && (potentialCharacterInRange.Level >= minLevel && potentialCharacterInRange.Level <= maxLevel))
                    {
                        charactersInRange.Add(potentialCharacterInRange);
                    }

                }
            }
        }
        catch (Exception ex)
        {

            Console.WriteLine($" Something went wrong {ex.Message}");
        }
        return charactersInRange;

    }

    Character ICharacterService.GetCharactersInRange(int minLevel, int maxLevel)
    {
        throw new NotImplementedException(); 

CodePudding user response:

The problem is that when you do this:

var charactersInRange = characterService.GetCharactersInRange(adventure.MinimumLevel, adventure.MaxLevel);

you think that you're calling this method:

public List<Character> GetCharactersInRange(int minLevel = 0, int maxLevel = 20)

but you're actually calling this method:

Character ICharacterService.GetCharactersInRange(int minLevel, int maxLevel)

That's because you have declared characterService as type ICharacterService:

private ICharacterService characterService;

so the explicit implement is the one that gets invoked.

What almost certainly should be the case is that the GetCharactersInRange method declare in the ICharacterService interface should have a return type of List<Character> rather than just Character. How does it make sense to have a method whose name explicitly states that it's getting multiple Character objects have a return type that explicitly permits only one? Your service class could then just implement that method instead of having two method with the same name and different return types. Your GameService code would then be calling that one and only method with the correct return type.

  •  Tags:  
  • c#
  • Related