How would one create a series of objects which are named object(x) where x is a number? like something like
int x = 1;
Object object(x) = new Object();
Every answer will be appreciated!
I suggest using Dictionary<int, Object> which is close to the desired syntax:
int x = 1;
// If you insist on object as a name you have to use # prefix
// since object is a reserved word
Dictionary<int, Object> #object = new Dictionary<int, object>();
#object.Add(1, new object());
...
// If you want to access the istance use #object[x]
object myObject = #object[x];
Related
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 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];
I've a class like as below:
public class Source
{
...
...
public List<Dictionary<int, int>> blanks { get; set; }
}
I've created an object of this and a Dictionary for it. I filled 'dic' Dictionary. Then, I add this dic to the blanks list.
Source src = new Source();
Dictionary<int, int> dic = new Dictionary<int, int>();
dic.Add(30, 50);
dic.Add(40, 60);
src.blanks.Add(dic);
And I try to access these 'Key' and 'Value' elements. But, I can't.
int a = src.blanks[0].Key;
int b = src.blanks[0].Value;
What can I do to access these elements?
Thanks.
src.blanks[0] is a whole dictionary, not a single KeyValuePair<int,int>. That is why you cannot access a .Key or .Value on it - there are potentially many keys, and many values associated with them.
You can access all key-value pairs in a dictionary at position zero by enumerating them, like this:
foreach (var kvp in src.blanks[0]) {
int a = kvp.Key;
int b = kvp.Value;
Console.WriteLine("Key:{0} Value:{1}", a, b);
}
blanks[0] returns a Dictionary<int, int>, you need to specify key of your item.
src.blanks[0][key]
Or loop through your values:
foreach(var pair in src.blanks[0])
{
int currentKey = pair.Key;
int currentValue = pair.Value;
}
You are trying to access a dictionary in the list which has no Key property. The Keyvaluepairs in a dictionary have keys.
So assuming you want to look into the first dictionary in the list:
Dictionary<int, int> dict = s.blanks[0];
// lookup 30:
int value = dict[30]; // 40
to get the value you should first index the list and then index the dictionary
to get value
int b = (src.blanks[0])[0]
You want something like the following:
var listItem = src.blanks[0];
var dictionaryItem = listItem[0];
var a = dictionaryItem.Key;
var b = dictionaryItem.Value;
Nevertheless, i advice you to get rid of those "nested generics" List<Dictionary<..,..>. You won't be able to distinguish what kind of object you're dealing with if you use these nested structs.
Use other structs that better represent your business logic. For example, you could derive your own class from List
I have a problem in creating dynamic objects. Please find the below code,
List<object> membersList = new List<object>();
foreach(var members in activityMembers){
dynamic myObject = new System.Dynamic.ExpandoObject();
myObject.MemberNumber = members.MemberNumber;
myObject.MemberName = members.Name;
foreach (var activity in members.ActivityList)
{
myObject.[activity.ActivityName] = activity.Minutes;
}
membersList.Add(myObject);
}
there inside the second foreach loop, i need to generate the properties to all activities. for example if there are 4 activities in members.ActivityList, then 4 properties needs to be generated for object.
myObject.Activity1 = 10;
myObject.Activity2 = 20;
myObject.Activity3 = 30;
myObject.Activity4 = 40;
How can i do this? What i did wrong here?
Regards,
Karthik.
Remove the . when you are indexing the object i.e. change
myObject.[activity.ActivityName] = activityMinutes;
to
myObject[activity.ActivityName] = activity.Minutes;
Actually this won't solve your problem straight away, it will compile fine but when you attempt to run it will throw a RuntimeBinderException as you can't index into a ExpandoObject directly. You need to cast it as a dictionary before iterating (that's effectively what it is) e.g.
var dict = (IDictionary<string, object>)myObject;
...
dict[activity.ActivityName] = activity.Minutes;
I suspect you need to treat the ExpandoObject as a dictionary for that part:
IDictionary<string, object> dictionary = myObject;
foreach (var activity in members.ActivityList)
{
dictionary[activity.ActivityName] = activity.Minutes;
}
That's the way of assigning properties to an ExpandoObject when you don't know the property name at compile-time.
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;