C# Syntax

Hi,

Does anyone know of the C# alternative to VB.NET 'with' syntax

ie VB.NET...

with obj_Widget
   .name = "widget1"
   .height = 10
   .width = 20
end with

instead of C#...

obj_Widget.name = "widget1";
obj_Widget.height = 10;
obj_Widget.width = 20;

'With' appears more compact and concise considering long
object names (with full namespace declaration).

Thanks

Richard White


Answer this question

C# Syntax

  • ITJ

    Widget o = obj_Widget;
    o.name = "widget1";
    o.height = 10;
    o.width = 20;

    I don't personally code like this, but this has the same advantages and disadvantages as VB's "With".
    Advantage: less typing
    Disadvantage: 'o.' is obscure (vs VB "With" where "." is obscure)

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB.NET to C# Converter
    Instant VB: C# to VB.NET Converter
    Instant C++: C# to C++ Converter
    Instant J#: VB.NET to J# Converter
    Clear VB: Cleans up VB.NET code

  • Samuel Cheah

    Thank's Konstantin, that clears that up!
  • WKleinschmit

    Hi,

    no, C# hasn't such a construct, you have to manually write out full names/props...

    Greetings,
       Konstantin

  • Travis McPhail

    Forgot to mention, but be aware that this simulation of the VB "With" will only work for reference types.  (With value types, it will compile, but you will not be accessing the original object).



  • C# Syntax