Tech Off Thread

8 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Equivalent of With Statment in C#?

Back to Forum: Tech Off
  • User profile image
    Shark_M

    Hi guys, i was just woundering if there was an equivalent to the With statement in C#?

    like
    With Something
    .control1, etc...
    End With

  • User profile image
    Yggdrasil

    Shark_M wrote:
    Hi guys, i was just woundering if there was an equivalent to the With statement in C#?


    Nope. Sorry.
    I think this is because one of C#'s design goals was to be explicit and not have possibly-ambiguous references floating about.

  • User profile image
    andokai

    The same thing can be achieved more or less in C# using a 'using' block.

  • User profile image
    Yggdrasil

    andokai wrote:
    The same thing can be achieved more or less in C# using a 'using' block.


    Not really. A C# using block is about scope and automatic disposal, not about shortening syntax.
    A VB With block saves you the trouble of writing the object's name again and again, but doesn't automatically dispose and doesn't scope any names.

  • User profile image
    W3bbo

    Yggdrasil wrote:
    Not really. A C# using block is about scope and automatic disposal, not about shortening syntax.
    A VB With block saves you the trouble of writing the object's name again and again, but doesn't automatically dispose and doesn't scope any names.


    There were also optimisation issues with "With" in VB6 and VBS, the compiler doesn't automatically insert the identifier into the places where it's needed, an ASP VBS page that uses "With Response" to call ".Write(Foo)" about 200 times will be tremendously slow compared to an explicit "response.write()" call. I never really understood why though.

  • User profile image
    ZippyV

    I thought it was the other way around: using a 'With" block would accellerate your code (if there are enough statements within the block of course).
    I still use it in vb.net but does it still cause a performance increase or not?

  • User profile image
    Maurits

    You could use a variable...

    With foo
        .bar(.baz)
    End With

    becomes

    var f = foo;
    f.bar(f.baz);

  • User profile image
    Yggdrasil

    Maurits wrote:
    You could use a variable...

    With foo
        .bar(.baz)
    End With

    becomes

    var f = foo;
    f.bar(f.baz);



    One of the refactorings that Resharper adds to VS that VS2005 doesn't have, last I heard, was the Introduce Variable refactoring, precisely for this purpose - if you have a long and convulted value - System.Reflection.Missing.Value, for instance, or ConfigurationSettings.AppSettings["MyAppKey"], you can replace all occurences of it with a variable.

    Saves a minute or two of fiddling about, copy-pasting, noticing you missed something and then getting it right.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.