How do I use a variable declared in a method, outside that method?

后端 未结 6 1442
眼角桃花
眼角桃花 2021-01-27 00:26

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

6条回答
  •  不思量自难忘°
    2021-01-27 01:07

    I guess what you want is

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

    which gives back the OpenFileDialog object. Now you can

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

提交回复
热议问题