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()
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()
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?
private Shape myShape;
Am i right?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
class test
{
private Shape myShape;
function a()
{
myShape = new Shape();
myShape.Color = SomeColor;
}
function b()
{
Color color = myShape.Color;
}
}