4

Possible Duplicate:
C# equivalent for Visual Basic keyword: 'With' ... 'End With'?

VB.NET

With Alpha.Beta.Gama.Eta.Zeta
    a = .ZetaPropertyA
    b = .ZetaPropertyB
    c = .ZetaPropertyC
End With

C#?

a = Alpha.Beta.Gama.Eta.Zeta.ZetaPropertyA
b = Alpha.Beta.Gama.Eta.Zeta.ZetaPropertyB  
c = Alpha.Beta.Gama.Eta.Zeta.ZetaPropertyC
Community
  • 1
  • 1
serhio
  • 28,010
  • 62
  • 221
  • 374
  • 1
    no, using is for classes implementing `IDisposeable`. – Femaref Nov 18 '10 at 20:37
  • I can smell `Alpa.Beta.Gama.Eta.Zeta.ZetaPropertyA` a mile off. There's probably something wrong with your architecture if you're having to reach across this many relationships to set a property. – spender Nov 18 '10 at 20:37
  • spender: OR Mapper with a tree like structure of objects. – Femaref Nov 18 '10 at 20:44
  • @spender - This is called Law of Demeter: http://en.wikipedia.org/wiki/Law_of_Demeter – Jakub Konecki Nov 18 '10 at 20:45
  • @spender: `GlobalProperties.Application.User.CurrentUser.TextOptions.WelcomeFontColor` – serhio Nov 18 '10 at 20:45
  • Just a comment on style, I used to really like the `With` construct in VB but a few years ago I decided that I didn’t like it any more. So now I use a local variable as a shorthand, even in VB in preference of a `With` block. – Konrad Rudolph Nov 18 '10 at 20:52
  • Just because VB jumped off a bridge, doesn't mean C# has to too ;) – Jon Hanna Nov 19 '10 at 01:11

5 Answers5

12

Nope, doesn't exist.

Though you could shorten it a bit:

var z = Alpha.Beta.Gama.Eta.Zeta;

z.ZetaPropertyA = a;
z.ZetaPropertyB = b; 
z.ZetaPropertyC = c;

for your other case:

var z = Alpha.Beta.Gama.Eta.Zeta;

a = z.ZetaPropertyA;
b = z.ZetaPropertyB;
c = z.ZetaPropertyC;

That should've been obvious though ;)

Femaref
  • 60,705
  • 7
  • 138
  • 176
  • 2
    I thought this was a pretty good workaround... Not perfect, but only one extra character per line. And I always thought the WITH statement was less readable for a maintenance programmer anyway. – David Nov 18 '10 at 20:38
  • 1
    +1 Nice workaround. You beat me to the punch. And i agree with David: "with" is ugly and seems to obfuscate rather than help! – Paul Sasik Nov 18 '10 at 20:39
  • nothing obfuscates like `$$var` in php. – Femaref Nov 18 '10 at 20:40
  • It may be 1 extra character per line (or 2 if you count the semi-colon), as David pointed out, but then you also don't have to have the "End With". Just thought I'd point that out, in case anyone is counting characters. – Dr. Wily's Apprentice Nov 18 '10 at 21:17
3

For new instances you can use object initializer:

Alpa.Beta.Gama.Eta = new Zeta
{
    ZetaPropertyA = a, 
    ZetaPropertyB = b,
    ZetaPropertyC = c
}
Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
2

No, there's nothing like the with construct in C#.

Jonas Elfström
  • 30,834
  • 6
  • 70
  • 106
1

Nope. The workaround is a (short) local variable name instead of with. Adds a few characters per line, but you still end up with fully qualified references.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Donnie
  • 45,732
  • 10
  • 64
  • 86
  • The code update doesn't change anything. You can still declare a `var` to everything except `ZetaXXX` and do as said. – Donnie Nov 18 '10 at 20:43
  • If a "With" expression is of a class type, the effect is analogous to using a local variable with a short name. If the expression is a mutable structure, however, there is no direct equivalent. Matching the semantics of "With MyPoints(I,J) : MyPoints = SomeOtherArray : I=someOtherValue : J=someOtherValue : .X=5 : .Y=9 : End With" would require making local copies of MyPoints, I, and J, or using some other trick to get the reference. – supercat Aug 26 '11 at 16:21
1

Sorry, C# doesn't have that. The object initializer @Jakub suggests might provide an alternative, or:

If you design Zeta yourself, you could use the fluent interface design pattern. That would allow you to do:

Alpha.Beta.Gama.Eta.Zeta
    .SetPropertyA(A)
    .SetPropertyB(B)
    .SetPropertyC(C);

Which comes close to what you want, at the expense of a lot of work elsewhere. And remember that a fluent interface is not always the best design choice.

Joseph Tanenbaum
  • 2,211
  • 15
  • 30