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
128 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Amazon S3 .Net error 5 44
Securing WEBAPI on Azure 2 28
C# DataTable (in memory) vs SQL Lookups 4 38
Telerik RadEditor Control Save 8 20
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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

696 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