Multiple objects in a using statement?

T

tshad

I tried to put multiple objects in one using statements like so:

using (fs = new FileStream(fromImagePath + "\\" +
(string)dr["originalFileName"],
FileMode.Open, System.IO.FileAccess.Read) , BinaryReader br = new
BinaryReader(fs))

But I get an error:

") expected"

Where the "," before the BinaryReader is.

Plus some "; missing" messages.

But this is how MSDN says to do it.

What am I missing here?

Thanks,

Tom
 
J

Jon Skeet [C# MVP]

tshad said:
I tried to put multiple objects in one using statements like so:

using (fs = new FileStream(fromImagePath + "\\" +
(string)dr["originalFileName"],
FileMode.Open, System.IO.FileAccess.Read) , BinaryReader br = new
BinaryReader(fs))

But I get an error:

") expected"

Where the "," before the BinaryReader is.

Plus some "; missing" messages.

But this is how MSDN says to do it.

What am I missing here?

You can only declare multiple variables in one using statement if
they're of the same type. However, you can nest using statements
without using extra braces:

using (FileStream fs = ...)
using (BinaryReader br = ...)
{
// Use br here
}
 
T

tshad

Jon Skeet said:
tshad said:
I tried to put multiple objects in one using statements like so:

using (fs = new FileStream(fromImagePath + "\\" +
(string)dr["originalFileName"],
FileMode.Open, System.IO.FileAccess.Read) , BinaryReader br = new
BinaryReader(fs))

But I get an error:

") expected"

Where the "," before the BinaryReader is.

Plus some "; missing" messages.

But this is how MSDN says to do it.

What am I missing here?

You can only declare multiple variables in one using statement if
they're of the same type. However, you can nest using statements
without using extra braces:

using (FileStream fs = ...)
using (BinaryReader br = ...)
{
// Use br here
}
Thanks,

Tom
--
Jon Skeet - <[email protected]>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
 

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