Question Why must a variable assignment statement be in a method?

failedtofail

Member
Joined
Jun 16, 2022
Messages
20
Programming Experience
Beginner
Hi

I have 3 classes, viz. MyClass1, My Class2, MyClass3 (note - did not bother with Constructors for MyClass 2 and 3 here). I was playing around instantiating MyClass2 and MyClass3 in MyClass1 and calling on methods in MyClass2 and 3. Got it to work but noticed something:

Following code works:

C#:
 class MyClass1
    {
        private int age;
        private string _name;
        
        public MyClass1()
        {
            //Constructor code
        }
        
        public string Name
        {
            get { return _name; }
            set
            {
                //code here to validate UI input for example
                Object2.FuntionA();
            }
        }
        
        public int MyMethod()
        {
            age = Object2.FuncB();
            Object2.variable1 = Object3.FuncA();
        }

        MyClass2 Object2 = new MyClass2();
        MyClass3 Object3 = new MyClass3();
    }

However, when I put the assignment statements in MyMethod outside the method then errors result:

C#:
class MyClass1
    {
        private int age;
        private string _name;
        
        public MyClass1()
        {
            //Constructor code
        }
       
        public string Name
        {
            get { return _name; }
            set
            {
                //code here to validate UI input for example
                Object2.FuntionA();
            }
        }
        
        public int MyMethod()
        {
            // come code
        }

        age = Object2.FuncB();  // Wrong - error
        Object2.variable1 = Object3.FuncA(); // Wrong - error

        MyClass2 Object2 = new MyClass2();
        MyClass3 Object3 = new MyClass3();

This tells me that assignment statements need to be encapsulated in a method or getters/setters of properties (which I suppose are methods as well).

Can someone please explain why the assignment statement cannot be on its own in a class?

Thanks.
 
Because that's how the language is defined. Statements can only be in methods.
 
Does it really matter why? Even if the reasons are dumb, that's still the way it is. The only statements allowed at the class level are declarations. You can assign a value to a field where you declare it, e.g.
C#:
private SomeType field1 = someValue;
private SomeOtherType field2 = new SomeOtherType {SomeProperty = someOtherValue};
Any other assignments that need to be done when the type or an instance of the type are initialised need to be in a constructor. That's how it is.
 
Can someone please explain why the assignment statement cannot be on its own in a class?
Or let's try a different tack on the question: Let's say you modify the compiler and support putting assignment statements anywhere in the class declaration. What order should they be executed in? If you have a partial class declared across multiple files, and each of those have free floating assignments, what order should those be executed in?
 
Back
Top Bottom