none
Using block RRS feed

  • General discussion


  • DbCommand dbCommand = null;
     using (dbCommand = GetDbCommand(db, sql))
     {
         FillUpdateCommandParams(ref db, ref dbCommand, entity);

    }

    Will dbcommand be disposed at the end even though i am passing it by ref?


    Anonymous

    Thursday, September 12, 2013 6:44 PM

All replies

  • The fact that you pass the variable by ref doesn't affect using in any way. Using will dispose whatever command object GetDbCommand returned. But ref normally means that you want to change the variable. If FillUpdateCommandParams replaces the command with a new one then this new one won't be disposed.

    I must say that such code is probably better avoided. FillUpdateCommandParams does more than the name implies if it really changes the command and it's not obvious to everyone which command will get disposed and which not. Also, declaring the "using" variable outside the using is best avoided, it can lead to the accidental use of a disposed object.

    Thursday, September 12, 2013 7:00 PM
  • Yes. The compiler generates code to dispose the original value of the using expression.



    Visual C++ MVP


    Thursday, September 12, 2013 7:06 PM
  • Also, declaring the "using" variable outside the using is best avoided, it can lead to the accidental use of a disposed object.

    I couldn't avoid this because declaring the DBCommand within the using block when it is passed by ref results in a compile time error. Variables passed by ref cannot be declared in a using block. How do i avoid this?


    Anonymous

    Thursday, September 12, 2013 7:21 PM
  • Well, if you use ref you can't really avoid that. Ideally you wouldn't combine using and ref this way to begin with.

    Note that FillUpdateCommandParams replaced the command then you probably need an additional Dispose to dispose that command. That will make this code look even more unusual. Ultimately it may be better to just use try/finally/Dispose instead of using.

    Thursday, September 12, 2013 7:42 PM
  • Hi jarrodk,<o:p></o:p>

    Thanks for your posting.<o:p></o:p>

    From your description, I think this issue is related to ADO.NET.So I think that Data Platform Development forums is more suitable for it. I move it to Data Platform Development forums .

    Data Platform Development forums : http://social.msdn.microsoft.com/Forums/en-US/home?category=dataplatformdev<o:p></o:p>

    Hope these help.<o:p></o:p>


    Lilia Gong <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, September 13, 2013 6:02 AM
  • The question is about using & ref. That's C# related and the fact that DbCommand is involve is irrelevant. The question equally applies to any disposable class.
    Friday, September 13, 2013 6:46 AM
  • It might be that you don't even have the correct use of ref since you need to break the using / ref rule. That is really confusing not only what is about to be disposed, but also what is dbCommand referencing after FillUpdateCommandParams returns. It might also be instance with completely different SQL that is used in GetDbCommand method.

    If you just load parameters to dbCommand object, you might be using ref in a way it is not meant to be used. So I would reviews what is done in FillUpdateCommandParams and change that if ref is not really needed.

    Tuesday, September 17, 2013 1:20 PM