using(Image image = Image.FromFile("bell.gif"))

T

Tony Johansson

Hi!

Here is some code and I suspect that this code is coded wrong because this
code doesn't make sense with how the using is used here. I suspect that it
should have been codes like see at the bottom.
Or is it something that I miss here saying that the way they use the using
here is perfectly correct and valid ?

The book I read say the following about the using. It says "The using
statement specifies that the image resources should be automatically
displosed at the end of the using block"

ResXResourceWriter rw = new ResXResourceWriter("Demo.resx");
using(Image image = Image.FromFile("bell.gif"))
{
rw.AddResource("Mylogo",image);
rw.AddResource("Title","Professional C#");
rw.AddResource("Chapter", "Localization");
rw.AddResource("Author", "Chistian Nagel");
rw.AddResource("Publisher", "Wrox Press");
rw.Close();
}

//Here is how it should have been coded accoding to my understanding where
the instance ResXResourceWriter is
// implicitly using the dispose pattern
using(ResXResourceWriter rw = new ResXResourceWriter("Demo.resx"))
{
Image image = Image.FromFile("bell.gif");
rw.AddResource("Mylogo",image);
rw.AddResource("Title","Professional C#");
rw.AddResource("Chapter", "Localization");
rw.AddResource("Author", "Chistian Nagel");
rw.AddResource("Publisher", "Wrox Press");
}


//Tony
 
A

Arne Vajhøj

Hi!

Here is some code and I suspect that this code is coded wrong because this
code doesn't make sense with how the using is used here. I suspect that it
should have been codes like see at the bottom.
Or is it something that I miss here saying that the way they use the using
here is perfectly correct and valid ?

The book I read say the following about the using. It says "The using
statement specifies that the image resources should be automatically
displosed at the end of the using block"

ResXResourceWriter rw = new ResXResourceWriter("Demo.resx");
using(Image image = Image.FromFile("bell.gif"))
{
rw.AddResource("Mylogo",image);
rw.AddResource("Title","Professional C#");
rw.AddResource("Chapter", "Localization");
rw.AddResource("Author", "Chistian Nagel");
rw.AddResource("Publisher", "Wrox Press");
rw.Close();
}

//Here is how it should have been coded accoding to my understanding where
the instance ResXResourceWriter is
// implicitly using the dispose pattern
using(ResXResourceWriter rw = new ResXResourceWriter("Demo.resx"))
{
Image image = Image.FromFile("bell.gif");
rw.AddResource("Mylogo",image);
rw.AddResource("Title","Professional C#");
rw.AddResource("Chapter", "Localization");
rw.AddResource("Author", "Chistian Nagel");
rw.AddResource("Publisher", "Wrox Press");
}

Actually Image is IDisposable and it is important to call
Dispose, because otherwise the file is locked.

But why not pick the best and do a using on both !?!?

Arne
 
K

Konrad Neitzel

Hi Tony!

Tony Johansson said:
ResXResourceWriter rw = new ResXResourceWriter("Demo.resx");
using(Image image = Image.FromFile("bell.gif"))
{
rw.AddResource("Mylogo",image);
rw.AddResource("Title","Professional C#");
rw.AddResource("Chapter", "Localization");
rw.AddResource("Author", "Chistian Nagel");
rw.AddResource("Publisher", "Wrox Press");
rw.Close();
}

using(ResXResourceWriter rw = new ResXResourceWriter("Demo.resx"))
{
Image image = Image.FromFile("bell.gif");
rw.AddResource("Mylogo",image);
rw.AddResource("Title","Professional C#");
rw.AddResource("Chapter", "Localization");
rw.AddResource("Author", "Chistian Nagel");
rw.AddResource("Publisher", "Wrox Press");
}

ResXResourceWriter.Close is releasing all resources, so the first block of
code is not that bad. But then there should be some try finally used, so
that the Close() is called even if you get an exception. So you are right, a
using statement would be nice on the ResXResourceWriter (In which case I
would no longer use the Close() call as you did, too!).

But System.Drawing.Image is also implementing IDisposeable! So your code is
missing that.

So my code would be:
using (ResXResourceWriter rw = new ResXResourceWriter("Demo.resx"))
using (Image image = Image.FromFile("bell.gif"))
{
rw.AddResource("Mylogo",image);
rw.AddResource("Title","Professional C#");
rw.AddResource("Chapter", "Localization");
rw.AddResource("Author", "Chistian Nagel");
rw.AddResource("Publisher", "Wrox Press");
}

With kind regards,

Konrad
 
T

Tony Johansson

Konrad Neitzel said:
Hi Tony!



ResXResourceWriter.Close is releasing all resources, so the first block of
code is not that bad. But then there should be some try finally used, so
that the Close() is called even if you get an exception. So you are right,
a using statement would be nice on the ResXResourceWriter (In which case I
would no longer use the Close() call as you did, too!).

But System.Drawing.Image is also implementing IDisposeable! So your code
is missing that.

So my code would be:
using (ResXResourceWriter rw = new ResXResourceWriter("Demo.resx"))
using (Image image = Image.FromFile("bell.gif"))
{
rw.AddResource("Mylogo",image);
rw.AddResource("Title","Professional C#");
rw.AddResource("Chapter", "Localization");
rw.AddResource("Author", "Chistian Nagel");
rw.AddResource("Publisher", "Wrox Press");
}

With kind regards,

Konrad

I have seen that the Image class has no Close method despite the class
implements the IDisposable interface. Why ?
So far I have seen that all classes that implements the IDisposable has a
Close method.
For example the TextReader implements the IDisposable interface and the
class has a Close method


//Tony
 
A

Arne Vajhøj

I have seen that the Image class has no Close method despite the class
implements the IDisposable interface. Why ?
So far I have seen that all classes that implements the IDisposable has a
Close method.
For example the TextReader implements the IDisposable interface and the
class has a Close method

Someone at Microsoft had to make a decision and did.

And while "closing a file" and "closing a database connection"
sounds natural then "closing an image" sounds weird. On the
other hand "disposing of an image" sounds OK.

Just guessing.

Arne
 
R

Random

I have seen that the Image class has no Close method despite the class
implements the IDisposable interface. Why ?

The IDisposable interface only requires you implement a Dispose()
method
So far I have seen that all classes that implements the IDisposable has a
Close method.

You haven't looked that hard then. Nearly all of the GDI+ classes
that implement IDisposable don't have a Close method, since it doesn't
make sense.
 
P

Patrice

So far I have seen that all classes that implements the IDisposable has a
Close method.

Actually this is the other way round.

Classes implementing IDisposable have a Dispose method. In some cases it's a
bit weird (you "close" a file you don't "dispose" a file) so some classes
have also a Close method that most often just calls Dispose...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top