CodeConverter icon indicating copy to clipboard operation
CodeConverter copied to clipboard

C# -> VB: Better winforms conversion

Open GrahamTheCoder opened this issue 7 years ago • 10 comments

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

  • [x] DefineConstants in the project file needs to be converted between comma separated (VB) and semicolon separated (CSharp)
  • [x] Everything is incorrectly qualified as [global] instead of Global
  • [x] Add Option Infer to project file (to allow type inference)
  • [x] Convert MultiplyAssignmentStatement
  • [ ] If one side of an & isn't a string, call ?.ToString() on it
  • [ ] If result of a division is type int, then use integer division \
  • [ ] Add default project imports into vbproj (e.g. System)
  • [ ] Conversion is very slow for large files/projects due to the clashing renamer calling GetCsSymbolsDeclaredByMethod which calls LookupSymbols with a wide scope a lot of times
  • [ ] Convert invoke method
  • [ ] All parts of a partial class need the same modifier
  • [ ] Ensure entry point Sub Main becomes public
  • [ ] Fix resource references (I think the fix is the same as it was VB->CS)
  • [ ] CS -> VB Coalesce DefineDebug and DefineTrace into DefineConstants
  • [ ] Use https://github.com/mashmawy/FastStyleTransfer as test case
  • [ ] Generate something that works in the designer post-conversion https://github.com/icsharpcode/CodeConverter/issues/593

GrahamTheCoder avatar Aug 24 '18 08:08 GrahamTheCoder

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..

ad48hp avatar May 30 '19 18:05 ad48hp

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!

GrahamTheCoder avatar May 31 '19 09:05 GrahamTheCoder

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 avatar May 31 '19 15:05 ad48hp

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 ?

ad48hp avatar Jun 01 '19 17:06 ad48hp

Can you give specific examples of code snippets that break?

GrahamTheCoder avatar Jun 01 '19 22:06 GrahamTheCoder

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 avatar Aug 06 '20 18:08 DotNetTester

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.

DotNetTester avatar Aug 06 '20 19:08 DotNetTester

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 avatar Aug 06 '20 20:08 GrahamTheCoder

@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!

GrahamTheCoder avatar Aug 09 '20 13:08 GrahamTheCoder

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.

jk9112605 avatar Jan 27 '21 03:01 jk9112605