如何在方法之外使用在方法中声明的变量? [英] How do I use a variable declared in a method, outside that method?

查看:56
本文介绍了如何在方法之外使用在方法中声明的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS 2008(C#)...我已经在GlobalClass中创建了一个函数以全局使用它..这是用于打开对话框的.当我在方法中调用此函数时,它可以工作,但是我无法使用在此创建的对象"OFD" ...

I am using VS 2008 (C#)... I have created a function in a GlobalClass to use it globally.. This is for opening a dialog box. When I call this function in my method it works but I am not able to use the Object "OFD" that I have created here...

static class GlobalClass
{
 public static void OFDbutton()
  {
   OpenFileDialog ofd = new OpenFileDialog();
   ofd.Filter = "Image files|*.jpg;*.jpeg;*.png;*.gif";
   DialogResult dr = ofd.ShowDialog();
  }
}

在表单方法中.我正在使用

In the form method. I am using

globalclass.ofdbutton(); //Calling the function
lable1.text=ofd.filename;

我想使用对象"ofd",但无法使用..为此,我必须做的事,请帮忙

I want to use object "ofd" but I am unable to do so.. What I have to do about this, please help

推荐答案

我想你想要的是

static class GlobalClass
{
 public static OpenFileDialog OFDbutton()
  {
   OpenFileDialog ofd = new OpenFileDialog();
   ofd.Filter = "Image files|*.jpg;*.jpeg;*.png;*.gif";
   DialogResult dr = ofd.ShowDialog();
   return ofd;
  }
}

会返回 OpenFileDialog 对象.现在您可以

which gives back the OpenFileDialog object. Now you can

OpenFileDialog ofd = globalclass.ofdbutton(); //Calling the function
label1.text=ofd.filename;

这篇关于如何在方法之外使用在方法中声明的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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