Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Object in C#

Is there an easy way to create something like the following JS code:

var players = [
    {name:"Joe",score:25,color:"red",attribs:[0,1,2,3,4]},
    {name:"Jenny",score:1,color:"black",attribs:[4,3,2,1,0]}
];

in C# (for Unity 3d)?

I've already looked at List, Dictionary and ArrayList, but everything seam so ... inflexible and overcomplicated...

The main objective here is to have something flexible, that can be access from many other places w/o the need to remember array indexes, variable types etc. Probably can't be done in C#... But something that is fairly close should be enough. ArrayList maybe...?

Thank you.

like image 773
user3789335 Avatar asked Oct 22 '14 00:10

user3789335


3 Answers

This JavaScript being an Array of objects is directly transferable to C#, and you have multiple ways to do it and multiple collection classes to use (unlike JavaScript which only has one collection class)

It can almost be written verbatim in C# and is perfectly valid (Using Anonymous Types):

var players = new [] {
    new  {name = "Joe",score = 25, color = "red", attribs = new int[]{ 0,1,2,3,4}},
    new  {name = "Jenny",score = 1, color = "black", attribs = new int[]{4,3,2,1,0}}
};

But I'm not sure if this is going to achieve what you want (Functionality wise)

IMO creating a typed object would be a better approach (I would consider this to be a better approach in both C# and JavaScript), so I would approach it more like this (JavaScript):

function Player(name,score,color,attribs)
{
    this.name = name;
    this.score = score;
    this.color = color;
    this.attribs = attribs;
}

var players = [
    new Player("Joe", 25, "red", [0, 1, 2, 3, 4]),
    new Player("Jenny",1,"black", [4, 3, 2, 1, 0])
];

and the same thing in C#:

public class Player
{
    public string name;
    public int score;
    public string color;
    public int[] attribs;
}


Player[] players = new Player[]{
         new Player(){ name = "Joe", score = 25, color = "red", attribs = new int[]{0,1,2,3,4} },
         new Player(){ name = "Jenny", score = 1, color = "black", attribs = new int[]{4,3,2,1,0} },
};

or to get a lot more flexibility you can use them in a List like:

 List<Player> players = new List<Player>(){
             new Player(){ name = "Joe", score = 25, color = "red", attribs = new int[]{0,1,2,3,4} },
             new Player(){ name = "Jenny", score = 1, color = "black", attribs = new int[]{4,3,2,1,0} },
    };
like image 163
OJay Avatar answered Oct 04 '22 15:10

OJay


Could you make a Player model (class) with the Name, Score, Color, Attribs properties and store each Player in memory in a List<Player>:

public class Player
{
    public string Name { get; set; }
    public int Score { get; set; }
    public string Color { get; set; }
    public int[] Attribs { get; set; }
}

...

List<Player> myPlayers = new List<Player>();
Player joe = new Player();
joe.Name = "Joe";
joe.Score = 25;
joe.Color = "red";
joe.Attribs = new int[] { 0, 1, 2, 3, 4 };;
myPlayers.Add(joe);

Using System.Linq you could query the List<Player> for specific players Eg:

Player player = myPlayers.SingleOrDefault(p => p.Name == "Joe");

Extra credit - always test that player is not default before trying to use it:

if (player != default(Player))
{
    // do something with joe
}
like image 42
logikal Avatar answered Oct 04 '22 16:10

logikal


More or less match for syntax would be anonymous types:

var players = new[]{
    new {name = "Joe", score = 25, color = "red", attribs = new[]{0,1,2,3,4}},
    new {name = "Jenny", score = 1, color = "black", attribs = new []{4,3,2,1,0}}
};

but it have very different meaning than JavaScript (array of immutable strongly typed objects in C# vs. dictionary of duck typed object in JavaScript).

You may want to look into using dynamic and ExpandoObject which exists to support languages like JavaScript in .Net if they are present in Unity3d.

    dynamic person = new System.Dynamic.ExpandoObject();
    person.Name = "John Smith";
    person.Age = 33;

Note that C# is strongly typed language and it may be better to change way of thinking to match the language. Staying with JavaScript is reasonable option if you can't get away from duck typed languages.

like image 26
Alexei Levenkov Avatar answered Oct 04 '22 14:10

Alexei Levenkov