Click here to Skip to main content
12,250,675 members (47,677 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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 5-Dec-12 1:59am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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

public void Method1()
{
object myObject = new object;
Method2(ref myObject);
}
public void Method2(ref object r_obj)
{
object useOtherObject = r_obj;
}
  Permalink  
Comments
Marcus Kramer 5-Dec-12 8:58am
   
My 5.
RobBranaghan 5-Dec-12 9:53am
   
Thank you Marcus :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

But try this simple way :-
public class MyForm : Form 
{
     Employee e;
 
    private void OnLoad()
    {
       e = new Employee ();
    }
 
    private void Display()
    {
      // use e 
    }
 
}
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160426.1 | Last Updated 5 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100