- Home /
Trouble with converting some JS code to C#
I have a nice object pooling script written in JS but I need C#. I've been teaching myself C# and don't know jack about JS, so I'm going through it slowly. I've come to a few lines where I don't quite understand their function or how to convey it in C#:
static var instance : EasyObjectPool;
function Start () {
instance = this;
Particularly, I don't understand the first line. It's not really like anything I've seen in C# before. EasyObjectPool is the name of the script this code is in. I'd post the full script but this "instance" variable is only used in these lines, so I don't figure the rest is necessary. The JS code is calling "instance" a variable (right..?) but I don't know what to call it in C#.
Any input is appreciated
so is easyobjectpool the class or a variable if a variable
static public EasyObjectPool instance;
void Start () {
instance = this;
}
If you need help in the rest of the conversion I try to help
Answer by tapir · Jun 19, 2014 at 01:29 AM
It seems like you are using a singleton pattern. You can use the same name for the variable. Normally you use singleton variables to avoid having multiple instance of a class. For example EasyObjectPoolController could instance an instance of EasyObjectPool, only if the static varibale EasyObjectPool.instance is null.
For more about singleton pattern this is the wikipedia article, its a basic start. Hope it helps!
public class EasyObjectPool
{
static EasyObjectPool instance;
void Start ()
{
instance = this;
}
}
Thanks, that's a new concept to me. Whether the rest of my script will be right or not I don't know yet but I'll ask a separate answer is the final thing is giving me problems
I'm glad it helped. I just translated all my project (50+ scripts) from js to c#, so I might be able to help with that. I wrote a custom python script that translated my very strict js. You definitely want that if you are dealing with lots of scripts.
There is also an online translator for that, but I'm a bit paranoid and don't like the idea of uploading my code to someone's computer, so I didn't used it so I have no idea if it works or not.
Link to online translator: http://www.m2h.nl/files/js_to_c.php
Your answer
Follow this Question
Related Questions
How to make this line work in C#? 1 Answer
Help converting this to C# - a few issues 3 Answers
java to C# conversion 1 Answer
What is this C# code in javascript 0 Answers
Converting a javascript to C# 2 Answers