Avatar of stephenlecomptejr
Flag for United States of America

asked on 

How to overcome unassigned local variable error?

I have no clue as to why I'm getting the Use of unassigned local variable 'myCounter' error.  The highlight is on the second myCounter in the following statement:  myCounter = myCounter + 1;
I'm just trying to have a variable act as a counter and then adds the total number of files found in a directory.  Thanks in advance for the answer.

Here's the full code listing:

private void btnSeek_Click(object sender, EventArgs e)
            long myCounter;
            string b4Change;
            string Directions;

            b4Change = txtPath.Text;

            //string theName;
            Directions = fBDialog.SelectedPath;

            if (Directions.Length <= 4 & Directions.Length > 1)
                txtPath.Text = b4Change;
                MessageBox.Show(Directions + " drive location too large to search.  Please browse to a subfolder or create one before attempting your search.");

            //seek options start here.


            string[] fileEntries = Directory.GetFiles(txtPath.Text);
            foreach (string fileName in fileEntries)
                // do something with fileName
                Directions = Path.GetFileName(fileName);
                //theName = Path.GetFileName((fileName));
                //theName = (fileName);
                myCounter = myCounter + 1;
            txtFound.Text = Convert.ToString(myCounter);

            //change main form options here.

            tbOptions.SelectedTab = tabPage2;
            txtDirections.Text = "Step 3.  Click on Rename Files button to actually perform final action.";

Avatar of undefined
Last Comment
Joel Coehoorn

8/22/2022 - Mon