Any Visual Basic 'With' analog in C #?

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

Vb.net

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

WITH#?

 a = Alpha.Beta.Gama.Eta.Zeta.ZetaPropertyA b = Alpha.Beta.Gama.Eta.Zeta.ZetaPropertyB c = Alpha.Beta.Gama.Eta.Zeta.ZetaPropertyC 
+4
source share
5 answers

No, it doesn’t exist.

Although you can 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; 

It should have been obvious though;)

+12
source

For new instances, you can use the object initializer:

 Alpa.Beta.Gama.Eta = new Zeta { ZetaPropertyA = a, ZetaPropertyB = b, ZetaPropertyC = c } 
+3
source

No, nothing like the with construct in C #.

+2
source

No. The workaround is the (short) name of the local variable instead of with . Adds a few characters per line, but you still get fully qualified links.

+1
source

Sorry, C # doesn't have this. The @Jakub object initializer may offer an alternative or:

If you create Zeta yourself, you can use the smooth interface . This will allow you to:

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

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

+1
source

All Articles