icsharpcode / CodeConverter

Convert code from C# to VB.NET and vice versa using Roslyn
https://icsharpcode.github.io/CodeConverter/
MIT License
818 stars 214 forks source link

C# -> VB: Better winforms conversion #170

Open GrahamTheCoder opened 6 years ago

GrahamTheCoder commented 6 years ago

I tackled similar issues for VB->C# in #540, #547, #550. Here are a few known bugs that occur in converting the standard winforms generated project. They're roughly ordered from easier to harder.

Help wanted

As well as VB's decreasing support from Microsoft and tool vendors, VB has a reputation as a starter/prototype language, but not being so good long term, robust projects. This means the number of people trying to convert a whole winforms project C#->VB is relatively small compared to converting snippets. On top of that, there are major differences in how VB structures winforms code, so there's a large amount of work involved in getting this right.

For those reasons this hasn't been a priority area, only occasionally improved, but if anyone's interested in working on this area I'd be very happy to support them doing so.

There's a May 2019 fork of this project which was itself converted to VB, and may well have fixes for some of these issues: https://github.com/icsharpcode/CodeConverter/issues/8#issuecomment-554673493 The author is very happy for them to be reintegrated where possible

Details

ad48hp commented 5 years ago

The structure of this codebase seem to very complicated for me yet. Could you look at repairing it yourself ? Also, i'm quite surprised this got quite w/o a touch for an entire year, there are seemingly very few people that care about this and also have abilities to recode it well..

GrahamTheCoder commented 5 years ago

If you're interested, I've done the tweaks to make the converted code actually run here: https://github.com/GrahamTheCoder/FastStyleTransfer/pull/1

Now there's an expected output for the input, it should be easier for someone to turn into unit tests and fix the individual issues in the converter. I do regard your interest in this as an upvote to work on it, so I'll aim to fix a couple of the more commonly hit issues sometime in June, though can't promise anything specific.

I'm happy to take any specific ideas for making it easier to get started on the project by the way. For example, anything I could add to Contributing.md.

As I mention on the readme, the C#->VB side of things is a bit neglected. It seems there are fewer experienced devs who know both VB and C#, and want to convert in that direction. For VB->C# people seem to more often convert a whole project, whereas for C#->VB, people more commonly seem to use the snippet conversion so they can use things from stack overflow for example. So there's also an element of how much benefit people stand to gain by helping out.

Thanks for your feedback!

ad48hp commented 5 years ago

Hah, great džob yet! There were just some mistakes like the event handles were missing. I already fixed it, it was caused by missing "WithEvents" constructorz in the Designer vb file and handles in the main vb file..

ad48hp commented 5 years ago

Also, i tried to convert the project back to C#, so it would be easier for the original author to continue working on it, but it compiled with some errors (mostly casting). Could you look at it then ?

GrahamTheCoder commented 5 years ago

Can you give specific examples of code snippets that break?

DotNetTester commented 4 years ago

I converted an old open source project on the google code archive to VB .NET and there were hundreds of errors and warnings. I suspect most of it is due to the differences in how the WinForms Designer works in VB .NET vs. C#.

Archive link. https://code.google.com/archive/p/little-painter/downloads

Download link. https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/little-painter/LittlePainter%200.2.zip

My original post. https://github.com/icsharpcode/CodeConverter/issues/603

DotNetTester commented 4 years ago

I'm trying to find a way to export all the errors and warnings in Visual Studio but so far I haven't been successful. I could upload it here if I found a way.

GrahamTheCoder commented 4 years ago

Thanks. No need post the full list of errors unless they seem environment specific somehow - I can easily run it against the repo to get them. Let me know if there are any particular patterns you spot that haven't been mentioned on linked issues

GrahamTheCoder commented 4 years ago

@DotNetTester I've had a look at that project now.

One new issue I noticed was that the default imports need adding to the project file. So adding

  <ItemGroup>
    <Import Include="System" />
    <Import Include="System.IO" />
  </ItemGroup>

into the vbproj fixes almost all the errors

Other than that, I just made Program.Main public, and changed:

LittlePainter/LittlePainter/common/SortedDoublyLinkedList.vb
-                Console.Write(en.Current & " ")
+                Console.Write(en.Current.ToString() & " ")

It then compiles, but runs into the resources issue (added to the description of this issue).

Thanks!

jk9112605 commented 3 years ago

Hah, great džob yet! There were just some mistakes like the event handles were missing. I already fixed it, it was caused by missing "WithEvents" constructorz in the Designer vb file and handles in the main vb file..

It will be great at C# to VB, if remove AddHandler in Designer.vb
convert private objName As className to Friend WithEvents objName as clasName in form.vb file At end of each event function add Handles objName.EventHandlerName

Thanks.