-4

I'm trying to output a changing integer value in main used and created in multiple other classes. For learning purposes I'm seeing if it's possible to do what I'm trying to do and see what's causing the errors. Here's the code:

class Program
{
    static void Main(string[] args)
    {
        // how do I avoid an identifier error with unit.() if the other class is unit:stats?
        unit.();
        // how can I get the troop value from the unit:stats class?
        Console.WriteLine(troop);
    }
}
public class stats
{
    public int troop;
}
public class unit : stats
{
    public void archer()
    {
        troop = 100;
    }

}

1 Answers1

3

You need to have an "instance" of your object. So, in your main:

unit myUnit = new unit(); //Create an instance of unit
myUnit.archer(); //Run the method that sets the troop number
Console.WriteLine(myUnit.troop);//Access the troop value from myUnit
EZI
  • 15,209
  • 2
  • 27
  • 33
MikeH
  • 4,242
  • 1
  • 17
  • 32