[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

C# basic doubt

Posted on 2006-11-14
5
Medium Priority
?
177 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 8

Expert Comment

by:Rytmis
ID: 17936864
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
ID: 17936890
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 1000 total points
ID: 17936972
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
ID: 17937352
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

656 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