Convert VB.NET To C# [duplicate] - c#

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert VB.NET code to C#
Hey,
I'm looking for a powerfull tool who can convert & C# code to VB.NET or vice-versa.
I've tried some websites but they are not very good.
Any ideas ?
Thanks

I have used .Net Reflector. Just load the DLL and select the language C#, VB etc.
See http://reflector.red-gate.com/download.aspx

The two most popular ones are developerFusions and teleriks:
http://www.developerfusion.com/tools/convert/vb-to-csharp/
http://converter.telerik.com/
Sometimes you will have to hand convert certain parts of the converted code that the converters have trouble understanding but on the whole they do a large part of the heavy lifting for you.
One example is that vb.net uses array indexes as () whereas c# uses []. For some reason I have seen the developerFusion get confused and leave these as () in the c# code which confuses the compiler.
I mostly use this for quick translations of code when I am answering forum questions and the op has specifically requested a vb.net answer. I just find it easier to code it in c# and then convert it.
If you have a very large project then you might find it tedious to convert each of the individual pages. In this case you might (I haven't actually tested this) find a way to convert the code more quickly by using the #develop IDE. I only say this as the developerFusion online converter is actually powered by code from this tool.
http://www.icsharpcode.net/OpenSource/SD/
Another thing to remember, as has been mentioned elsewhere in this thread, is that its entirely possible to mix and match languages within a single project. The only restriction to my knowledge is that you can only have one language per folder. This is to do with the way that the .net code is compiled. By default each folder is compiled into its own assembly.
If you really need to mix and match on the same page I think you could make a usercontrol to contain the code for one language and put it onto a different languages page.

You've both online- and offline solutions at your disposal.
A very popular online converter: Developer Fusion's Converter
Reflector has for long been the most popular offline source inspector. It allows you to view any assembly in the language of your choice. However, recently they changed and it is now no longer free.

Related

VB.NET for a C# Developer

I may be starting a new job which requires VB.NET but I am a C# developer and even though I may be able to understand the code, writing it from scratch seems to be a hassle for me for a while.
There are C#>VB.NET converter out there (online) and where you paste your C# code and it converts it into VB.NET code. My question is whether there is any person who experienced this and whether it is a good temp solution or I am gonna experience so much difficulties with that? Do they convert good?
And probably I am gonna run the codes on Asp.net.
An example converter: http://www.developerfusion.com/tools/convert/csharp-to-vb/
Thanks in advance.
Don't use converters - learn VB.NET and the differences between its syntax and C#.
There is a very good comparison cheat sheet here to get you started.
In practice, you will find that most of the time you are interacting with familiar .NET objects in the same way and you only have some syntax differences (though generics and delegate syntax are such a pain that one tends to shy away from them).
Microsoft has stated that they are trying to bring both language to feature parity, so anything you can do in C#, you should be able to do with VB.NET (with minor differences normally).
Update - don't forget that compiled code (in assemblies) should work identically in both languages (assuming CLS compliance), so you could write a library in C# for use with VB.NET and vice versa.
You can try the Telerik Code Converter
That being said, it would be a valuable exercise to convert the code manually. You'll gain a good amount of experience by doing a manual conversion and you'll learn some of the key differences between the 2 languages that may help you going forward.
I'd say your code will work, but you'll miss some special features for which there is no C# equivalent and which would make your code fit better with the language. Some examples:
In C#, you assign event handlers with +=, which will be translated to AddHandler. In VB, however, it's much more common to use WithEvents instance variables. This is especially relevant for ASP.NET, where C# often uses the AutoEventWireup feature, which done in VB through WithEvents instead.
In C#, you access XML through LINQ to XML method calls, which will be translated to the matching method calls in VB. However, in VB, it's more natural to use the integrated language support for XML.
This is good for converting the bulk of your code, but it's not a total solution. One thing you will have issues with is the converter knowing what to do with C#'s indexer brackets ([]) vs. method parentheses (()). VB uses parenthesis for indexers and methods and there's no way for it to know which to use.
I've gone through conversion hell with these things and finally decided that they were just too much trouble and that it was much easier to just convert it by hand. I come from a VB background, so this wasn't a huge deal for me.
For what you want to do, though, you need to learn VB.NET syntax. Writing everything in C# and converting it to VB.NET is not a good, long-term solution. You will eventually have to learn VB.NET. Your manager(s) will not be keen to the fact that you're not learning the core fundamentals of the language you were hire to program in.
Don't do it... There are converters, but you will find that once you learn the key differences that you will be fine. You will interact with the .NET libraries in the same way so much of the programming will be the same.
I just changed jobs recently and went the opposite direction. I'm glad I took the time to learn C#. Major dividends in the end and you'll be more versatile.

Translate vbscript to C# using ANTLR

I need to write a translator for vbscript to c#. What would be the basic steps invloved to translate using ANTLR? I am not very clear about whether to use grammar (lexer/parser? file or stringtemplate or AST or all.
Suggestions?
Thanks in advance.
Is this really possible?
I'm "translating" (read: rewriting) a MS Access/VBA application since two years to C# and found out that even the online available converters (like this one which is more VB.NET, but anyway) fails at most basic conversions.
So my assumption until now is that there are way too much kind of constructs that are simply not translatable from VBScript to C#.
See What kinds of patterns could I enforce on the code to make it easier to translate to another programming language?
The easiest way to translate VBScript to C# would be using VB.NET as an intermediate step. VBScript is very similar to VB.NET (there are some differences though), so all you need to do is copy-paste your code in a new solution in Visual Studio. After you make a few changes, the code will be able to compile.
You can leave it at that (you can call the resulting DLL from your C# code), but even if you don't want to do that, converting VB.NET to C# is trivial. You can even decompile your resulting DLL/EXE (although this will lead to some loss of information) or use any online VB.NET to C# converter.

Looking for a C# code parser [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I'm looking for a set of classes (preferably in the .net framework) that will parse C# code and return a list of functions with parameters, classes with their methods, properties etc. Ideally it would provide all that's needed to build my own intellisense.
I have a feeling something like this should be in the .net framework, given all the reflection stuff they offer, but if not then an open source alternative is good enough.
What I'm trying to build is basically something like Snippet Compiler, but with a twist. I'm trying to figure out how to get the code dom first.
I tried googling for this but I'm not sure what the correct term for this is so I came up empty.
Edit: Since I'm looking to use this for intellisense-like processing, actually compiling the code won't work since it will most likely be incomplete. Sorry I should have mentioned that first.
While .NET's CodeDom namespace provides the basic API for code language parsers, they are not implemented. Visual Studio does this through its own language services. These are not available in the redistributable framework.
You could either...
Compile the code then use reflection on the resulting assembly
Look at something like the Mono C# compiler which creates these syntax trees. It won't be a high-level API like CodeDom but maybe you can work with it.
There may be something on CodePlex or a similar site.
UPDATE
See this related post. Parser for C#
If you need it to work on incomplete code, or code with errors in it, then I believe you're pretty much on your own (that is, you won't be able to use the CSharpCodeCompiler class or anything like that).
There's tools like ReSharper which does its own parsing, but that's prorietary. You might be able to start with the Mono compiler, but in my experience, writing a parser that works on incomplete code is a whole different ballgame to writing one that's just supposed to spit out errors on incomplete code.
If you just need the names of classes and methods (metadata, basically) then you might be able to do the parsing "by hand", but I guess it depends on how accurate you need the results to be.
Mono project GMCS compiler contains a pretty reusable parser for C#4.0. And, it is relatively easy to write your own parser which will suite your specific needs. For example, you can reuse this: http://antlrcsharp.codeplex.com/
Have a look at CSharpCodeCompiler in Microsoft.CSharp namespace. You can compile using CSharpCodeCompiler and access the result assembly using CompilerResults.CompiledAssembly. Off that assembly you will be able to get the types and off the type you can get all property and method information using reflection.
The performance will be pretty average as you will need to compile all the source code whenever something changes. I am not aware of any methods that will let you incrementatlly compile snippets of code.
Have you tried using the Microsoft.CSharp.CSharpCodeProvider class? This is a full C# code provider that supports CodeDom. You would simply need to call .Parse() on a text stream, and you get a CodeCompileUnit back.
var codeStream = new StringReader(code);
var codeProvider = new CSharpCodeProvider();
var compileUnit = codeProvider.Parse(codeStream);
// compileUnit contains your code dom
Well, seeing as the above does not work (I just tested it), the following article might be of interest. I bookmarked it a good long time ago, so I believe it only supports C# 2.0, but it might still be worth it:
Generate Code-DOMs directly from C# or VB.NET
It might be a bit late for Blindy, but I recently released a C# parser that would be perfect for this sort of thing, as it's designed to handle code fragments and retains comments:
C# Parser and CodeDOM
It handles C# 4.0 and also the new 'async' feature. It's commercial, but is a small fraction of the cost of other commercial compilers.
I really think few people realize just how difficult parsing C# has become, especially if you need to resolve symbolic references properly (which is usually required, unless maybe you're just doing formatting). Just try to read and fully understand the Type Inference section of the 500+ page language specification. Then, meditate on the fact that the spec is not actually fully correct (as mentioned by Eric Lippert himself).

Is it possible to convert VBA to C#?

I have a few modules of block of code in VBA to run on few Access databases. I would like to know how I should proceed if I want to convert the coding to C# environment. And is it possible to implement and obtain the same results as I am getting now with Access and VBA?
I am completely new to C# at this point.
Automatic conversion isn't possible at the moment, but doing it manually will also help improve your C# skills. There's a Top 10 article here that takes you through the common differences:
http://msdn.microsoft.com/en-us/library/aa164018%28office.10%29.aspx
You may also find the following links useful:
The MSDN page for developing Office solutions with C#:
http://msdn.microsoft.com/en-us/library/ms228286.aspx
The MSDN Visual C# application development page (for starting out in C# development):
http://msdn.microsoft.com/en-us/library/aezdt881.aspx
Good luck and I hope this helps.
One thing to be aware of is that some object name spaces and library references are included automatically when you are coding in VBA. These need to be explicitly added when working in C#. For example,
Selection.TypeText("foo")
in VBA becomes
using Microsoft.Office.Interop.Word;
Application word = new Application();
word.Selection.TypeText("foo");
in C#. Library references can be added by right-clicking the References folder in the Solution Explorer and choosing "Add Reference".
Generally, you should be able go convert the code manually. You won't find any automated code converters that will do it.
That being said, the frameworks which you use to access data are quite different in the C# and VBA worlds (they're even quite different betwween VB.NET and VBA!). So you're going to want to read up on ADO.NET before you get started.
Given that VBA is VERY similar to vb.net, then I would suggest you converting to VB.net. In fact you can often cut + paste in code, especially if such code is from a code module. If your code has DAO.Recordsets, then I suggest creating a class in VB.net that “mimics” the dao.recordset. As such most VBA code can near 100% go into a vb.net code module. So this suggests that you don’t need a conversion, but simply move the VBA code to VB.net. In fact 99% of the commands in VBA exist with the SAME syntax in VB.net.

What is the best C# to VB.net converter? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
While searching the interweb for a solution for my VB.net problems I often find helpful articles on a specific topic, but the code is C#. That is no big problem but it cost some time to convert it to VB manually.
There are some sites that offer code converters from C# to VB and vice versa, but to fix all the flaws after the code-conversion is nearly as time-consuming as doing it by myself in the first place.
Till now I am using http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx
Do you know something better?
Telerik has a good converter that is based on SharpDevelop that has worked pretty well over the years, though it has not been updated in years (due to it being based on SharpDevelop).
I've recently come across a roslyn based converter as well. I don't know how well it works or how well maintained it is, but as it's open source you can always fork it and update it as needed.
If you cannot find a good converter, you could always compile the c# code and use the dissasembler in Reflector to see Visual Basic code. Some of the variable names will change.
I currently use these two most often:
http://converter.telerik.com/
http://www.carlosag.net/tools/codetranslator/
But have also had some success with these others:
http://converter.atomproject.net/
http://www.dotnetspider.com/convert/Csharp-To-Vb.aspx
http://www.developerfusion.com/tools/convert/csharp-to-vb/
SharpDevelop has a built-in translator between C# and VB.NET. Is not perfect thought (e.g. the optional values in VB.NET doesn't have an equivalent in C#, so the signature of the converter method must be edited), but you can save some time, as you are making all operations inside an IDE and not a webpage (copy C# code, paste, hit button, copy VB.NET code, paste on IDE :P )
I think the best thing to do is learn enough of the other language so that you can rewrite by hand, there's some quite difficult differences in certain aspects that I'm not sure a converter would handle very well. For example, compare my translation from C# to VB of the following:
public class FileSystemEventSubscription : EventSubscription
{
private FileSystemWatcher fileSystemWatcher;
public FileSystemEventSubscription(IComparable queueName,
Guid workflowInstanceId, FileSystemWatcher fileSystemWatcher) : base(queueName, workflowInstanceId)
{
this.fileSystemWatcher = fileSystemWatcher;
}
becomes
Public Class FileSystemEventSubscription
Inherits EventSubscription
Private myFileSystemWatcher As FileSystemWatcher
Public Sub New(ByVal QueueName As IComparable, ByVal WorkflowInstanceID As Guid, ByVal Watcher As FileSystemWatcher)
MyBase.New(QueueName, WorkflowInstanceID)
Me.myFileSystemWatcher = Watcher
End Sub
The C# is from the Custom Activity Framework sample, and I'm afraid I've lost the link to it. But it contains some nasty looking inheritance (from a VB point of view).
I am using a free Visual Studio 2012 plug-in named Language Convert
It works perfectly on 2010/2012, unfortunately isn't working at VS 2013 yet.
The conversion is not 100% accurate, but it is definitely very helpful, to launch for the first time it is a bit tricky, check before the image below :
Last I checked, SharpDevelop has one and it is open source too.
You can load your DLL or EXE into Redgate's (formerly Lutz Roeder's) .Net Reflector, select your method and then the desired language from the language combo. The code of the selected method will be displayed in the selected language.
I hope this helps.
You can try this one converter. There is functionality for C# to VB and VB to C#.
Hope this helps.
Carlos Aguilar Mares has had an online converter for about 40 forevers - Code Translator but I would agree that Reflector is the better answer.
While not answering your question, I will say that I have been in a similar position.
I realised that code samples in C# were awkward when I was really starting out in .NET, but a few weeks into my first project (after I grown more familiar with the .NET framework and VB.NET itself), I found that it was interesting and sometimes beneficial to have to reverse-engineer the C# code. Not just in terms of syntax, but also learning about the subtle differences in approach - it's useful to be open-minded in this respect.
I'm sticking with VB.NET as I learn more and more about the framework, but before long I'll dip my to into C# with the intention of becoming 'multi-lingual'.
Currently I use a plugin for VS2005 that I found on CodeProject (http://www.codeproject.com/KB/cs/Code_convert_add-in.aspx); it use an external service (http://www.carlosag.net/Tools/CodeTranslator/) to perform translation.
Occasionally, when I'm offline, I use a converter tool (http://www.kamalpatel.net/ConvertCSharp2VB.aspx).
The one at http://www.developerfusion.com/tools/convert/csharp-to-vb/ (new url) now supports .NET 3.5 syntax (thanks to the #develop guys once again), and will automatically copy the results to your clipboard :)

Categories

Resources