Click here to Skip to main content
15,891,253 members
Articles / Programming Languages / JScript .NET
Article

Converting JScript.NET to C#.

Rate me:
Please Sign up or sign in to vote.
2.09/5 (8 votes)
29 Apr 20033 min read 57.5K   883   10   7
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


Written By
Web Developer
United States United States
Just a crazy developer with crazy ideas of grandure.

What's that? Anal probe? NO, I said "Anal CODE!" Get that thing away, and get back to EditPlus.

Comments and Discussions

 
QuestionJscript.net OpenGL Pin
Member 150914636-Mar-21 8:10
Member 150914636-Mar-21 8:10 
Questionc# to js.net Pin
filmee2426-Mar-14 22:07
filmee2426-Mar-14 22:07 
GeneralA higher quality VB to C# converter Pin
pickle head28-May-07 9:34
pickle head28-May-07 9:34 
Generalpity Pin
Swenni19-Mar-07 6:50
Swenni19-Mar-07 6:50 
It hangs in the 2nd line on a simple '{'. Pity.
Questionwhere is the code? Pin
kornstyle31-Mar-05 8:44
kornstyle31-Mar-05 8:44 
AnswerRe: where is the code? Pin
Michael Freidgeim19-Jun-06 17:41
Michael Freidgeim19-Jun-06 17:41 
GeneralGreat work Pin
Anthony_Yio19-Jun-03 23:07
Anthony_Yio19-Jun-03 23:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.