Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Converting JScript.NET to C#.

0.00/5 (No votes)
29 Apr 2003 1  
Want to make it easy to make the jump to C#? Here's the answer.

Introduction

If you were originally creating ASP pages using JScript, and moved on ASP.NET you might have taken the route of JScript.NET, like I did.  I've done a lot with this language and can do anything I need to do with it.

I love JScript.NET but I'm finding that to be a big boy, I need to step up to the big C#.  And like me, you are probably sick of needing to reference the JScript engine if you are gonna be sharing components with others.

The Solution

The two languages are not too dissimilar.  They have very similar syntax and use many of the same reserved words and objects.

You could spend hours scraping through your code doing find and replace on many things until you have an equivalient cs file, but I got pretty sick of that early on.

So I made this tool to convert your code.  I have included everything, so please feel free to study or improve on it as you see fit. I have taken it to a level that I feel comfortable.  Any more, I might as well write the compiler.

How to use the tool

It is really straight forward.  You will need the .NET Framework installed.  Just run the app in the release folder.  Click the select button to choose the .js file to convert and then click convert.  You will need to be sure that the .js file is already compileable using jsc.exe, or you can't be sure how the tool will respond.  In the possibility that the app will hang, take note of what line it hangs on, break that like up more, and try again.  If it seems like a line is taking a while, give it up to twenty seconds before ending the task.

The resultant code is saved as a cs file adjacent to the original js file and is a best attempt to convert your code to C#, making certain assumptions which may need to be reverted after the fact.  You will most likely still need to debug, but it shouldn't take you very long and definitly not as long as if you did it by hand.

How it works

I use regular expression pattern matching to run Regex.Replace using MatchEvaluators.  Many of the more complex ones can take forever if trying to search an entire document.  So some of the replacements are done on the entire document and many more are done line by line.  For those not familiar with regular expressions or how they work in the .NET Framework take a look at my other article: Understanding Regular Expressions in .NET.  The tool I use to evaluate regular expressions was instrumental in being able to make this happen.  Running sample pattern matching to test and perfect groupings is very important and can be a tedious process, but well worth the result.

Conclusion

JScript.NET is really a great language and is very flexible, but you will need to be very concious of late binding that will eventually cause errors and reduce performance.  C# being the native .NET language, seems to be devoid of this issue and forces you to early bind and makes your compiled classes very tight and efficient.

It is time to move up.  Enjoy.

For those trying to convert from VB.NET, I found this article:

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here