Converting from VB to C#
Asked Answered
G

2

5

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.

Grant answered 23/12, 2013 at 13:45 Comment(3)
You just used a crappy converter. It is a common problem, lots of them do a so-so job on converting vb.net to c#. And you got what you paid for. The vb.net Select Case statement is just not that easy to convert to the c# switch statement, it is a lot less capable. Start at the top of the error list and work your way down, fixing the converter mistakes.Dugaid
I know you have done your conversion but for others considering such a task I can recommend the Telerik converter which is online, free and can do bulk file conversions. I have used this for .net web projects and it has worked very well. I'm not sure how it copes with these types of Case statements though as I've only ever used it to convert from C# to VB rather than VB to C#Wastebasket
Telerik uses the same conversion library (NRefactory) as SharpDevelop so it will have the same problem.Fastness
S
6

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.

Sweatband answered 23/12, 2013 at 13:50 Comment(0)
K
6

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.

Kneeland answered 23/12, 2013 at 13:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.