Solved

C# basic doubt

Posted on 2006-11-14
5
170 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
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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Data Saving(2) 2 41
getting id from database 5 37
Calling stored proc in EDMX 11 23
Sum Column in GridView 3 42
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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!
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

930 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

13 Experts available now in Live!

Get 1:1 Help Now