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
122 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

707 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

18 Experts available now in Live!

Get 1:1 Help Now