0

So Im redoing one of my old browser based javascript games and recoding it in c# i really liked the layout of my code and how smooth the workflow was so I wanna keep that same pattern when I code it in c#

So I have some nested arrays that look like this

var names = {
    town: "",
    mayor: "",
},
  wood = {
    name: "wood",
    amount: 0,
    increment: 0,
    max: 100,
    storage: 0,
    storageCost: {
    wood: 50,
    stone: 50
  }
};

Ive been trying to find out what the c# equivalent is but with no luck im sure this is a thing they have in c#(cause why wouldnt they?)

SO yea Answer providing a link to maybe some documentation? or just letting me know what the equivalent is

Please do note I am not asking for you to code anything for me I just need to know the right syntax

EDIT: To be more precise I wanna be able to get a value as simple as wood.storageCost.stone and get the value 50

4
  • it is Dictionary<String, Object> or you can create custom class Commented Jul 19, 2017 at 23:05
  • @IlyaBursov dictionarys arnt gonna achieve what im trying to do thier not the same thing as a multidimensional array Commented Jul 19, 2017 at 23:20
  • arrays in javascript are hashmaps, hashmaps in c# are called dictionaries, you can achieve everything you want with them Commented Jul 19, 2017 at 23:21
  • 1
    Is there a reason you don't want to create classes for "names", "wood", and "storageCost"? That seems to be the most simple answer to me. Commented Jul 19, 2017 at 23:30

3 Answers 3

2

Although dynamic could be a solution, I would like to suggest another approach that is more close to one of C#'s main strengths: static typing.

So, to come close to your main goal, which is to access a value by simply doing wood.storageCost.stone, I would create the following classes:

public class Material
{
    public string Name { get; set; }
    public int Amount { get; set; }
    public int Increment { get; set; }
    public int Max { get; set; }
    public int Storage { get; set; }
    public StorageCost StorageCost { get; set; }
}

public class StorageCost
{
    public int Stone { get; set; }
    public int Wood { get; set; }
}

Then, you could instantiate it like so:

var wood = new Material 
{
    Name = "wood",
    Amount = 0,
    Increment = 0,
    Max = 100,
    Storage = 0,
    StorageCost = new StorageCost
    {
        Stone = 50,
        Wood = 50
    }
};

Afterwards, you can access the value like so:

wood.StorageCost.Stone

Some considerations:

  • The use of PascalCase is due to C#'s naming guidelines. More information here. You may very well use lowercase names, if you wish.
  • Although I created a class for your StorageCost, I suspect that you may want to add and remove storage costs. If so, you could use an IDictionary<string, int>, but then, to access the value, you would do something like wood.StorageCost["stone"].
  • The main advantage of this approach is that you'd have compile-time code checking, which might allow you to avoid bugs sooner. More information here
Sign up to request clarification or add additional context in comments.

Comments

1

You can use dynamic type.

E.g.:

dynamic names = new
{
    town = "",
    mayor = "",
};

dynamic wood = new
{
    name = "wood",
    amount = 0,
    increment = 0,
    max = 100,
    storage = 0,
    storageCost = new
    {
        wood = 50,
        stone = 50
    }
};

3 Comments

so in theory i could just call debug.log(wood.storageCost.stone and it will return 50?
Sort of. .NET/C# equivalent of console.log() is System.Console.WriteLine(). I'm not sure about debug.log() in JavaScript.
The debug.log is actually a function in the engine I use but I did give this a try theres no dynamic definition but there is a DynamicGI definition it doesnt seem to work the same as this is there any imports?
0

Someone suggested using the dynamic keyword, but honestly, I would follow an object oriented approach so that you don't have to repeat yourself, and you will get intellisense on all the classes you create.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.