Solved

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

Posted on 2014-12-25
11
125 Views
Last Modified: 2014-12-25
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.
0
Comment
Question by:lapucca
  • 6
  • 5
11 Comments
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40518154
re:>  used inside of the try block

The scope is not fine. Have it before try.
0
 

Author Comment

by:lapucca
ID: 40518158
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?
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40518161
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

0
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 

Author Comment

by:lapucca
ID: 40518164
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.
0
 

Author Comment

by:lapucca
ID: 40518165
Correction, I meant to say that I do assignment to the variable "inside of the try block"
0
 
LVL 34

Accepted Solution

by:
Mike Eghtebas earned 500 total points
ID: 40518170
I know your variable is not string but doing the following may make some difference in this case:

String temp="";   //intiallization
try {
0
 

Author Comment

by:lapucca
ID: 40518172
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.
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40518173
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();
        }
0
 

Author Comment

by:lapucca
ID: 40518178
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.
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40518180
Well, popup candler will look impressive. I will start working at it shortly.
0
 

Author Comment

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

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
RLDC Reporting in Visual studio 11 30
vb.net winforms sizing/resolution? 4 42
linq, c# 8 24
Code enhancement 4 22
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question