8 Replies - 530 Views - Last Post: 02 February 2013 - 03:48 AM Rate Topic: -----

#1 nighttrain  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 22-September 10

Using vs finally

Posted 01 February 2013 - 02:31 AM

Hi there all good people,

I am using a lot of excel objects, and to be sure all have been completly closed i am using marshall realase inside finnaly block of try.
Some time ago i found out about using clause which is implementing Dispose automatically when using clause is ending. Could you tell me if this is really best why to realase objects e.g excel or other i mean if i put my code inside using clause it is for sure that every object would be realased? My question is because i heard somewhere that using clause is the same as realase some objects like in Finally clause of try. Could you follow me up about that?

P.S Another question regarding Len function - in documentation i read that the function is for strings. So every time i when i want to count lenght of integer i am always firstly converting the integer to string and then use with Len, is there another function like Lenghth for strings - for integers?

br

This post has been edited by nighttrain: 01 February 2013 - 02:33 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Using vs finally

#2 maxrock  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 70
  • Joined: 13-September 09

Re: Using vs finally

Posted 01 February 2013 - 04:56 AM

Using ensures that object is disposed when exiting. May it be normal exit or exit due to exception.
It basically equivalent to a try catch finally having dispose statements in finally block.

Please also refer:MS library

Could not understand your other example. please elaborate with example and purpose.

Regards,
Was This Post Helpful? 0
  • +
  • -

#3 nighttrain  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 22-September 10

Re: Using vs finally

Posted 01 February 2013 - 06:52 AM

Ok so its like i thought since i am using the try/catch/finally - and inside finally i am realasing the used objects in try clause its equal as i would like to use 'using clause'... So its not guaranty let's say 'stronger' way to realase the object...
Correct me if i am wrong please... And now the question is which one is better to use?? ;/ I found out below info about that could you agree with that?:

'The using construct is shorthand for a try/finally block. That is, the compiler generates:
FileStream fs = null;
try
{
    fs = File.Open(path);
  '  // ...
}
finally
{
    if (fs != null)
        fs.Dispose();
}

'So it is proper to use using if you do not need a catch, but if you do then you should just use a normal try/catch/finally.



------------------------
P.S The example:

var.ToString.Length > 0   'var is the numeric value so i am converting it to string cause i want to use the Lenght function


This post has been edited by nighttrain: 01 February 2013 - 07:13 AM

Was This Post Helpful? 0
  • +
  • -

#4 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: Using vs finally

Posted 01 February 2013 - 12:03 PM

The benefits of the using statement vs an explicit try-finally for IDisposable resources, in my eyes, are as follows:

1) A using statement narrows the scope of the resource to inside the using block, where as with your explicit try-finally, the scope of fs is wider, and you could therefore potentially use fs after it has been disposed.

2) To me, a using statement is cleaner, and more focused. It removes some visual noise, and boilerplate code, therefore making the intent of the code clear, but keeping the underlying mechanics hidden. This helps readability.

3) You don't have to remember to call Dispose() when using a using statement.

Quote

'The using construct is shorthand for a try/finally block. That is, the compiler generates:


Almost yes, with just one slight change in the finally block (in C#, since your example was in C#):

Spoiler


Quote

'So it is proper to use using if you do not need a catch, but if you do then you should just use a normal try/catch/finally.


You can use using statements, and still catch exceptions, simply by wrapping the call that may throw an exception in a try-catch, as usual.


Finally, there are occasions when it is genuinely more appropriate to use a try-finally, than a using statement, even on IDisposable resources. For example, when you have nested IDisposable resources, as shown here.

This post has been edited by CodingSup3rnatur@l-360: 01 February 2013 - 03:15 PM

Was This Post Helpful? 0
  • +
  • -

#5 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: Using vs finally

Posted 01 February 2013 - 12:21 PM

Quote

var.ToString.Length > 0 'var is the numeric value so i am converting it to string cause i want to use the Lenght function


Well, that seems like a reasonable solution, to be honest. No need to complicate things when you have a simple solution that does what you need :)

However, if you really want to avoid converting the integer to a string, you could write an extension method. Something quick and dirty like:

<Extension()> _
Public Function CountDigits(ByVal i As Integer) As Integer
    Dim positiveNum As Integer = Math.Abs(If(i <> Integer.MinValue, i, i + 1))
    If positiveNum = 0 Then Return 1
    Return Convert.ToInt32(Math.Floor(Math.Log10(positiveNum) + 1))
End Function



That particular example only counts digits, and doesn't count '-' signs like the ToString() solution does.

You can then call it like:

Dim noOfDigits As Integer = var.CountDigits()

This post has been edited by CodingSup3rnatur@l-360: 01 February 2013 - 12:37 PM

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3490
  • View blog
  • Posts: 11,902
  • Joined: 12-December 12

Re: Using vs finally

Posted 01 February 2013 - 01:00 PM

Does Using, though, properly handle and dispose of the COM (Excel) resource? Does it mean that we can avoid complicated code such as:

// Cleanup
GC.Collect();
GC.WaitForPendingFinalizers();

Marshal.FinalReleaseComObject(xlRng);
Marshal.FinalReleaseComObject(xlSheet);

xlBook.Close(Type.Missing, Type.Missing, Type.Missing);
Marshal.FinalReleaseComObject(xlBook);

xlApp.Quit();
Marshal.FinalReleaseComObject(xlApp);

Was This Post Helpful? 0
  • +
  • -

#7 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: Using vs finally

Posted 01 February 2013 - 02:23 PM

I admittedly have done very little Office Interop, so I'm not probably not the best person to ask about that subject :)

However, if the Office Interop objects don't implement IDisposable, then my first instinct would probably be to create IDisposable wrappers around the objects I require.
Was This Post Helpful? 1
  • +
  • -

#8 nighttrain  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 22-September 10

Re: Using vs finally

Posted 02 February 2013 - 03:07 AM

@andrewsw:
That was why i was asking about using - to be just make excel objects behind the using clause and rich the point and avoid also the problem with not realases objects e.g in memory...
I am still not sure about that so i think i will stay with try/ctach/finally block as i am using till now with almost 200 reports... I was workingfor a long time to prepare approach to handle excel object after work but afterwaords now i have no problems i am just realasing them in Finally block, cause i am not belive that just using statement do the work :)/>.

But thank you all guys for help. The conslusion what i have now is to use 'using' with not complicated and advance objects like for instance Excel...

thank you all again for helpfull conversation

This post has been edited by nighttrain: 02 February 2013 - 03:11 AM

Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3490
  • View blog
  • Posts: 11,902
  • Joined: 12-December 12

Re: Using vs finally

Posted 02 February 2013 - 03:48 AM

Yes, Excel does not implement IDisposable so it would be necessary to create an IDisposable wrapper around the Excel COM object, as mentioned:

Quote

..it is important to understand that since the purpose of Using is to create a scope for an object where Dispose is automatically called upon leaving the scope that this requires that the object actually implements IDisposable. Using an object that does not implement IDisposable will result in an error, just as manually calling Dispose for the object in a Finally block would result in an error. For a list of objects in the framework that implement IDisposable you can look at the IDisposable definition in the docs. However, with that said, you should also implement IDisposable in your own classes that use unmanaged resources such as Interop etc.

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1