|
Nitpicking here, but you forgot if (fs != null) . The finally block looks like
if (fs != null)
((IDisposable)fs).Dispose();
|
|
|
|
|
Spot on. Sorry and all that.
|
|
|
|
|
Pete O'Hanlon wrote: because (presumably) the developers have identified that particular items have to be freed up probably because they are expensive to maintain.
Great. And isn't this very similar to responsibility for memory allocation and freeing that makes a Garbage Collection environment so desirable? I mean you'd think a VB developer that can actually understand the Dispose Pattern could handle new and delete. I'm just saying...
led mike
|
|
|
|
|
I don't call dispose on streams, but I do call close(). Isn't that the same thing?
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
John Simmons / outlaw programmer wrote: I don't call dispose on streams, but I do call close(). Isn't that the same thing?
Yes, as long as you guarantee that Close() is called. As I stated earlier, the using statement wraps a try/finally block, so you need to ensure that Close is called in the finally portion.
|
|
|
|
|
|
John Simmons / outlaw programmer wrote: using (using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
I think this statement is wrong. You can use nested using statements in this way
using(DisposableObject1)
using(DisposableObject2)
{
}
John Simmons / outlaw programmer wrote: but doesn't the same thing happen when the object goes out of scope?
Do you mean it will be disposed when the scope ends ? GC will handle it, but there would be a delay in disposal. This will produce same IL which try/finally is producing. So it's just a syntactic shortcut to call Dispose() method which ensures immediate disposal.
|
|
|
|
|
John Simmons / outlaw programmer wrote: but doesn't the same thing happen when the object goes out of scope?
Yes but garbage collection is non-deterministic and using offers deterministic resource clean up. I'm just the messenger, don't shoot me!
led mike
|
|
|
|
|
GC and dispose are also doing different stuff.
GC frees the memory allocated by the managed object.
Dispose is supposed to release native resources allocated by an object.
So even if you call dispose, the managed object is still present untill it gets eaten by the GC.
|
|
|
|
|
John Simmons / outlaw programmer wrote: I realize that this ensures that everything will be properly closed and disposed without you having to do anything, but doesn't the same thing happen when the object goes out of scope?
No, it doesn't. The memory management doesn't use reference counting, which means that nothing happens because of an object going out of scope.
Because there is no possibility to add any code that will run when the object goes out of scope, the IDisposable interface was created, so that the lifetime of an object could be controlled from the code.
Ordinary objects that doesn't have a specific need for any cleanup, doesn't have a Dispose method. They will just be removed when the garbage collector notices that the can be.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
>>so that the lifetime of an object could be controlled from the code.
rather: Lifetime of resources used by an object could be controlled from the code.
The disposable object itself will not be removed from memory when you call dispose on it..
|
|
|
|
|
Roger Alsing wrote: rather: Lifetime of resources used by an object could be controlled from the code.
Good point. I should have said that it's for controlling the life cycle of the object rather than the lifetime.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
John Simmons / outlaw programmer wrote: I realize that this ensures that everything will be properly closed and disposed without you having to do anything, but doesn't the same thing happen when the object goes out of scope?
Not when the object goes out of scope, but when the GC runs and that too only if the object has a finalizer. Garbage collection cleans up the object itself; it doesn't attempt to clean up any resources held by the object. IDisposable (and the finalizer) do the resource cleanup part.
|
|
|
|
|
See http://www.codeproject.com/KB/cs/tinguusingstatement.aspx[^]
John Simmons / outlaw programmer wrote: Is it wrong to not want to sue this construct in favor of more obvious code?
Do you want to type out all the try-finally-dispose things again and again, or understand using once and save the typing (AND reading)?
edit: omfg, it took me 30min to write this and find a link there wasnt any replies when i started typing...
[ My Blog] "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
dnh wrote: edit: omfg, it took me 30min to write this and find a link there wasnt any replies when i started typing...
Sorry - I just typed my answer from memory which was why I missed a null check.
|
|
|
|
|
What's with the extra using ( ?
|
|
|
|
|
Wasn't this the OP's question?
-Spacix
All your skynet questions[ ^] belong to solved
I dislike the black-and-white voting system on questions/answers.
|
|
|
|
|
|
Does anyone know a simple way to do this?
I suppose I could convert the createTimeStamp object to a string, and then use a terrible method to break the pieces down to individual objects, then pass those objects to another method through interfaces that creates the DateTime object, however that seems awefully redundant and I am sure someone has a much better way of doing it.
Any help is appreciated.
psuedocode sample (of what I would "like" to do):
foreach (SearchResult result in results)
{
int date = Convert.ToDateTime(result.Properties["createTimeStamp"][0]);
DateTime today = DateTime.UtcNow;
TimeSpan diff = today - date;
}
modified on Tuesday, May 6, 2008 10:09 AM
|
|
|
|
|
You know, I have been working on this for hours, and by posting this and then modifying it with the psuedocode, I ended up answering my own question.
int date = ...
should read
DateTime date = ...
Thank to anyone who put any thought into this.
Scot
|
|
|
|
|
I have a series of fairly large enums, and each value within them has a free text equivalent.
Within my code, I need to get the free text for a given enum value. Currently I am using a fairly traditional method of doing this, i.e. a switch/case on the enum value which obtains the text description.
However this is proving to be a pain to maintain, so does anybody know of a way of adding the free text as an attribute of the enum. For example
public enum MyEnum
{
[Description("This Wibbles")]
Value1,
[Description("This Wobbles")]
Value2,
[Description("Ding Dong")]
Value3
}
|
|
|
|
|
See here [^].
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
The description attribute is exactly what I personally use for this, with a simple helper method to obtain the description from the enum item:
<code>public static class EnumHelper
{
public static string GetDescription(System.Enum enumValue)
{
FieldInfo fi = enumValue.GetType().GetField(enumValue.ToString());
object[] descAttribs = fi.GetCustomAttributes(typeof(DescriptionAttribute), true);
if (descAttribs.Length == 0)
return null;
return ((DescriptionAttribute)descAttribs[0]).Description;
}
}</code>
Usage:
string description = EnumHelper.GetDescription(MyEnum.Value1);
Hope that helps.
|
|
|
|
|
Thanks alot, that works perfectly
Out of curiosity, is it possible to add object type attributes?
for example
public enum EnumAlpha
{
value1,
value2
}
public enum EnumBeta
{
[ObjectDescription(EnumAlpha.value1)]
value1,
[ObjectDescription(EnumAlpha.value2)]
value2,
}
|
|
|
|
|
I dont see what you're trying to achieve, but the answer is yes, you can define your own attributes and markup your code with those attributes.
|
|
|
|