473,805 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple objects in a using statement?

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

using (fs = new FileStream(from ImagePath + "\\" +
(string)dr["originalFileNa me"],
FileMode.Open, System.IO.FileA ccess.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
Jun 27 '08 #1
2 7795
tshad <ts***@dslextre me.comwrote:
I tried to put multiple objects in one using statements like so:

using (fs = new FileStream(from ImagePath + "\\" +
(string)dr["originalFileNa me"],
FileMode.Open, System.IO.FileA ccess.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
}

--
Jon Skeet - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #2

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************@m snews.microsoft .com...
tshad <ts***@dslextre me.comwrote:
>I tried to put multiple objects in one using statements like so:

using (fs = new FileStream(from ImagePath + "\\" +
(string)dr["originalFileNa me"],
FileMode.Open, System.IO.FileA ccess.Read) , BinaryReader br = new
BinaryReader(f s))

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 - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com

Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
11708
by: Hollywood | last post by:
After doing a search through google's archives of this list, I didn't see what I was looking for so here goes... Is it possible to serialize/deserialize multiple objects from a single XML file? I was looking to use the XML serialization to be able to translate complex XML elements in a single XML file through XML serialization as opposed to using SAX and coding my own serializer. However, the XML Serialization API appears to only...
5
2715
by: Bill Priess | last post by:
Hey gang, Ok, I'm stumped on this one... I am using the using statement to wrap a SqlDataAdapter that I am using to fill a DataTable. Now, what I need to know is, just how much block-scope applies to objects created in the using scope. For example: <code> static DataTable getTable()
8
3709
by: J-T | last post by:
I have a class like below I have a couple of questions about that: 1) I like to use "Using statement" when creating an object of this class,so I had to implement IDisposable.Am I doing this right here? 2) Do I have to be worried about those objects I have created in GetPackageNames() ?? for instance making them null afterward or something? 3) This class is in my business layer As you can see I'm using Microsoft.SQLServer.DTSPkg80...
0
1593
by: tayssir.hawary | last post by:
Dear all, I need to get the old value when multiple objects are selected. It works fine when a single object is selected, but e.OldValue contains null when multiple objects are selected. I appreciate a reply:) Tayssir Hawary
2
5020
by: Robert Bravery | last post by:
Hi all, Being new to C# and .net I often don't know how to use things. I have created an app that imports excel data, it works well, with methods to open excel, extract the data and close excel. Now a colege of mine suggested useinf the usingstatement, so that the office object is dispossed/destroyued correctly, saying that it prevents memoryleaks. Problem is I am having problems trying to achieve this, I keep getting the error: type...
6
1960
by: Juan Zamudio | last post by:
Hi all, I'm using vs 2003, i have a question about using and as, i was reading in programing .net components that one way to avoid null references inside using was using as, something like this: using(var as IDisposable){ ..... } the book saids that if the conversion is null the code inside using will not run, but i cant make it work, I have a FileStream that opens a file, the path is a parameter, something like this:
1
6186
by: Andrew Poulos | last post by:
I'm building some e-learning that's managed by an learning management system (LMS). The LMS allows only one "field" for me to store custom lesson data. The e-learning currently uses a number of separate javascript objects. How can I concatenate them into one object (to JSON string them to then feed them to the LMS) and then to later parse the JSON string back into the original multiple objects? Andrew Poulos
2
294
by: tshad | last post by:
I tried to put multiple objects in one using statements like so: using (fs = new FileStream(fromImagePath + "\\" + (string)dr, FileMode.Open, System.IO.FileAccess.Read) , BinaryReader br = new BinaryReader(fs)) But I get an error: ") expected"
3
1929
by: Israel | last post by:
I always wondered why the using command can only take one object. I always find that this isn't sufficient for drawing so I end up always disposing in my finally block but then I have to remember to put everything in there. Why can't the using command just allow something like this? using (Pen mypen = new Pen(Color.Black), Brush mybrush = new SolidBrush(Color.Blue)) { }
5
7148
by: Hillbilly | last post by:
MSDN Remarks "as a rule" the using statement should be used when instantiating objects which inherit IDisposable. Other than the obvious unmanaged objects like the file system example, fonts and the database how else may it be determined which classes and their objects inherit IDisposable? And the remarks imply the using statement is a different and perhaps more efficient way to use objects than try-catch-finally? ...
0
9718
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10613
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10107
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3846
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.