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.
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.
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
Here is sample
String temp;
try {
temp = "Sample Text";
// your code
}
catch {
//instead
}
Finally
{
temp="";
}
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.
ASKER
Correction, I meant to say that I do assignment to the variable "inside of the try block"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
Thank you.
Please consider using something similar to this for now:
I will work to get rid of:
private DataGridView DataGridView()
{
throw new NotImplementedException();
}
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();
}
I will work to get rid of:
private DataGridView DataGridView()
{
throw new NotImplementedException();
}
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.
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.
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.
The scope is not fine. Have it before try.