Updating a local copy versus the original is not what I would call a "subtle" distinction. WITH Block = "code broken" in many possible circumstances. It's probably fine if used to abbreviate syntax whilst staying within the structure of the same object... The most likely situation is where you may be traversing a structure such as a network of objects whose interconnections my be implicitly changed by setting properties. End With" blocks referring to different objects within each other.
Every once in awhile, you come across this exception: System.
Null Reference Exception – “Object reference not set to an instance of an object.” You see people asking about this everywhere; about why are they getting this error. Note, regardless of the scenario, the cause is always the same in .
It is certainly a trap for the unwary, but the way WITH block works, as implemented, is the most robust approach imho. It is a pattern in Kent Beck's Smalltalk Best Practice Patterns book.
I don't think it is right to say "code broken". The 'with' is basically the 'cascade' from Smalltalk.
Net: You are trying to use a reference variable who’s value is Nothing/null.
When the value is Nothing/null for the reference variable, that means it is not actually holding a reference to an instance of any object that exists on the heap. Net, you are trying to access a string that has not been initialized. You can’t even compile code like the following in C# Remember, a string is a reference type (a character array) that has to have a value.
Whilst you would be unlikely to code something explicit like the above, in some situations this might occur inadvertently so you should be aware of the issue. It occurred to me after writing my comments that you probably meant avoid nesting "With ...
Consider: So, the WITH statement is not simply syntactical sugar, it is genuinely a different construct. I would just explicitly specify each source and target, as it [email protected] Skeet That was quick!
In my experience, almost every time the "With" statement is used, there's some Law of Demeter violation occurring.
User ID="0" I'd argue that exposing the entire Grid View contained in the Refund Dialog to external code is also a violation of the Law of Demeter.
User ID="0" ...so on In the later example there are even performance benefit over the first example because in the first example Im fetching the user every time I access a user property and in the WITH-case I only fetch the user one time. NET, it compiles to exactly the same code as fully-qualifying the object name, so there is no performance penalty for this sugar. Append("zap") End With Dim sb2 As New String Builder sb2. (By the way, Tom, I'm interested in knowing what happened with the debugger -- I can't recall ever seeing any unusual behavior in the debugger based on a @John Rudy - Find the beginning of a With statement and set a breakpoint. establishes an invariate local pointer to the original object, not a copy of the object. I usually find that when I'm performing many different operations on an object beyond its initial construction, those operations should be encapsulated as a single one on the object itself.