-1

I'm wondering what (and if it's defined) the behavior is if i need to re instanciate a variable within it's own using() tag?

I have an actual use case for this, using SharpDX i have variables that i want declared outside the render loop (used for each frame) that i'd ideally wrap as this using(thevariable){MainLoop{}}; However if something happens (the user resizes) that variable may need to be disposed & reinitialized. I see in the samples it's being initialized to null & then re initialized within the loop if needed, re disposed, and then disposed of again after the loop which looks a lot worse than using well, using statements so i was wondering what would be the behavior of code such as this:

void Main()
{
    using(var MyDisposable = new MyDisposable())
    {
        If(SomeCondition)
        {
             MyDisposable.Dispose() // Do i need to call this? Or is it auto called thanks to using when leaving the block? What will using call dispose on, the object that MyDisposable refers to, or the one it refered to back when i called the using statement?
             MyDisposable = new MyDisposable(); // Is this going to be managed by the using block? Is this even valid?
        }
    }
}

Also I'm not sure if the behavior changed over time, I'm assuming not but if it did I'm interested in .net 4.5 answers

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Ronan Thibaudau
  • 3,413
  • 3
  • 29
  • 78
  • 1
    Why not just not do that, and then don't worry about it? – John Saunders Oct 15 '13 at 04:12
  • Because i don't write code that i don't understand and as i mentioned i don't even know if it has a well defined behavior. It's the first time i stumble on disposable objects i may have a functional need to re instanciate within the scope of their definition. – Ronan Thibaudau Oct 15 '13 at 04:15
  • Sorry, no. I have never found a valid use case for "reinstantiating". Just use a different variable. If it's a different instance, then it's different, even if it's the same class. Different instance = different variable, and life is much simpler. – John Saunders Oct 15 '13 at 04:17
  • But i mentioned a use case, this is actually how it's written in the sharpdx samples (except they don't use the using keyword), there's a variable representing "something" (there are multiple such somethings) and at some point it may need to be re created (if resolution changes for example). That can only be detected within the inner scope (nor where it is instantiated) which leads us to instantiating with = null & then handling it Inside (in case of change) and outside (for the final dispose). I'd like to match that to the using pattern & i don't know how nor know if it's "safe". – Ronan Thibaudau Oct 15 '13 at 04:18
  • Post a URL for one of those examples. Maybe this is the first valid use case I've seen. That would surprise me, but I've been surprised in the past. – John Saunders Oct 15 '13 at 04:20
  • I will let me see if i find a direct link to a source file online without the full download required, however note that it doesn't make the question less interesting even if you find an alternate way, i'd "still" want to know how this behaves and how well defined it is. – Ronan Thibaudau Oct 15 '13 at 04:22
  • Can't seem to find it on their github but if you download this : http://sharpdx.org/upload/SharpDX-SDK-LatestDev.exe in the samples directory you'll find many samples, take the one Under SharpDX-SDK-LatestDev\Samples\Direct3D11\MiniCube , in program.cs have a look at how the variables backBuffer, renderView, depthBuffer & depthView are handled. They are all declared outside of the Run() Method (called once per frame) as they are valid accross frames, however they need re created when the resolution changes (within the loop) and they can be fully disposed (even if never re created) after loop – Ronan Thibaudau Oct 15 '13 at 04:29

1 Answers1

2

I think you'll find that this results in a compiler error.. specifically:

Cannot assign to 'MyDisposable' because it is a 'using variable'

So, the answer to your question is: Nothing happens in this instance. Your code won't compile.

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
  • Well i didn't test it but i guess this cuts the chase short, however i'm still wondering if somehow there's a way to use using to make this cleaner, else i guess i'll write my own mini using. – Ronan Thibaudau Oct 15 '13 at 04:31