1

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;

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

0

6 Answers 6

4

You might want to rework your method to actually return the filename instead.

Something like

public static string OFDbutton()
{
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = "Image files|*.jpg;*.jpeg;*.png;*.gif";

    if (ofd.ShowDialog() == DialogResult.OK)
        return ofd.Filename;
    else
        return string.Empty;
}

Of course, this is a very naive approach, and you might want to read up on variable scope and object oriented design in general.

Edit: This answer expands on the issue and improves the design, taking into account that the user may have clicked cancel in the dialog itself.

Edit2: Shamelessly copying from the linked answer, I modify my own snippet.

2

When you declare a variable inside a method, then the variable is scoped to that method.

if you want to be able to use that variable outside that method as well, you'll have two options:

Return the variable:

    public static string OFDMethod()
    {
       using( var ofd = new OpenFileDialog() )
       {   
           ofd.Filter = "Image files|*.jpg;*.jpeg;*.png;*.gif";
           if( ofd.ShowDialog() == DialogResult.OK )
           {
               return ofd.Filename;
           }
           else
           {
                return string.Empty;
           }
       }
    }

or make an out parameter for that variable (which I'd certainly not prefer in this case)

    public static void OFDMethod(out string selectedFilename)
    {
       using( var ofd = new OpenFileDialog() )
       {   
           ofd.Filter = "Image files|*.jpg;*.jpeg;*.png;*.gif";
           if( ofd.ShowDialog() == DialogResult.OK )
           {
               selectedFilename = ofd.Filename;
           }
           else
           {
                selectedFilename = string.Empty;
           }
       }
    }
0
1

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;
2
  • Thanks, I was exactly looking for this.... I was returning the ofd but the return type I was using was string that is why I was not able to use the object. I needed the object "ofd" as a whole not alone ofd.filename as per my project requirement........
    – Jain
    Jul 11, 2012 at 14:16
  • @Jain Your shown code only used ofd.Filename, which is why most everyone answered by returning just ofd.Filename. You should never return more than you need. It wasn't clear that you needed more, but it's good that you found a solution that works for you. =)
    – J. Steen
    Jul 12, 2012 at 8:29
0

Either change the method to return the filename or the dialog object itself Or move the openfiledialog into an seperate property outside the method

0

Do it like this -

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

lable1.text = GlobalClass.OFDbutton();
0

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

You can't. You can move the declaration to outside the method. Then it becomes a field of the containing class.

But as many others have said, in this case it's better to return the filename.

The only things a method "exhibits" to the outside world, are the parameters (it might mutate the objects they reference; or assign to them if they're ref or out) and the return value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.