Solved

C# basic doubt

Posted on 2006-11-14
5
169 Views
Last Modified: 2010-04-16
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()
0
Comment
Question by:Rahamathulla_J
  • 2
5 Comments
 
LVL 8

Expert Comment

by:Rytmis
Comment Utility
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;
     }
}

0
 

Author Comment

by:Rahamathulla_J
Comment Utility
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?
0
 
LVL 15

Accepted Solution

by:
ozymandias earned 250 total points
Comment Utility
class test
{

     // declare shape
     private Share myShape;

     public test(){
          // instantiate shape
          myShape = new Shape();
     }

     function a()
     {
          set values to class shape
     }

     function b()
     {
          color = myShape.color
     }
}
0
 
LVL 8

Expert Comment

by:Rytmis
Comment Utility
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.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now