-4
using System;

namespace OOPSProject
{
    class HeadOffice
    {
        string mgr;
        mgr = "Mr.Amol Pawal";
        public void HeadOfficeAddress()
        {
            Console.WriteLine("Pune");
        }
        public void HeadOfficeManager()
        {
            Console.WriteLine("Manager is:" + mgr);
        }
    }
}
cHao
  • 84,970
  • 20
  • 145
  • 172

3 Answers3

1

You cannot assign a value to a variable (mgr in your case) out of class methods. Put mgr = "Mr.Amol Pawal"; in your constructor or use string mgr = "Mr.Amol Pawal";.

Ali Sepehri.Kh
  • 2,468
  • 2
  • 18
  • 27
1

mgr is a field it must be assigned with aconstructor or with declaration

1st way:in constructor

class HeadOffice
{
    public HeadOffice()
    {
        mgr = "Mr.Amol Pawal";
    }
    string mgr;
    public void HeadOfficeAddress()
    {
        Console.WriteLine("Pune");
    }
    public void HeadOfficeManager()
    {
        Console.WriteLine("Manager is:" + mgr);
    }
}

2nd way:with declaration

class HeadOffice
{
    string mgr= "Mr.Amol Pawal";;
    public void HeadOfficeAddress()
    {
        Console.WriteLine("Pune");
    }
    public void HeadOfficeManager()
    {
        Console.WriteLine("Manager is:" + mgr);
    }
}

if you don't know the difference between field and variable

see:What is the difference between field, variable, attribute, and property in Java POJOs?

Community
  • 1
  • 1
Masoud Mohammadi
  • 1,721
  • 1
  • 23
  • 41
0
string mgr = "Mr. Amol Pawal";
jayvee
  • 160
  • 2
  • 17