如何使用不同方法中的Object到另一个方法? [英] How to use Object from different Method in to another Method?

查看:89
本文介绍了如何使用不同方法中的Object到另一个方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





假设我们有2个方法。我在First Method中声明了一个对象,我想在第二个方法中使用该对象。我该怎么办?/

解决方案

通过参数将对象发送到第二个方法。

您可以将其作为参数或REF



  public   void  Method1()
{
object myObject = new 对象;
Method2( ref myObject);
}
public void Method2( ref object r_obj)
{
object useOtherObject = r_obj ;
}


但是试试这个简单的方法: -

  public   class  MyForm:Form 
{
Employee e;

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

私人 void 显示()
{
// 使用e
}

}


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?/

解决方案

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;
}


But try this simple way :-

public class MyForm : Form 
{
     Employee e;

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

    private void Display()
    {
      // use e 
    }

}


这篇关于如何使用不同方法中的Object到另一个方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆