none
Consider the following code and reply RRS feed

  • Question

  • using System;

    namespace abc{

    public class A{

    public int i;

    }

    public class B{

    A objectOfA=new A();

    objectOfA.i=10;// this is not working , i can't even see objectOfA in intellisense in both VS2010&12 

    }

    }

    Monday, February 9, 2015 10:25 AM

Answers

  • You seem to be trying to execute code, i.e. setting the i property of objectOfA, with no context. That line of code isn't in a constructor or method or anything. At which point do you expect it to run?

    You can initialise fields at the point of class creation (as you are doing with the line "A objectOfA = new A()") but you can't do anything else. You would need to put the other line in the constructor for class B.

    public class B{
    
    A objectOfA=new A();
    
    // Constructor
    public B()
    {
       objectOfA.i=10;
    }
    
    }

    Note that Wall.E's previous comment about using properties (i.e. getter/setting methods) rather than directly accessing another class's fields is also valid.

    Monday, February 9, 2015 12:08 PM
  • To simply initialize i, you can use the object-initializer syntax when you are creating A, like this:

    A objectOfA = new A() { i = 10 };

    In your code, you have written an assignment statement directly into the class but haven't put it in the body of any method.  Don't confuse field declarations that have variable initializers with statements.  To put arbitrary code that will execute a statement when B is constructed, then you'll have to add a constructor for B.  Doing so would be an alternate solution to your problem.  See below:

    public class B{
        A objectOfA = new A(); // field declaration with variable initializer
    
        // Declare a constructor for B
        public B()
        {
            objectOfA.i = 10; // assignment statement
        }
    }


    • Edited by Wyck Monday, February 9, 2015 1:13 PM A couple of comments for more clarity.
    • Proposed as answer by cheong00Editor Tuesday, February 10, 2015 8:30 AM
    • Marked as answer by Fred BaoModerator Wednesday, February 18, 2015 7:08 AM
    Monday, February 9, 2015 1:11 PM

All replies

  • use methods, getter/setter to assign or retrieve values.
    Monday, February 9, 2015 11:31 AM
  • You seem to be trying to execute code, i.e. setting the i property of objectOfA, with no context. That line of code isn't in a constructor or method or anything. At which point do you expect it to run?

    You can initialise fields at the point of class creation (as you are doing with the line "A objectOfA = new A()") but you can't do anything else. You would need to put the other line in the constructor for class B.

    public class B{
    
    A objectOfA=new A();
    
    // Constructor
    public B()
    {
       objectOfA.i=10;
    }
    
    }

    Note that Wall.E's previous comment about using properties (i.e. getter/setting methods) rather than directly accessing another class's fields is also valid.

    Monday, February 9, 2015 12:08 PM
  • To simply initialize i, you can use the object-initializer syntax when you are creating A, like this:

    A objectOfA = new A() { i = 10 };

    In your code, you have written an assignment statement directly into the class but haven't put it in the body of any method.  Don't confuse field declarations that have variable initializers with statements.  To put arbitrary code that will execute a statement when B is constructed, then you'll have to add a constructor for B.  Doing so would be an alternate solution to your problem.  See below:

    public class B{
        A objectOfA = new A(); // field declaration with variable initializer
    
        // Declare a constructor for B
        public B()
        {
            objectOfA.i = 10; // assignment statement
        }
    }


    • Edited by Wyck Monday, February 9, 2015 1:13 PM A couple of comments for more clarity.
    • Proposed as answer by cheong00Editor Tuesday, February 10, 2015 8:30 AM
    • Marked as answer by Fred BaoModerator Wednesday, February 18, 2015 7:08 AM
    Monday, February 9, 2015 1:11 PM
  • not working
    Monday, February 9, 2015 3:11 PM
  • not working
    What is not working? Post the code you have now and explain what the problem is. It would also help if you tell us what version of Visual Studio you are using because some of the solutions won't work in old versions of Visual Studio.
    Monday, February 9, 2015 3:34 PM
  • not working

    The code I posted compiles and works.

    Perhaps you'd like to share with us the code that you tried?

    Remember, you can't put a statement in a class.  [even if it refers to a field of the class.]

    public class B{
    
    A objectOfA=new A();
    
    objectOfA.i = 10; // error CS1519: Invalid token '=' in class, struct, or interface member declaration
    
    }

    This should work, provided this is exactly what you're trying to do.  

    public class B
    {
        A objectOfA = new A() { i = 10 };
    }

    If you're trying to do something more complex than i = 10, then you should really use a constructor as demonstrated in the code that RJP1973 and I have previously posted that uses a constructor for B.


    Monday, February 9, 2015 5:06 PM
  • The second variant of Wyck's code should work in all versions of Visual Studio way back to VS2002.NET. He accurately nailed the issue in original poster's code.

    All non-initializer statements must be placed inside a method, be it normal method, constructor or destructor.

    The object initializer syntax only works in .NET v3.5 or later, AFAIK, though.

    Tuesday, February 10, 2015 8:35 AM
    Answerer