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