I want to create a list of strings whose name should be the value of string variable.
Overall I want to assign a variable value to a (object) listname.
(Pseudo Code):
string s = "listname";
list<String>.Name = s;
Closest thing you can do is use a Dictionary to reference the variables.
Dictionary<string, List<string>> myLists = new Dictionary<string, List<string>>();
var s = "listname";
myLists.Add(s, new List<string>());
// To access
var list = myLists[s];
Related
I have a list like,
List<string> list = new List<string>();
list.Add("MEASUREMENT");
list.Add("TEST");
I have a dictionary like,
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("BPGA", "TEST");
dict.Add("PPPP", "TEST");
dict.Add("RM_1000", "MEASUREMENT");
dict.Add("RM_2000", "MEASUREMENT");
dict.Add("CDMA", "TEST");
dict.Add("X100", "XXX");
Now, I want to get all matched data from dictionary based on list.
Means, all data from list match with dict value then get new dictionary with following mathched values
Is there any way to achieve this by using lambda expression?
I want result like this.
Key Value
"BPGA", "TEST"
"PPPP", "TEST"
"RM_1000", "MEASUREMENT"
"RM_2000", "MEASUREMENT"
"CDMA", "TEST"
Thanks in advance!
You should be using the dictionary like it is intended to be used i.e. a common key with multiple values for example:
Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
Then all you need to do when adding the values is:
dict.Add("TEST", new List<string>() { /*strings go in here*/ });
Then to get all the results from a key like:
List<string> testValues = dict["TEST"];
To make it safe however you should check that the key exists i.e.
if (dict.ContainsKey("TEST"))
{
//Get the values
}
Then to add values to a current key you go do something like:
dict["TEST"].Add("NewValue");
If you insist on keeping the same structure, although I do not recommend it, something like the following will work:
List<string> testKeys = new List<string>();
foreach (var pairs in dict)
{
if (pair.Value == "TEST")
{
testKeys.Add(pair.Key);
}
}
Or even the following LINQ statement:
List<string> testKeys = dict.Where(p => p.Value == "TEST").Select(p => p.Key).ToList();
For a generic query to find the ones from your list use:
List<string> values = dict.Where(p => list.Contains(p.Value)).ToList();
var a = new myTestClass();
var b = new myTestClass2();
list<string> instList = new list<string>();
instList.add("b");
public void simpleFunc()
{
foreach(string i in instList){
a.fieldName = **i.myFieldName;**
//HERE i is b which refers to the instance of myTestClass2
}
what I would like to accomplish here is: create a List of string whihc are the names of class instances then inside a for each loop use the instlist strings like an instance of the class and add something to that property of that class
is this possible?
If you really want to do that, you can create a dictionary with the name as key and the instance as value. I do wonder though why you are thinking the variable name is so important... A variable name can change easily and after all, it is just a pointer to the actual object. Shouldn't you create a class to save the 'metadata' of the variable?
Dictionary<string, string> d = new Dictionary<string, string>();
d.Add(nameof(a), a);
d.Add(nameof(b), b);
string valueAtB = d["b"];
I want to Achieve Something like this
var mainList = Model.ContentItems;
Dictionary<string, string> dictTerms = new Dictionary<string, string>();
foreach (var outeritems in mainList) {
string page = ".Page";
dynamic Terms = outeritems.ContentItem + page;
}
I am doing this because the object is coming from database and i don't know the object field.
So I will first loop through all the objects and then will do something like this to get the actual value of the field. I know it will convert it to string but is there anything I dont know yet where I can do this.
I have this dictionary and tuples set up in SetValue() as below :-
var myDict = new Dictionary<string, Tuple<string, string>>();
private void SetValue()
{
var myTuple1= Tuple.Create("ABC", "123");
var myTuple2= Tuple.Create("DEF", "456");
myDict.Add("One", myTuple1)
myDict.Add("Two", myTuple2)
}
I am trying to retrive the tuple in GetValue() as below :-
private void GetValue()
{
var myTuple = new Tuple<string, string>("",""); //Is this correct way to initialize tuple
if (myDict.TryGetValue(sdsId, out myTuple))
{
var x = myTuple.Item1;
var y = myTuple.Item2;
}
}
My question is whether this is the correct way to initialize tuple while retrieving the same from a dictionary ? Is there a better code?
var myTuple = new Tuple<string, string>("","");
If it's an out parameter, the object doesn't need to be initialized before being used. You should just be able to do:
Tuple<string,string> myTuple;
if (myDict.TryGetValue(sdsId, out myTuple))
{
var x = myTuple.Item1;
var y = myTuple.Item2;
}
You don't need to create an instance for an out parameter. Just declare the local variable as Tuple but don't assign a value.
Tuple<string, string> myTyple;
I have a dictionary with an array in it defined as:
Dictionary<string, string[]> wordDictionary = new Dictionary<string, string[]>();
is there a way in c# to access specific values in the dictionary without the foreach iteration.
Try this:
var t = wordDictionary ["myKey"][myArrIndex]
For example, this will give you the whole array:
var t = wordDictionary ["myKey"]
while this will give you the value in the array at position 5:
var t = wordDictionary ["myKey"][5]
If you know the key you can access it like this:
string[] str=wordDictionary["yourString"];
What about this?
string firstFoo = wordDictionary["foo"][0]