471,627 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,627 software developers and data experts.

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.SelectedPath
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(object sender, EventArgs error)
{
string path2;
try
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.ShowNewFolderButton = false;
if (dialog.ShowDialog() == DialogResult.OK)
{
path2 = dialog.SelectedPath;
}

label1.Text = path2;
}
Jan 29 '06 #1
2 1331
If dialog.ShowDialog() != 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=path2 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" <Se**********@discussions.microsoft.com> wrote in message
news:84**********************************@microsof t.com...
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.SelectedPath
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(object sender, EventArgs error)
{
string path2;
try
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.ShowNewFolderButton = false;
if (dialog.ShowDialog() == DialogResult.OK)
{
path2 = dialog.SelectedPath;
}

label1.Text = path2;
}

Jan 29 '06 #2
Thanks for the help. Works great!

"Sean Campbell" wrote:
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.SelectedPath
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(object sender, EventArgs error)
{
string path2;
try
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.ShowNewFolderButton = false;
if (dialog.ShowDialog() == DialogResult.OK)
{
path2 = dialog.SelectedPath;
}

label1.Text = path2;
}

Jan 30 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by hhh12347 | last post: by
1 post views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.