FolderBrowserDialog Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sean Campbell

    #1

    FolderBrowserDialog Question

    Hi there!

    I have a form with a button and a label.

    Can anyone offer some insight as to why the following code snippet gives me
    the following error: "Use of unassigned local variable 'path2'? If I remove
    the variable all together and assign the label1.text = dialog.Selected Path
    then the code works but I don't understand why. I hope someone can shed some
    light on the subject. Thanks.

    Sean Campbell

    private void button1_Click(o bject sender, EventArgs error)
    {
    string path2;
    try
    {
    FolderBrowserDi alog dialog = new FolderBrowserDi alog();
    dialog.ShowNewF olderButton = false;
    if (dialog.ShowDia log() == DialogResult.OK )
    {
    path2 = dialog.Selected Path;
    }

    label1.Text = path2;
    }
  • C.C. \(aka Me\)

    #2
    Re: FolderBrowserDi alog Question

    If dialog.ShowDial og() != DialogResult.OK then path2 never gets initialized.

    To fix it all you have to do is change

    string path2;

    to

    string path2 = "";

    That way path2 has been initialized.

    The other option is to move the label1.Text=pat h2 line inside the if()
    statement since you probably only want to set the Text if they click Ok
    anyway?

    --
    ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~
    Charles Cox
    VC/VB/C# Developer
    ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~

    "Sean Campbell" <SeanCampbell@d iscussions.micr osoft.com> wrote in message
    news:84DD0690-1DBB-4533-9A1A-448E70704C6D@mi crosoft.com...[color=blue]
    > Hi there!
    >
    > I have a form with a button and a label.
    >
    > Can anyone offer some insight as to why the following code snippet gives
    > me
    > the following error: "Use of unassigned local variable 'path2'? If I
    > remove
    > the variable all together and assign the label1.text = dialog.Selected Path
    > then the code works but I don't understand why. I hope someone can shed
    > some
    > light on the subject. Thanks.
    >
    > Sean Campbell
    >
    > private void button1_Click(o bject sender, EventArgs error)
    > {
    > string path2;
    > try
    > {
    > FolderBrowserDi alog dialog = new FolderBrowserDi alog();
    > dialog.ShowNewF olderButton = false;
    > if (dialog.ShowDia log() == DialogResult.OK )
    > {
    > path2 = dialog.Selected Path;
    > }
    >
    > label1.Text = path2;
    > }[/color]


    Comment

    • Sean Campbell

      #3
      RE: FolderBrowserDi alog Question

      Thanks for the help. Works great!

      "Sean Campbell" wrote:
      [color=blue]
      > Hi there!
      >
      > I have a form with a button and a label.
      >
      > Can anyone offer some insight as to why the following code snippet gives me
      > the following error: "Use of unassigned local variable 'path2'? If I remove
      > the variable all together and assign the label1.text = dialog.Selected Path
      > then the code works but I don't understand why. I hope someone can shed some
      > light on the subject. Thanks.
      >
      > Sean Campbell
      >
      > private void button1_Click(o bject sender, EventArgs error)
      > {
      > string path2;
      > try
      > {
      > FolderBrowserDi alog dialog = new FolderBrowserDi alog();
      > dialog.ShowNewF olderButton = false;
      > if (dialog.ShowDia log() == DialogResult.OK )
      > {
      > path2 = dialog.Selected Path;
      > }
      >
      > label1.Text = path2;
      > }[/color]

      Comment

      Working...