convert vb file to c# with reflector

  • Thread starter Thread starter mp
  • Start date Start date
M

mp

Trying reflector for first time to convert a buildable vbnet class to c#
this started as a vb6.bas file (utility functions used in most of my
projects)
i converted it to vb.net by addressing all the errors and warnings when
first converting vb6 project containing this file to dotnet project

now i used reflector to open the .vb file and got the message it's not an
..exe
so i opened the whole vb.net project and
select the .vb file
converted to c# with the language dropdown
copied text in disassembler pane
went to c# project
menu Project|add class| code file
pasted the copied c# code
near the top is
{

// Fields

private static int $STATIC$GetNextClassDebugID$008$lClassDebugID;

this creates error "Unexpected character '$'

maybe code file isn't the right type of file to choose for a former .bas
module?

i tried also using a class file instead of code file and get the same
errors

how does one convert a vb.net file to a c# file with reflector?

ps i also exported the whole project to c# and still have the same errors

some of these seem occur where in vb6 i had a static long variable in a
function

i'll have to find how to declare a static var in c#...probably in the help
files

thanks

mark
 
Trying reflector for first time to convert a buildable vbnet class to c#
this started as a vb6.bas file (utility functions used in most of my
projects)
i converted it to vb.net by addressing all the errors and warnings when
first converting vb6 project containing this file to dotnet project

Reflector will not necessarily give you working code - in either vb or c#.
All reflector does is look at the il and try to reverse that into C# or VB
language constructs. But, the il generated is not necissarily a 1-to-1 match
with the original code....

What you really need is a utility that can convert vb source to c# source.
That's more of 1-to-1 match... Well, mostly :) Since there are features in
one langauge that don't necissarily map to the other.
now i used reflector to open the .vb file and got the message it's not an
.exe
so i opened the whole vb.net project and
select the .vb file
converted to c# with the language dropdown
copied text in disassembler pane
went to c# project
menu Project|add class| code file
pasted the copied c# code
near the top is
{

// Fields

private static int $STATIC$GetNextClassDebugID$008$lClassDebugID;

this creates error "Unexpected character '$'

maybe code file isn't the right type of file to choose for a former .bas
module?

i tried also using a class file instead of code file and get the same
errors

how does one convert a vb.net file to a c# file with reflector?

ps i also exported the whole project to c# and still have the same errors

some of these seem occur where in vb6 i had a static long variable in a
function

i'll have to find how to declare a static var in c#...probably in the help
files

Now... When you say a static var... I'm assuming you mean the VB meaning
which is static local values - and this is one of those places where there is
no direct C# to VB match. In C# there are no static local values - they need
to exist at the class level (basically, static in C# = shared in VB). In
fact, I haven't looked - but, my guess is that even in VB the actual static
variable is being promoted to the class level anyway by the compiler... In
other words the compiler takes the VB code:

Class SomeClass

Public Sub SomeSub()
Static i As Integer = 10
...
End Sub
End Class

And turns into the IL equivalent of:
Class SomeClass
Private Shared _i As Integer = 10

Public Sub SomeSub()
....
End Sub
End Class

So... Now for the direct answer. In C#, it would look like the second
version of the class - but, with the following syntax:

class SomeClass
{
private static int _i = 10;

public void SomeSub()
{
...
}
}

HTH
 
Thanks Tom
I'm finding this harder than i thought it would be to find the simplest
constructs in going from vb6 to net
mark
Tom Shelton said:
Now... When you say a static var... I'm assuming you mean the VB meaning
which is static local values - and this is one of those places where there
is
no direct C# to VB match. In C# there are no static local values - they
need
to exist at the class level (basically, static in C# = shared in VB).

makes sense thanks
 
If you have access to the original VB6 source, try opening it with VS.NET.

The VS IDE's built in upgrade wizard should try to convert the VB6 code to
VB.NET code, and hopefully after some editing you will have a working VB.NET
source.

Then it's easier to convert it to C# (or you can build a .NET assembly from
the VB.NET code and use in your C# program - depends on what code it is
you're trying to upgrade)

Good luck,
Johnny J.
 
Back
Top