Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting from VB to C#

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.

like image 609
Tim Avatar asked Dec 23 '13 13:12

Tim


2 Answers

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.

like image 102
Roy Dictus Avatar answered Oct 17 '22 00:10

Roy Dictus


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.

like image 45
Adriano Repetti Avatar answered Oct 17 '22 00:10

Adriano Repetti