Click here to Skip to main content
Licence 
First Posted 29 Apr 2003
Views 37,023
Bookmarked 10 times

Converting JScript.NET to C#.

By | 29 Apr 2003 | Article
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

About the Author

essence

Web Developer

United States United States

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralA higher quality VB to C# converter Pinmemberpickle head9:34 28 May '07  
Generalpity PinmemberSwenni6:50 19 Mar '07  
Questionwhere is the code? Pinmemberkornstyle8:44 31 Mar '05  
AnswerRe: where is the code? PinmemberMichael Freidgeim17:41 19 Jun '06  
It is saved in the same directory as source file, but with extension .CS
 
Michael Freidgeim.
Blog: http://geekswithblogs.net/mnf/
GeneralGreat work PinmemberAnthony_Yio23:07 19 Jun '03  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 30 Apr 2003
Article Copyright 2003 by essence
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid