-3

For some reason my program gives me an error, telling me "Use of unassigned local variable 'path'", it's really god damn annoying, tried restarting visual studio ( 2017 community ) several times and to no avail, tried to rebuild Solution... nothing is working for some reason..

enter image description here

using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InterfacedStorage
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            OpenFile();
        }

        public void OpenFile()
        {
            OpenFileDialog openFile = new OpenFileDialog();
            string path; // Declared path
            if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                path = openFile.FileName; // Input path from a file
            }


            Excel excel = new Excel(path, 1); // Path is not declared...

            MessageBox.Show(excel.ReadCell(0, 0));
        }
    }
}
d219
  • 2,707
  • 5
  • 31
  • 36
  • I don't read "undeclared variable" anywhere in that image because that's not the actual error. Please don't change the words of an error message that you don't fully understand – Camilo Terevinto Feb 27 '18 at 11:40
  • 2
    compiler errors don't go away by restarting VS or rebuilding the solution. They go away by researching the error message and applying the changes that (in this case) you find in the first [result](https://www.google.de/search?client=opera&ei=hTOVWpSoN8rW5gLwyqOgCw&q=c%23+Use+of+unassigned+local+variable+&oq=c%23+Use+of+unassigned+local+variable+&gs_l=psy-ab.3..0j0i22i30k1l9.4443877.4443877.0.4444812.1.1.0.0.0.0.424.424.4-1.1.0....0...1c.1.64.psy-ab..0.1.424....0.IH7fRzy7UU0) – Mong Zhu Feb 27 '18 at 11:46
  • 2
    another rediculously absurd idea here would be to consult the [documentation of the compiler error](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0165). – Mong Zhu Feb 27 '18 at 11:48
  • unassigned != undeclared – Tedil Feb 27 '18 at 12:13

1 Answers1

4

Can't agree with the other two answers because they ignore the fact that the user may not select a file. There is a reason for "Use of unassigned local variable"

I would restructure/edit the function as follows

    public void OpenFile()
    {
        OpenFileDialog openFile = new OpenFileDialog();

        if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            string path = openFile.FileName; // Input path from a file

            Excel excel = new Excel(path, 1); // Path is not declared...

            MessageBox.Show(excel.ReadCell(0, 0));

        }

    }

I'd probably also put a try...catch in there to ensure that the user has selected a valid path (can't remember the defaults for OpenFileDialog).

spodger
  • 1,668
  • 1
  • 12
  • 16