views:

71

answers:

3

Some code to illustrate my question:

With Test.AnObject

    .Something = 1337
    .AnotherThing = "Hello"

    ''// why can't I do this to pass the object itself:
    Test2.Subroutine(.)
    ''// ... and is there an equivalent, other than repeating the object in With?

End With
+3  A: 

There is no way to refer to the object referenced in the With statement, other than repeating the name of the object itself.

Patrick McDonald
+3  A: 

I suspect you'll have to repeat yourself. If the expression (to get the object) is expensive, then perhaps drop it into a variable first, and either use that variable in the With, or drop the With completely:

tmp = Test.AnObject;
tmp.Something = 1337;
...
Test2.Subroutine(tmp);
Marc Gravell
+1  A: 

As others have said, you're going to have to write

Test2.Subroutine(Test.AnObject)

This is a good example of why it's worth being a little careful with the With construct in VB.Net. My view is that to make it worth using at all, you really need to be setting more than one or two properties, and/or calling more than one or two methods on the object in the With statement.

When there are lots, and you're not interspersing the .SomeProperty = , or .DoSomething, with other things, it's a terrific aid to readability.

Conversely, a few dots sprinkled amongst a load of other stuff is actually a lot harder to read than not using With at all.

In this case, . characters on their own could easily get lost visually, although of course, it would be syntactically consistent.

I guess they just chose not to implement it. VB isn't really the sort of language where they want to encourage single character language elements, and as a heavy user of VB.Net, I broadly agree with that.

Bottom line: if you're using a With clause with many contained elements, having to refer to the object itself isn't that big a deal. If you're using it with just one or two, maybe better not to use a With clause in the first place.

ChrisA