These don't convert correctly for...

vb to C# 'with' clause not converting correctly

The VB 'with' keyword is currently converted by taking the 'with' param and placing it infront of any dots, eg:

With myTextBox
.Text = "Text"
.ForeColor = Colors.Blue
End With

becomes:
myTextBox.Text = "Text";
myTextBox.ForeColor = Colors.Blue;

Which is fine for trivial cases, however if the 'With' parameter does something more interesting, this conversion is not valid, eg:

With myListView.Items.Add(itemText)
.SubItems.Add(subItem1Text)
.SubItems.Add(subItem2Text)
End With

becomes:

myListView.Items.Add(itemText).SubItems.Add(subItem1Text);
myListView.Items.Add(itemText).SubItems.Add(subItem2Text);

which is clearly going to add two items with one subitem instead of one item with two subitems.

The VB 'with' clause is short-hand for creating a temporary variable and then using that, so the conversion should be:

var temp1 = myListView.Items.Add(itemText);
temp1.SubItems.Add(subItem1Text);
temp1.SubItems.Add(subItem2Text);

(which with c# 3.0 is now possible)

3 votes
Vote 0 votes Vote Vote
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service

    You'll receive a confirmation email with a link to create a password (optional).

    Signed in as (Sign out)
    You have left! (?) (thinking…)
    anonymousanonymous shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    1 comment

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service

      You'll receive a confirmation email with a link to create a password (optional).

      Signed in as (Sign out)
      Submitting...
      • UFOUFO commented  ·   ·  Flag as inappropriate

        At least a warning should be generated when a function call is detected in the With parameter.

        I'd also rather replace the 'var' statement with a correct type identifier (for C# <3.0) than rewriting the entire with clause.

      Knowledge Base and Helpdesk