Converting from VB to C#

瘦欲@ 提交于 2019-12-10 14:48:48

问题


I was tasked with converting a solution from VB to C#. There were 22 projects and hundreds of classes, so I decided to research converters. I finally settled on SharpDevelop, which is an IDE with an included converter. I ran it on each of my projects, and have plenty of errors to fix, but I should be able to go through them and hopefully figure them out. The main issue I am having is with the summary log. I have hundreds of lines for various classes reading:

-- line 0 col 0: Case labels with binary operators are unsupported : Equality
-- line 0 col 0: Case labels with binary operators are unsupported : Equality
-- line 0 col 0: Case labels with binary operators are unsupported : Equality
-- line 0 col 0: Case labels with binary operators are unsupported : Equality
-- line 0 col 0: Case labels with binary operators are unsupported : Equality

I've looked this up, but am not finding a good explanation on what it really means or how to correct it. most of what I find are lines of commented code that say something like:

// ERROR: Case labels with binary operators are unsupported : LessThan

40:

Could someone please provide a bit more information on what causes this error means and how to correct it. Thank you.


回答1:


It means that in C# there is no equivalent for Case Is = (part of a Select Case in VB)... Except of course that there really is.

You can rewrite:

Case Is = 999

as

case 999:

in C#.

There is really no equivalent for Case Is < though, you'll have to rewrite that with if.




回答2:


Select in VB.NET has pretty more complex syntax than its C# counterpart, there is nothing you can do so you have to rewrite your Select statements into if/else:

Select myVariable
    Case 1
        ' Do #1 
    Case 2, 3
        ' Do #1
    Case Is < anotherValue
        ' Do #3
End Select

You have to rewrite to:

if (myVariable == 1)
    ; // #1
else if (myVariable == 2 || myVariable == 3)
    ; // #2
else if (myVariable < anotherValue)
    ; // #3

In general with C# switch you can only test for equality (that's the warning you get) so for anything else you have to go back to a plain if.



来源:https://stackoverflow.com/questions/20744845/converting-from-vb-to-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!