Multiple objects in a using statement?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tshad

    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


  • Jon Skeet [C# MVP]

    #2
    Re: Multiple objects in a using statement?

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

    Comment

    • tshad

      #3
      Re: Multiple objects in a using statement?


      "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
      news:MPG.22ac5b 588c78a489d01@m snews.microsoft .com...
      tshad <tshad@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 - <skeet@pobox.co m>
      Web site: http://www.pobox.com/~skeet
      Blog: http://www.msmvps.com/jon.skeet
      C# in Depth: http://csharpindepth.com

      Comment

      Working...