73

At work, I'm frequently working on projects where numerous properties of certain objects have to be set during their construction or early during their lifetime. For the sake of convenience and readability, I often use the With statement to set these properties. I find that

With Me.Elements
    .PropertyA = True
    .PropertyB = "Inactive"
    ' And so on for several more lines
End With

Looks much better than

Me.Elements.PropertyA = True
Me.Elements.PropertyB = "Inactive"
' And so on for several more lines

for very long statements that simply set properties.

I've noticed that there are some issues with using With while debugging; however, I was wondering if there were any compelling reasons to avoid using With in practice? I've always assumed the code generated via the compiler for the above two cases is basically the same which is why I've always chosen to write what I feel to be more readable.

1
  • 6
    +1 for question title in question form. Suggestion: in the question body, mention some of the debugging issues you have experienced with With statement if possible.
    – Geoffrey
    Jan 18, 2011 at 14:32

10 Answers 10

70

If you have long variablenames and would end up with:

UserHandler.GetUser.First.User.FirstName="Stefan"
UserHandler.GetUser.First.User.LastName="Karlsson"
UserHandler.GetUser.First.User.Age="39"
UserHandler.GetUser.First.User.Sex="Male"
UserHandler.GetUser.First.User.Occupation="Programmer"
UserHandler.GetUser.First.User.UserID="0"
....and so on

then I would use WITH to make it more readable:

With UserHandler.GetUser.First.User
    .FirstName="Stefan"
    .LastName="Karlsson"
    .Age="39"
    .Sex="Male"
    .Occupation="Programmer"
    .UserID="0"
end with

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.

I can get the performance gain without using with, like this:

dim myuser as user =UserHandler.GetUser.First.User
myuser.FirstName="Stefan"
myuser.LastName="Karlsson"
myuser.Age="39"
myuser.Sex="Male"
myuser.Occupation="Programmer"
myuser.UserID="0"

But I would go for the WITH statement instead, it looks cleaner.

And I just took this as an example so dont complain over a class with many keywords, another example could be like: WITH RefundDialog.RefundDatagridView.SelectedRows(0)

6
  • 7
    +1 for also explaining performance difference. Suggestion: place Embrace in bold at top of answer.
    – Geoffrey
    Jan 18, 2011 at 14:29
  • 2
    Doesn't the compiler optimize away this performance-gain in most cases?
    – Alxandr
    Jul 1, 2013 at 12:54
  • 1
    I'd be very surprised if the VB.NET compiler did not perform this simple optimization. The two code snippets are most probably compiled to the same MSIL.
    – Saeb Amini
    Aug 11, 2014 at 6:08
  • 1
    Your examples do not demonstrate good coding practice. See @ljorquera's answer below.
    – David
    Apr 21, 2015 at 18:27
  • However, even with the extreme example with the very long variable-"chain" you could avoid a With: Dim user = UserHandler.GetUser.First.User. Now continue with the user variable. Nov 15, 2017 at 8:54
24

In practice, there are no really compelling points against it. I'm not a fan, but that's a personal preference, there's no empirical data to suggest that the With construct is bad.

In .NET, it compiles to exactly the same code as fully-qualifying the object name, so there is no performance penalty for this sugar. I ascertained this by compiling, then disassembling, the following VB .NET 2.0 class:

Imports System.Text

Public Class Class1
    Public Sub Foo()
        Dim sb As New StringBuilder
        With sb
            .Append("foo")
            .Append("bar")
            .Append("zap")
        End With

        Dim sb2 As New StringBuilder
        sb2.Append("foo")
        sb2.Append("bar")
        sb2.Append("zap")
    End Sub
End Class

The disassembly is as follows -- note that the calls to sb2's Append method look identical to the With statement calls for sb:

.method public instance void  Foo() cil managed
{
  // Code size       91 (0x5b)
  .maxstack  2
  .locals init ([0] class [mscorlib]System.Text.StringBuilder sb,
           [1] class [mscorlib]System.Text.StringBuilder sb2,
           [2] class [mscorlib]System.Text.StringBuilder VB$t_ref$L0)
  IL_0000:  nop
  IL_0001:  newobj     instance void [mscorlib]System.Text.StringBuilder::.ctor()
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  stloc.2
  IL_0009:  ldloc.2
  IL_000a:  ldstr      "foo"
  IL_000f:  callvirt   instance class [mscorlib]System.Text.StringBuilder [mscorlib]System.Text.StringBuilder::Append(string)
  IL_0014:  pop
  IL_0015:  ldloc.2
  IL_0016:  ldstr      "bar"
  IL_001b:  callvirt   instance class [mscorlib]System.Text.StringBuilder [mscorlib]System.Text.StringBuilder::Append(string)
  IL_0020:  pop
  IL_0021:  ldloc.2
  IL_0022:  ldstr      "zap"
  IL_0027:  callvirt   instance class [mscorlib]System.Text.StringBuilder [mscorlib]System.Text.StringBuilder::Append(string)
  IL_002c:  pop
  IL_002d:  ldnull
  IL_002e:  stloc.2
  IL_002f:  newobj     instance void [mscorlib]System.Text.StringBuilder::.ctor()
  IL_0034:  stloc.1
  IL_0035:  ldloc.1
  IL_0036:  ldstr      "foo"
  IL_003b:  callvirt   instance class [mscorlib]System.Text.StringBuilder [mscorlib]System.Text.StringBuilder::Append(string)
  IL_0040:  pop
  IL_0041:  ldloc.1
  IL_0042:  ldstr      "bar"
  IL_0047:  callvirt   instance class [mscorlib]System.Text.StringBuilder [mscorlib]System.Text.StringBuilder::Append(string)
  IL_004c:  pop
  IL_004d:  ldloc.1
  IL_004e:  ldstr      "zap"
  IL_0053:  callvirt   instance class [mscorlib]System.Text.StringBuilder [mscorlib]System.Text.StringBuilder::Append(string)
  IL_0058:  pop
  IL_0059:  nop
  IL_005a:  ret
} // end of method Class1::Foo

So if you like it, and find it more readable, go for it; there's no compelling reason not to.

(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 With statement, so I'm curious to know what behavior you did see.)

5
  • 7
    @John Rudy - Find the beginning of a With statement and set a breakpoint. Step to the next line (so you're hiding the first line right under the if block). Highlight it, then 'Add Watch'. You should see this: 'With' contexts and statements are not valid in debug windows.
    – Tom
    Nov 13, 2008 at 13:58
  • 1
    Ah, interesting! I don't frequently use Watches in debugging, which is why I never ran across it. I shall have to try that -- and maybe, for grins, try it across 2003, 2005 and 2008 and see if any of them behave differently. Thanks for the tip!
    – John Rudy
    Nov 13, 2008 at 14:43
  • 5
    If With is used with an array of a mutable value type (such as Rectangle), the generated code will different from anything that could be produced without With.
    – supercat
    Oct 24, 2012 at 19:31
  • 1
    You can't "watch" it because it is not really there, it is a copy on the stack. When it goes out of scope, so do your changes to it! I guess it implements the "immutable data" concept nicely.
    – user4624979
    Sep 24, 2015 at 15:59
  • @no comprende - No, that's not right. WITH.. establishes an invariate local pointer to the original object, not a copy of the object. Changes are made to the object as you would expect. When it goes out of scope, it is the local pointer that is destroyed.
    – JohnRC
    May 2, 2016 at 17:29
15

There is a difference between using With and making repeating references to an object, which is subtle but should be borne in mind, I think.

When a WITH statement is used, it creates a new local variable referencing the object. Subsequent references using .xx are references to properties of that local reference. If during the execution of the WITH statement, the original variable reference is changed, the object referenced by the WITH does not change. Consider:

Dim AA As AAClass = GetNextAAObject()
With AA
    AA = GetNextAAObject()

    '// Setting property of original AA instance, not later instance
    .SomeProperty = SomeValue
End With

So, the WITH statement is not simply syntactical sugar, it is genuinely a different construct. 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. 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.

4
  • 1
    Updating a local copy versus the original is not what I would call a "subtle" distinction. More like: "code broken" versus "code works". WITH Block = "code broken" in many possible circumstances. It is like a pointer in reverse, it un-points to the actual data. Your changes are un-changed for you. What is the use case for such a programming construct?
    – user4624979
    Sep 24, 2015 at 15:56
  • 1
    Apologies for long delay in responding to your comment. I don't think it is right to say "code broken". It is certainly a trap for the unwary, but the way WITH block works, as implemented, is the most robust approach imho. The WITH statement establishes a local, temporary, invariant pointer to the object (not a local copy of the object) and within the scope of the WITH statement will reliably refer to the same object all the way through regardless of what side effects the code might have. I think that is quite clear and quite sensible.
    – JohnRC
    May 2, 2016 at 17:23
  • Probably the difference kicks in when dealing with structures (records), not objects and references to them. At least if I'm getting the text above regarding "making a copy" Aug 17, 2017 at 14:31
  • Oops, looked it up a bit and it seems structures are kind-of supported there, but only in a read-only way. See my answer (just adding it) on that Aug 17, 2017 at 14:37
13

It's all about readability. Like all syntactic sugar, it can be overused.

Embrace it IF you're setting several members of an object over a few lines

With myObject
  .Property1 = arg1
  .Property2 = arg2
...

Avoid doing anything else with "With"

If you write a With block that spans 50-100 lines and involves lots of other variables it can make it REALLY difficult to remember what was declared at the top of the block. For obvious reasons, I won't provide an example of such messy code

1
  • 6
    I was looking for an answer like this in here. With blocks that are longer than say, one screen-height, add unnecessary burden on the reader.
    – JKomusin
    Oct 2, 2012 at 18:53
6

Where it makes the code genuinely more readable, go for it. Where it makes it less readable, avoid it - in particular, I suggest you avoid nesting With statements.

C# 3.0 has this feature solely for object initialization:

var x = new Whatever { PropertyA=true, PropertyB="Inactive" };

This is not only pretty much required for LINQ, but it also makes sense in terms of where the syntax doesn't indicate a code smell. 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.

One note about your example - do you really need the "Me" at all? Why not just write:

PropertyA = True
PropertyB = "Inactive"

? Surely "Me" is implied in that case...

5
  • I was just doing that for a concise example, not to really demonstrate how I'm using in real practice.
    – Tom
    Nov 12, 2008 at 12:37
  • 1
    Isn't it just typical how hard it is to pull realistic examples out of thin air... they always seem simpler than a real world case and always somebody somebody comes up with a way to simplify just the example, but not the general case.
    – bart
    Nov 12, 2008 at 13:56
  • I don't really get why you recommend avoiding nesting 'With ... End With' statements? I do that frequently when working with Excel in vba and via COM and it really makes my life easier as I go deeper into the property levels. Is there any concrete reason qhy I should avoid doing this?
    – yu_ominae
    Jul 14, 2014 at 7:10
  • @yu_ominae: It makes it harder if there are names in common - and harder in general to know which name is associated with which object. I would just explicitly specify each source and target, as it were.
    – Jon Skeet
    Jul 14, 2014 at 7:14
  • 1
    @JonSkeet That was quick! It occurred to me after writing my comments that you probably meant avoid nesting "With ... End With" blocks referring to different objects within each other. It's probably fine if used to abbreviate syntax whilst staying within the structure of the same object... depending on the size of the block as well of course. I never really gave it much thought, so thanks for making me think about this issue.
    – yu_ominae
    Jul 14, 2014 at 7:19
5

I would be suspicious of code that uses a lot this keyword: if it is used to make easier to set lots of instance variables or properties I think this may indicate that your classes are too large ( Large Class smell ). If you use it to replace long chains of calls like this:

UserHandler.GetUser.First.User.FirstName="Stefan"
UserHandler.GetUser.First.User.LastName="Karlsson"
UserHandler.GetUser.First.User.Age="39"
UserHandler.GetUser.First.User.Sex="Male"
UserHandler.GetUser.First.User.Occupation="Programmer"
UserHandler.GetUser.First.User.UserID="0"

then you are probably violating Demeter Law

4
  • It was only to show an example. It could be long variablenames in combination with one or two keywords..
    – Stefan
    Nov 12, 2008 at 13:01
  • 1
    Like this for example: WITH RefundDialog.RefundDatagridView.SelectedRows(0)
    – Stefan
    Nov 12, 2008 at 13:06
  • 1
    I'd argue that exposing the entire GridView contained in the RefundDialog to external code is also a violation of the Law of Demeter. In my experience, almost every time the "With" statement is used, there's some Law of Demeter violation occurring. Long variable names might be a case for the With statement, but I find that the developer has to be careful not to let the With block get to big or it becomes hard to track which object makes up the "With context". May 5, 2009 at 14:08
  • Agreed. As long as you are following good object-oriented principles, you shouldn't need multiple "hops" into objects like this. And if you're just dealing with properties or methods on an immediate object reference, the With statement becomes much less helpful.
    – David
    Apr 21, 2015 at 18:26
3

I don't use VB.NET (I used to use plain VB) but...

Is the leading dot mandatory? If so, then I don't see a problem. In Javascript, the result of using with is that a property of an object looks just the same as a plain variable, and that is very dangerous, as you don't see if you're accessing a property or a variable, and thus, with is something to avoid.

Not only is its use easier on the eyes, but for repeated access to properties of an object, it's likely to be faster, as the object is fetched through the method chain only once, and not once for every property.

I do agree with other replies that you ought to avoid nested use of with, for the same reason as why to avoid with altogether in Javascript: because you no longer see what object your property belongs to.

0
3

The 'with' is basically the 'cascade' from Smalltalk. It is a pattern in Kent Beck's Smalltalk Best Practice Patterns book.

A summary of the pattern: use it when it makes sense to group the messages sent to the object. Don't use it if it just happens to be some messages sent to the same object.

1
  • When will it make sense and when not?
    – Geoffrey
    Jan 18, 2011 at 14:33
2

AVOID the WITH Block at all costs (even readability). Two reasons:

  1. the Microsoft Documentation about With...End With says that in some circumstances, it creates a copy of the data on the stack, so any changes that you make will be thrown away.
  2. If you use it for LINQ Queries, the lambda results DO NOT Chain and so each intermediate clause's result is thrown away.

To describe this, we have a (broken) example from a Textbook that my co-worker had to ask the author about (it is indeed incorrect, the Names have been changed to protect... whatever):

With dbcontext.Blahs
.OrderBy(Function(currentBlah) currentBlah.LastName)
.ThenBy(Function(currentBlah) currentBlah.FirstName)
.Load()
End With

The OrderBy and ThenBy have No Effect at all. IF you reformat the code by ONLY dropping the With and End With, and adding line continuation characters at the end of the first three lines... it works (as shown 15 pages later in the same textbook).

We don't need any more reason to search and destroy WITH Blocks. They only had meaning in an Interpreted framework.

1

There's a gotcha when using it with structures, aka you can't set their fields, since you're working on a local copy (made at time of entry in with block) of the "with" expression and not working with a (copy of an) object reference in that case:

The data type of objectExpression can be any class or structure type or even a Visual Basic elementary type such as Integer. If objectExpression results in anything other than an object, you can only read the values of its members or invoke methods, and you get an error if you try to assign values to members of a structure used in a With...End With statement. This is the same error you would get if you invoked a method that returned a structure and immediately accessed and assigned a value to a member of the function’s result, such as GetAPoint().x = 1. The problem in both cases is that the structure exists only on the call stack, and there is no way a modified structure member in these situations can write to a location such that any other code in the program can observe the change.

The objectExpression is evaluated once, upon entry into the block. You can't reassign the objectExpression from within the With block.

https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/with-end-with-statement

guess the compiler could have been a bit more clever if you pass to with statement a structure name instead of an expression that returns a structure, but seems it's not

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.