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);
}
}
}
Asked
Active
Viewed 55 times
-4

cHao
- 84,970
- 20
- 145
- 172
-
It would be helpful if you actually asked the question... – silvo Sep 14 '14 at 08:03
-
@Amol Pawal did you get your answer? – Masoud Mohammadi Sep 24 '14 at 16:47
3 Answers
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