Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Suppose We have 2 MEthods. I have declared one object in First Method and i want to use that object in second method.?? How can i do that?/
Posted

Send the object to the 2nd method via a parameter.
You could either send it as a parameter or a REF

C#
public void Method1()
{
object myObject = new object;
Method2(ref myObject);
}
public void Method2(ref object r_obj)
{
object useOtherObject = r_obj;
}
 
Share this answer
 
Comments
fjdiewornncalwe 5-Dec-12 8:58am    
My 5.
Rob Branaghan 5-Dec-12 9:53am    
Thank you Marcus :)
But try this simple way :-
C#
public class MyForm : Form 
{
     Employee e;

    private void OnLoad()
    {
       e = new Employee ();
    }

    private void Display()
    {
      // use e 
    }

}
 
Share this answer
 
Comments
Nikhil@123 5-Dec-12 8:50am    
hi,
You are declaring a variable in class and using it in different method, i dont want that i wants to use a Object from 1 method to another method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900