I just installed VS 2017 a couple of days ago.
Normally I work in C#.
However, I have one project that was written in VB.Net.
I want to pull out a selection of VB procedures and functions and put them in a static class.
However, when I try to add a new Class to the project it only gives me the option of a C# class, no VB at all.
Yes I could convert the VB code to C# but that is a lot of work and I don't want to do it since I shouldn't have to.
Has anyone encountered this and if so did you find a solution?
Thanks.
C# and VB.Net can coexist in the same solution, but not the same project. You can add a second project to your solution. Make it a VB.Net class library. Add any VB.Net modules to the new class library. Add a reference in your C# project to the VB.Net class library. Build all and enjoy!
If you do decide to translate from VB.Net to C# to keep things cleaner, I have had good luck with the code translator found here: http://converter.telerik.com/
Related
This question already has answers here:
How to call a function written in VB from C# application
(3 answers)
Closed 1 year ago.
I wonder if you can open a VB.NET form1 from C# form1
like VB.NET Form1.Show(); inside the button function of the C# code. I just thought of this will trying learn various code in them, can someone help to verify if this is possible???
The answer is Yes, but you can't have C# and VB.net in a single project. So create a solution containing two projects, one in C# the other in Vb.Net and you can open the vb.Net project form in C# as you suggest.
There is one major consideration though - that's dependencies. For this to work, the vb.net project is a dependency of the c# project, so the C# project sees all the VB.Net project but not the other way around.
It is possible to configure the solution to make both projects reference eachother - here's a post about it. Reference in two projects
However that's complicated and so I'd suggest having a third project (or fourth if needed) that contains any code you want to have common to both the VB and C# projects as a dependency to them both.
I am using asp.net 2012 and have c# class project where I would like to include some existing vb.net classes.
What would be the best way to be able to use both c# and vb.net classes?
Either convert the vb.net classes into C# or put the vb.net classes into their own assembly and add a reference to the assembly.
put the VB.NET classes in a project of its own and then reference that project, just as you would any third party control.
As already mentioned, one of the alternatives is to convert c# to vb.net or vice-versa.
You can find many pages in the internet to do that, like these ones:
http://www.developerfusion.com/tools/convert/vb-to-csharp/
http://www.developerfusion.com/tools/convert/csharp-to-vb/
If you convert the code from one language to the other make sure you test the changes properly.
If you decide to keep VB.NET code in a separate Visual Studio project and reference it in the C# code you need to ensure that the VB.NET code is CLS compliant. This is a good starting point:
Writing CLS-Compliant Code
See also:
Cross-Language Interoperability
Make C# a .NET library and include it in your VB.NET/ASP.NET project.
Follow this http://msdn.microsoft.com/en-us/library/t990ks23.aspx for more info...
I'm building an c# app that is designed to replicate and enhance the functionality of an VB6 app developed many years ago.
My problem is that one module in the old app does some realy complicated things and trying to replicate this starting from specs would be a big pain for me. Even if this sounds like the best idea.
My question is : is there a way i could keep the old VB module and integrate it into the c# app, so that i wouldn't have to rewrite everything ?
Thanks!
Yes. You can turn the module into a COM object, and .Net will happily call into COM objects. Depending on what you have right now you may need to find a copy of Visual Studio 6 to make that happen (it's still available on MSDN if you have a subscription). To give you more exact instructions we'd need to see more of your code.
Visual Studio has a wizard that allows you to convert VB6 into VB.NET, which you could then reference in your C# project and use. However, this migration tool is no longer shipped with Visual Studio 2010 (it was shipped in older versions) Even still though, there generally is a lot of manual touchup work required, you are generally better off rewriting it by hand which could result in higher quality code, anyway. My recommendation to you is to rewrite the code, you can make touchups and make sure that it works correctly without having to go the COM/Interop route, but don't necessarily shun that option either. If rewriting all the code really is an issue, then interop is a solution.
You can use COM or export the module as a DLL. Here's some information for you regarding VB6 and COM.
In theory yes, turning it into a com module, and calling it from your c# code. Whether this would be easier or not, I am not sure.
The problem is that this issue will not go away, and if there are changes needed to the module, or the interface, then someone has to do it. So practical yes, but consider carefully what you are doing.
You can migrate the existing vb code to vb.net and compile it as a separate assembly and add it to the C# application.
Can't VS2010 (maybe 2008) support both VB and C# projects in the same solution? If I'm not mistaken, they can. So you could actually convert your VB6 code into VB.Net and have the project in your solution with the C# stuff.
Frequently i have to program in VB.Net and Visual C#, so it would be helpful if someone can provide with a list of Refrence for Classes of in VB.net and C#.
I mean like
My.Computer.Registry in VB Maps to Microsoft.Win32.Registry in C#.
Similarly i want a refrence of other classes.
In your example, you can use the latter in both C# and VB.NET. The former is a VB.NET "crutch" (or "shortcut", to put it nicely) so that VB developers can find stuff via IntelliSense, and are contained within a special assembly used in VB (but that can also needlessly be referenced from C#). My suggestion: don't use "My.*", use the real classes that are available from any .NET language (incl. C#) - it will make moving between languages easier and you will be able to ask BCL questions more clearly on SO.
I have to convert a C# project to VB.NET and VBA. It's a console app that uses the Microsoft Office API (Microsoft.Office.Interop.*).
My two questions:
C# -> VB.NET: .NET Reflector is a good tool for this, except that it doesn't preserve source code comments. Is there a way to do this?
.NET -> VBA: is there an automated tool to do this conversion? It would need to be a client side tool (because the code is proprietary).
The best conversion seems to be done by SharpDevelop. Open your project and from the tools menu and select convert code to. Since it has all of your c# source code it does a very good job.
There are many ways to convert C# to VB.Net; for example, try Developer Fusion.
It is not possible to automatically convert C# to VBA because the environment, language, and libraries are radically different. However, you could expose the C# code as a COM server and call it from a thin wrapper in VBA.
Answer for question #1:
You cannot use Reflector to preserve source code comments because Reflector is disassembling the compiled code, which has had all comments removed.
Consider using a commercial tool like InstantVB, which will converts C# source code into VB source code (including comments):
http://tangiblesoftwaresolutions.com/Product_Details/Instant_VB.html
You could convert the C# source code to VB.Net manually with this:
http://www.developerfusion.com/tools/convert/csharp-to-vb/
which will preserve comments. As far as automatically converting C# to VBA or VB6, I'd be willing to say that that is almost certainly going to be completely impossible. There are many aspects of C#/.Net that are not available in VBA or VB6 (like inheritance, for one).