Home > Enterprise >  I'm having some issues referencing a class to another class and the error I'm getting is I
I'm having some issues referencing a class to another class and the error I'm getting is I

Time:06-04

I have a Player class and I am trying to reference that class to my Main class. but I put some code in there that's giving me an error when I try and reference it to my main class. there is no given argument that corresponds to the required formal perimeter, 'statName' of 'Player.PLayer(string, int)' can someone help me? I'm pretty sure it has to do with mushing all the code together but I just don't know and am wanting a little help. here are my classes.

here is my program class

class Program
{
    PLayer Playerstat = new PLayer(); // I'm trying to reference the class Player here but it's not letting me.
    
    static void Main()
    {
        Console.WriteLine("please pick a mode");
        PLayGame();
        PickMode();
       


        Console.ReadKey();
    }

    public static void PLayGame()
    {
        List<string> Options = new List<string> { "Easy mode: ", "med mode: ", "Hard mode", "super hard mode: " };
        

        foreach(string ModePick in Options)
        {
            Thread.Sleep(570);
            Console.WriteLine(ModePick);
        }
    }

    private static void PickMode()
    {
        string pick = Console.ReadLine();
        if (pick == "easy")
        {
            Console.WriteLine("you have picked easy.");
        }
            
    }

    
}

and here is my Player class

public class PLayer
{
    public string Name;
    private int _statValue;
    private string _statName;
    public bool Dead;

    public PLayer(string statName, int statValue)
    {
        _statName = statName;
        _statValue = statValue;
    }

    public override string ToString() => _statName   ": "   _statValue;

    List<PLayer> stats = new List<PLayer>
    { new("Health", 100),
      new("Accuracy", 89),
      new("Blade Amount", 2),
      new("Blade Damage", 20),
      new("Blade Lanth", 40),
      new("Blade durablity", 50),
      new("Col", 0)
    };

    //all the NPCs you know. 
    List<string> Npc = new List<string> { "levi: ", "Armin: ", "Mikasa: ", "Hange: ", "Erwin: " };
    //skills for killing titans
    List<string> Skills = new List<string>();

    public void PrintStats()
    {
        Console.WriteLine("your stats are: ");
        foreach (var stat in stats)
        {
            Thread.Sleep(575);
            Console.WriteLine(stat.ToString());
        }
    }

}

CodePudding user response:

  1. Names like "PLayer" (both "P" and "L" capitalized) are just asking for trouble.

    BEST PRACTICE: Use PascalCase, e.g. class Player

  2. Use namespaces. MSVS will do this for you by default (if you're using the Visual Studio IDE); you didn't show us whether you're using them yourself.

    Using namespaces makes it easy to qualify your class names when referring to objects that are in different modules.

  3. MAIN PROBLEM:

    If you want Player Playerstat = new Player() ... then you need to declare a no-args ("parameterless") constructor for "Player"!

CodePudding user response:

You have several issues with your code, a HUGE one being a Stack Overflow Error.

The way your PLayer class is designed causes an infinite loop if you were to have created it properly in the first place.

First, to fix the error you are seeing, you'll want to pass in parameters into the Constructor of your PLayer class.

In your PLayer class you have a constructor:

public PLayer(string statName, int statValue)
    {
        _statName = statName;
        _statValue = statValue;
    }

Which requires you to supply a string statName and an int statValue whenever you instantiate your PLayer.

So in your Program the line you have written as:

PLayer Playerstat = new PLayer(); // I'm trying to reference the class Player here but it's not letting me.

Needs to be changed to something like:

PLayer Playerstat = new PLayer("ExampleString", 1); // I'm trying to reference the class Player here but it's not letting me.

However, that's not the main issue here. If you end up fixing that, you'll go to run your program and get hit with a Stack Overflow error. The reason being, your PLayer class, upon instantiation, tries to create a List<PLayer> within itself with your code:

List<PLayer> stats = new List<PLayer>
    { new("Health", 100),
      new("Accuracy", 89),
      new("Blade Amount", 2),
      new("Blade Damage", 20),
      new("Blade Lanth", 40),
      new("Blade durablity", 50),
      new("Col", 0)
    };

Each PLayer that you try to create in here, will also try to create its own list of PLayers which repeats infinitely until your computer would crash.

If you want to be able to create a PLayer with custom stats that you can print out, perhaps try approaching it with a Dictionary in the PLayer class that holds the stats. That way, you could supply a Dictionary to the PLayer constructor to populate it on instantiation.

Perhaps a PLayer class that looked like the following would be better suited for your needs:

public class PLayer
    {
        public string Name;
        public bool Dead;

        public PLayer(string name = "", Dictionary<string, int> stats = null)
        {
            Name = name;

            if (stats != null)
                this.Stats = stats;
        }

        Dictionary<string, int> Stats = new Dictionary<string, int>
        {
            { "Health", 100 },
            { "Accuracy", 89 },
            { "Blade Amount", 2},
            { "Blade Damage", 20 },
            { "Blade Lanth", 40 },
            { "Blade durablity", 50 },
            { "Col", 0}

        };



        //all the NPCs you know. 
        List<string> Npc = new List<string> { "levi: ", "Armin: ", "Mikasa: ", "Hange: ", "Erwin: " };
        //skills for killing titans
        List<string> Skills = new List<string>();

        public void PrintStats()
        {
            Console.WriteLine("your stats are: ");
            foreach (var stat in stats)
            {
                Thread.Sleep(575);
                Console.WriteLine(stat.ToString());
            }
        }

    }

Which you could then make use of like:

class Program
{
     static PLayer Playerstat; 
    
    static void Main()
    {

       Dictionary<string, int> CustomStats = new Dictionary<string, int>
            {
            { "Health", 1000 },
            { "Accuracy", 5 },
            { "Blade Amount", 15},
            { "Blade Damage", 5 },
            { "Blade Lanth", 20 },
            { "Blade durablity", 100 },
            { "Col", 0}

            };

        // Here we actually provide parameters for the constructor
        Playerstat = new PLayer("Example Player Name", CustomStats);


        Console.WriteLine("please pick a mode");
        PLayGame();
        PickMode();
       


        Console.ReadKey();
    }

    public static void PLayGame()
    {
        List<string> Options = new List<string> { "Easy mode: ", "med mode: ", "Hard mode", "super hard mode: " };
        

        foreach(string ModePick in Options)
        {
            Thread.Sleep(570);
            Console.WriteLine(ModePick);
        }
    }

    private static void PickMode()
    {
        string pick = Console.ReadLine();
        if (pick == "easy")
        {
            Console.WriteLine("you have picked easy.");
        }
            
    }

    
}
  •  Tags:  
  • c#
  • Related