Background
I come from the Javascript world. In my scenario, I want to store data that would look like the following when in JSON:
let operations = {
"a" : {
"key" : "a"
"desc" : "Add"
"response" : " "
},
"s" : {
"key" : "s"
"desc" : "Subtract"
"response" : "-"
},
"m" : {
"key" : "m"
"desc" : "Multiply"
"response" : "*"
},
"d" : {
"key" : "d"
"desc" : "Divide"
"response" : "/"
},
}
I'd like to be able to access it dynamically, so that if I have a variable, currentOp = "a"
, I can use that variable to access it. In Javascript, I'd write:
let description = operations[currentOp].desc
In C#
Inside my main class, I tried out making a tuple, then accessing:
public (
(string desc, string key, string responseChar) a,
(string desc, string key, string responseChar) s,
(string desc, string key, string responseChar) m,
(string desc, string key, string responseChar) d
) ops = (
("Add", "a", " "),
("Subtract", "s", "-"),
("Multiply", "m", "*"),
("Divide", "d", "/")
);
I can access it when using dot notation:
ops.a.desc
I get errors if I try to use bracket notation:
ops[curOp].desc
So then I tried making a separate class:
public class Operations {
static public (string userOption, string userChar, string responseChar) a { get; } = ("a - Add", "a", " ");
static public (string userOption, string userChar, string responseChar) b { get; } = ("s - Subtract", "s", "-");
static public (string userOption, string userChar, string responseChar) c { get; } = ("m - Multiply", "m", "*");
static public (string userOption, string userChar, string responseChar) d { get; } = ("d - Divide", "d", "/");
}
But when accessing again, it works with dot notation, but fails with bracket notation.
Question
What am I doing wrong here, and am I missing a better way?
Notes:
I did play about with ExpandoObject, but I couldn't get that to initiate values with a single line, so wasn't able to make it a static property of the class.
CodePudding user response:
In your case, You are using a tuple and trying to access data using square brackets.
In the case of a tuple, [] is an indexer. With the help of square brackets, you can access elements at a particular location.
like,
var description = ops[0].desc; //Add
When you try using .a
, you read the description value by the tuple's field name.
var description = ops.a.desc
| |
| Tuple field name
Tuple
Because you are trying to access the value by field name, the second approach is working fine.
In the first approach, it will start working if you pass an index instead of a field name.
An elegant way is to create a class called Operation
,
public class Operation
{
public string Key { get; set; }
public string Description { get; set; }
public string Response { get; set; }
public Operation(string key, string desc, string response)
{
this.Key = key;
this.Description = desc;
this.Response = response;
}
}
Now create a dictionary that will store the key and instance of the Operation
class,
Dictionary<string, Operation> dict = new Dictionary()
{
{"a", new Operation("a", "Add", " ")},
{"s", new Operation("s", "Subtract", "-")},
{"m", new Operation("m", "Multiply", "*")},
{"d", new Operation("d", "Division", "/")},
}
Now access value using an appropriate key,
var result = dict["a"].Description;