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.
Related
I know this is wrong (trying to code in C# with a C/C++ mindset).
But is it possible to create inline functions / inline recursive functions (til the Nth call) / macros in C#?
Because if I intend to call the same function 1000 times, but it's a simple function, the overhead of creating the stack is quite big... And unnecessary.
In C i could use inline functions. Is there something like that in C#?
Again... I'm not saying that C/C++ is better... I'm just new to C# and have none to ask those simple questions...
Inline functions in C#?
Finally in .NET 4.5, the CLR allows one to force1 method inlining
using MethodImplOptions.AggressiveInlining value. It is also available
in the Mono's trunk (committed today).
[MethodImplAttribute(MethodImplOptions.AggressiveInlining)]
void Func()
{
Console.WriteLine("Hello Inline");
}
The answer should be: don't worry about it. No need to start with micro-optimizations unless you've tested it and it's actually a problem. 1000 function calls is nothing until it's something. This is majorly overshadowed by a single network call. Write your program. Check the performance against the goals. If it's not performant enough, use a profiler and figure out where your problem spots are.
Yes, C# is a very powerful general purpose language that can do nearly anything. While, inline functions / macros are generally frowned upon, C# does provide you with multiple tools that can accomplish this in a more concise and precise fashion. For example, you may consider using template files which can be used (and reused) in nearly all forms of .NET applications (web, desktop, console, etc).
http://msdn.microsoft.com/en-us/data/gg558520.aspx
From the article:
What Can T4 Templates Do For Me?
By combining literal text, imperative code, and processing directives, you can transform data in your environment into buildable artifacts for your project. For example, inside a template you might write some C# or Visual Basic code to call a web service or open an Excel spreadsheet. You can use the information you retrieve from those data sources to generate code for business rules, data validation logic, or data transfer objects. The generated code is available when you compile your application.
If you're new to programming I would recommend against implementing templates. They are not easy to debug in an N-Tier application because they get generated and ran at run-time. However, it is an interesting read and opens up many possibilities.
I know you can dynamically create a .NET assembly using Emit, System.Reflection and manually created IL code as shown here.
But I was wondering is it possible to dynamically create and execute C# code block real-time, in a running application. Thanks for any input or ideas.
Edit:
As I understand CodeDOM allows you to compile C# code into EXE file rather than "just" executing it. Here's some background information and why (as far as I can tell) this isn't the best option for me. I'm creating an application that will have to execute such a dynamically created code quite a lot [for the record - it's for academic research, not a real-world application, thus this cannot be avoided]. Therefore, creating/executing thousands of dynamically created EXEs aren't really efficient. Secondly - all dynamic code fragments returns some data that is hard to read from separately running EXE. Please let me know if I'm missing something.
As for DynamicMethod approach, pointed out by Jon Skeet, everything would work like a charm if there would be an easier way to write the code itself rather than low level IL code.
In other words (very harshly speaking) I need something like this:
string x = "_some c# code here_";
var result = Exec(x);
Absolutely - that's exactly what I do for Snippy for example, for C# in Depth. You can download the source code here - it uses CSharpCodeProvider.
There's also the possibility of building expression trees and then compiling them into delegates, using DynamicMethod, or the DLR in .NET 4... all kinds of things.
Yes, it is. There are several applications that do just that - see LinqPad and Snippy.
I believe they use the CSharpCodeProvider.
Yes. See this MSDN page regarding using the CodeDOM.
Some example code extracted from the above mention page:
CodeEntryPointMethod start = new CodeEntryPointMethod();
CodeMethodInvokeExpression cs1 = new CodeMethodInvokeExpression(
new CodeTypeReferenceExpression("System.Console"),
"WriteLine", new CodePrimitiveExpression("Hello World!") );
start.Statements.Add(cs1);
You can Use CodeDom to generate code and create in memory assemblies based on that generated code. THose can then be used in the current application.
Here's a quick link to the msdn reference, it is quite extensive material.
MSDN: Using the CodeDom
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.
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.
Is there a tool to convert a VB.NET 2005 project to a C# 2008 project. I am trying to convert our project to VS 2008 and mostly port all the vb.NET code in some projects to C# 3.0/3.5.
I had a similar decision to make with a VB.net project.
Solution was a compromise, I decided to run with dual VB and C#.
Upgrading VB.NET 2005 to 2008 is easy bit.
Added CSharp and VB folders to App_Code and
<codeSubDirectories>
<add directoryName="CSharp" />
<add directoryName="VB" />
</codeSubDirectories>
to compilation section of web.config
As Kev says, it's not as straight forward as you might expect, and you will likely run into unexpected issues , that make running with dual language support the best solution
I know this doesn't directly answer the question, but it's an alternative approach
You could check out SharpDevelop. It's an open source .NET development environment.
SharpDevelop has some code conversion built in.
Take a look at this.
http://www.developerfusion.com/tools/convert/vb-to-csharp/
Try to google term "convert vb.net project to c# project", you will find a couple of options there.
There's a tool on CodeProject called GBVB (Goodbye VB) that purports to 'flawlessly' convert VB.NET code to C# code. However the author does add a number of caveats such as the problems with ambiguous code, optional parameters etc.
Another approach could be to use Denis Bauer's Reflector.FileDisassembler .NET Reflector add-in and disassemble the compiled assemblies back to C#.
Handling .aspx/.asmx pages and their code-behind files would probably be a special case because you'd need to manually change the <%# Page Codebehind="Default.aspx.cs" %> directives to point to the correct source files.
Automatic VB.NET to C# conversion isn't really as straight forward as it initially appears to be. I've been here before and gave up on a fairly large project, there's a lot of fiddling about with specific edge cases where the translation either wouldn't compile or produced unexpected behaviour at runtime. I elected to go for a dual language solution where all new code was written in C# and older VB.NET code was ported gradually over time.
hi this code answer not
public student ()
{}
private string code;
private string name;
public string code()
{
set (code=Value);
Get return code;
}
Telerik has a converter you might want to take a look at.
Since others have mentioned online conversion tools I've found www.carlosag.net to work the best for my code. Maybe b/c I work other 3rd party API's but it seems like a lot of the other online converter just error out if they hit a line they can't convert.
SharpDevelop is quite good, but at my company we've found VBConversions to provide a much more complete conversion. It's a commerical app though, but for the time saved over SharpDevelop it was a no-brainer for us.
As a specific example, one thing we found that SharpDevelop didn't convert correctly was VB indexes, which use curvy brackets. It seemed unable to distinguish between indexes and method calls so didn't convert the indexes to square brackets. VBConversions converted them fine. This one thing made it worth its purchase for us.
It looks like Tangible Software have a very similarly product to VBConversions, but I don't have any experience with it. Jon Skeet suggested it though, so it must be pretty good.