-3
public class Player
{
    private string Name;
    private int Health = 100;
    private int Damage;

    public Player(string name, int health, int damage)
    {
        name = Name;
        health = Health;
        damage = Damage;
    }        
}

public class Enemy
{
    public void enemyTakeDamage()
    {
        int takenDamage;            
    }

    private string Name;
    private int Health = 100;
    private int Damage;
    public string enemyMessages;

    public Enemy(string name, int health, int damage)
    {
        name = Name;
        health = Health;
        damage = Damage;
    }        
}

class Program
{
    static void Main(string[] args)
    {
        bool dead;
        Player P1 = new Player("Zach", 100, 20);               
    }            
}

So I initialized an object called P1 with the name of "zach" and the hp of 100 and damage of 20, I cannot access those 3 variables elsewhere. These arent the private variables in the player class, Im talking about the ones in the class that are intialized by doing :

public Player(string name, int health, int damage)

I thought I can just call P1.health anywhere I wanted if it is public? any help is appreciated and this is my first text based console game I'm working on so other input is also appreciated.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Zach
  • 61
  • 7
  • That is currently all the code, am I missing something in it? And I'll try referencing thing but how would I go about redefining it? I would prefer to reference it once and then be able to use its variables. – Zach May 08 '18 at 01:14
  • 2
    Possible duplicate of [Getter and Setter declaration in .NET](https://stackoverflow.com/questions/17881091/getter-and-setter-declaration-in-net) – OneCricketeer May 08 '18 at 01:16
  • The fact that your fields are private hides them to external classes. You can either make them public or add getters – OneCricketeer May 08 '18 at 01:18
  • 4
    Your class and constructor are public, but your class variables are declared private. – Edwin Chua May 08 '18 at 01:18
  • 2
    @Zeeeee, you should use properties, not fields, and at the bare minimum you need to make them `public` if you want to expose them. – Rufus L May 08 '18 at 01:19
  • So I realized from what you guys said that me setting the variables in the class private blocks it but I thought it could take the parameter variables... Now that I fixed that, what can I do to allow the new player (P1) to be initialized and able to be called from any class? just making it public? – Zach May 08 '18 at 01:29
  • your Constructor field assignment is the opposite. For ex: it should be: `Name = name`. This means you are assigning the argument in `name` parameter to class' field `Name`. – kurakura88 May 08 '18 at 01:48
  • Your player and enemy constructors are pretty obviously wrong. You're making no effort to use the instance's properties so I'm not sure what the point of your question is. – ProgrammingLlama May 08 '18 at 01:48

2 Answers2

1

If you want to reach properties/fields like p1.Health, you have to change it public.

Secondly, your assignment order is wrong.

Third, you can access p1.Health not p1.health, because Player does not has propert/field which is name health. It is constructor function's parameter name

public class Player
{
    public string Name;
    public int Health = 100;
    public int Damage;

    /* II. way
          public string Name {get;set;}
          public int Health {get;set;} = 100;
          public int Damage {get;set;}
      */

    /* III. way
        private string _name;
        private int _health = 100;
        private int _damage;

        public string Name {get { return _name ; } }
    */

    public Player(string name, int health, int damage)
    {
        Name = name;
        Health = health;
        Damage = damage;
    }
}

public class Enemy
{
    public void enemyTakeDamage()
    {
        int takenDamage;
    }

    private string Name;
    private int Health = 100;
    private int Damage;
    public string enemyMessages;

    public Enemy(string name, int health, int damage)
    {
        Name = name;
        Health = health;
        Damage = damage;
    }
}


public class Program
{
    static void Main(string[] args)
    {
        bool dead;
        Player P1 = new Player("Zach", 100, 20);
        Console.WriteLine("Name : " + P1.Name + Environment.NewLine +
                          "Health : " + P1.Health +Environment.NewLine +
                          "Damage : "+ P1.Damage);

        Console.ReadKey();
    }

    /* static player
    public static Player P1;
    static void Main(string[] args)
    {
       bool dead;
       P1 = new Player("Zach", 100, 20);
       Console.WriteLine("Name : " + P1.Name + Environment.NewLine +
                         "Health : " + P1.Health + Environment.NewLine +
                         "Damage : " + P1.Damage);

       Console.ReadKey();
    }
   */
}
Adem Catamak
  • 1,987
  • 2
  • 17
  • 25
  • I added everything you posted and also have the initialize command as so: public Player P1 = new Player("Zach", 100, 20); and it still wont show up in another class (btw its located the line under the end of the "main method" – Zach May 08 '18 at 01:37
  • Player P1 is only in Main scope. If you'd like to have that player being accessible to anyone, you need to declare that Player P1 as static. – kurakura88 May 08 '18 at 01:51
  • If you want to reach `properties` it will be like above code. If you want to create `Player` object and use it every where, it should be static like @kurakura88 says. – Adem Catamak May 08 '18 at 01:54
  • I tried adding static by doing both the combination "Public Static" And just "static" It let me access it in the main method but still not anywhere else, for an example I would want it be able to be called from the public void enemyTakeDamage() (Inside the "enemy class" )so I can take the players health and subtract it by the enemy's damage number. – Zach May 08 '18 at 02:01
1

Looking at everything, here are the definitions I think you're after:

public class Player
{
    public string Name { get; set; }
    public int Health { get; set; } = 100;
    public int Damage { get; set; }
    public bool IsAlive { get; set; }

    public Player(string name, int health, int damage)
    {
        Name = name;
        Health = health;
        Damage = damage;
        IsAlive = true;
    }

    public void TakeDamageFromEnemy(Enemy enemy)
    {
        if(IsAlive)
        {
            Health -= enemy.Damage;
            if (Health < 0)
            {
                IsAlive = false;
                MessageBox.Show("Player is dead!");
            }
        }
    }

    public void DoDamageToEnemy(Enemy enemy)
    {
        if(enemy.IsAlive)
        {
            enemy.Health -= Damage;
            if (Health < 0)
            {
                IsAlive = false;
            }
        }
    }
}

public class Enemy
{

    private string Name { get; set; }
    private int Health { get; set; }= 100;
    private int Damage { get;  set;}
    public string enemyMessages { get; set; }
    public bool IsAlive { get; set; } 

    public Enemy(string name, int health, int damage)
    {
        Name = name;
        Health = health;
        Damage = damage;
        IsAlive = true;
    }
    //This is likely replaced by the Player's DoDamageToEnemy method.
    public void enemyTakeDamage()
    {
        int takenDamage;
    }   
}


public class Program
{
    static void Main(string[] args)
    {
        Player playerOne = new Player("Zach", 100, 20);
        Enemy enemyOne = new Enemy("Trogg",15,5);
        Enemy enemyTwo = new Enemy("Dragon",1000,50);
        playerOne.TakeDamageFromEnemy(enemyTwo);
        playerOne.TakeDamageFromEnemy(enemyOne);
        playerOne.DoDamageToEnemy(enemyOne);
        playerOne.TakeDamageFromEnemy(enemyTwo);
        Console.ReadLine();
    }
}

Assuming Damage is how much damager a character puts out, then this should work as a combat model. You can Change the Do and Take Damage to Player.DoDamage(Enemy e) and Enemy.DoDamage(Player p) respectively.

  • Thank you! this is exactly what I want but I just want to make sure of one thing. You have a MessageBox.Show("Player is dead!"); is this just an example of what I can add because It says MessageBox doesnt exist in the current context. Thanks again :D – Zach May 08 '18 at 02:52
  • you would need a reference to System.Windows.Forms in your using statements. That being said, yes this is just a good example to get you to the place where you can start really going crazy. I've found having a working first draft of the code helps seeing how to make the code better later. – EntityFrameworkToday May 08 '18 at 03:22