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)
1 comment
-
UFO
commented
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.