Link to home
Start Free TrialLog in
Avatar of lapucca
lapucca

asked on

Why am I getting error disposing a couple of objects in the finally block of the try-catch?

Hi, I'm using VS2012.
In my Gridview control's Rowupdaiting event, I declare a GridViewRow variable then i start the try-catch-finally blocks.  When I reference this GridViewRow variable and want to access the Dispose() method, the code got red underline that shows error when I hover over it, "Use of unassigned local variable..."

The variable is assigned a value and used inside of the try block.  Why am I getting this variable?  the scope looks fine to me and it's confusing with this error.  Thank you.
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

re:>  used inside of the try block

The scope is not fine. Have it before try.
Avatar of lapucca
lapucca

ASKER

So, I have to have the assignment before the try block to be able to dispose it?  So it won't be able to catch error occur if assignment fail?
You can declare it before bu instantiate it after try. If you want to post that portion of the code if you need help.

Here is sample
String temp;
try {
  temp = "Sample Text";
  // your code
}
catch {
  //instead
}
Finally
{
  temp="";
}

Open in new window

Avatar of lapucca

ASKER

That's exactly what I did.  That's why it's mind boggling for me why it even complaint that I do assignment outside of the try block.  That is the same thing you're doing in your code example.  I declare above of the try block.  I then assign inside the try block...  It then gave error, red underline.
Avatar of lapucca

ASKER

Correction, I meant to say that I do assignment to the variable "inside of the try block"
ASKER CERTIFIED SOLUTION
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lapucca

ASKER

GridViewRow and UserPrincipal.  The only thing I can assign to during declaration is null.  Tried that and it worked.  Pretty annoying that it can't just accept a declaration without assignment.  
Thank you.
Please consider using something similar to this for now:
      private void Form1_Load(object sender, EventArgs e)
        {
            DataGridView dgv = DataGridView();
            try
            {
                dgv = new DataGridView();
                dgv.Name = "dataGridView1";
            }
            catch (Exception ex)
            {

            }
            finally 
            {
                dgv.Dispose();
            }
       
        }

        private DataGridView DataGridView()
        {
            throw new NotImplementedException();
        }

Open in new window


I will work to get rid of:
 private DataGridView DataGridView()
        {
            throw new NotImplementedException();
        }
Avatar of lapucca

ASKER

That's fine, it worked with null assignment.  Thank you.

I need help with the DAte/time column.  It's too hard with template field to find a use a regular expression that would validate date./time format.  And it's too complicated for users.  I think I need to find a way to pop up a calendar that allow users to enter date and time for that column in Gridview control.  This is the last thing that I need to do for this application.
Well, popup candler will look impressive. I will start working at it shortly.
Avatar of lapucca

ASKER

Thank you very much!  You can answer to that other question where you were helping me with formatting the output of the template field.