0

I have a simple code to work with getters and setters. I have created a object from book class in another class. But this object doesn't recognize by the class. Why is that? I get this error

An object reference is required for the nonstatic field, method, or property 'member'

class Book
{
    private int num;

    public void setNum(int no)
    {
        this.num = no;
    }      
    public int getNum()
    {
        return this.num;
    }


}

class Program
{
    Book bb = new Book();
    public static void Main()
    {
        bb.setNum(10);
        Console.WriteLine("Insert value" + bb.getNum);
    }
}
Joe
  • 45
  • 1
  • 9

1 Answers1

0

GetNum is a function, you have to add () to it to get the value.

If you don't do this, C# is looking for a property with this name and this does not exist.

Jerodev
  • 32,252
  • 11
  • 87
  • 108