Link to home
Start Free TrialLog in
Avatar of Rahamathulla_J
Rahamathulla_J

asked on

C# basic doubt

I have 2 functions in a class

class test
{
      function a()
      {
            set values to class shape
      }

      function b()
      {
            color = shape.color
      }
}

In function b() want the value of shape class which was set in function a()
Avatar of Rytmis
Rytmis

This assumes you already have a Shape class with a Color property:

class test
{
     private Shape myShape;

     function a()
     {
          myShape = new Shape();
          myShape.Color = SomeColor;
     }

     function b()
     {
          Color color = myShape.Color;
     }
}

Avatar of Rahamathulla_J

ASKER

My problem is i am not able to declare myShape in the functon b. I hope i didnt declare globaly

private Shape myShape;

Am i right?
ASKER CERTIFIED SOLUTION
Avatar of ozymandias
ozymandias
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you declare myShape in function scope, you can't expect to get to it outside that function's scope. Instance variables are not global in scope, but are avaiable to all instance methods of a class.

Ozymandias' example instantiates myShape in the constructor so you don't have to do it in a() or b() but both can still access it.