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
123 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 33

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 33

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
 

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 33

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 33

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 33

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now