Question Multivalued dictionary

jkeertir

Member
Joined
Apr 18, 2020
Messages
9
Programming Experience
1-3
Dear All,
I want to create a dictionary whose key is string and values are string and list of six variables.


How can I do this in c#?
 
Dear All,
I want to create a dictionary whose key is string and values are string and list of six variables.


How can I do this in c#?
Get your grouped values organized in a class structure like @Skydiver demonstrated in post 12. Keep it simple and object oriented.
 
I have created a class
C#:
class DictionaryData
{
    public string Groupnme {get;set;}
    public int TableID{get;set;}
    public List<OutputData> _EMS_OutputDatas = new List<OutputData>
}

class OutputData
{
        public string Dispname {get;set;}
        public string Lablename {get;set;}
        public object unit {get;set;}
}
public List<OutputData>calculate(string DI){
    List<OutputData> OutputData_obj = new List<OutputData>();
    List<string>ruledata = new List<string>();
    for(i=0;i<ruledata.count();i++)
    {
        OutputData obj =new OutputData();
        obj.DisplayName=ruledata[i].Displayname;
        obj.Labelname = ruledata[i].lablename;
        obj.unit=ruledata[i].k;
        OutputData_obj.Add(obj);
    }
    return OutputData_obj;
    
}
void main()
{
    Dictionary<string,DictionaryData> kvp = new Dictionary<string,DictionaryData>();
    DictionaryData mydict = new DictionaryData();
    List<string> mylist = new List<string>();
    for(i=0;i<mylist.count;i++)
    {
        mydict.Groupnme = GetGroupname();
        mydict.TableID= GetTableID();
        mydict._EMS_OutputDatas=calculate(mylist[i]);
        kvp.Add(mylist[i],mydict);
        
    }
}
Here I am getting dict after all element added ,as sum of all the list in OutputData.
suppose first element of kvp dict has 10 elements in the list,and second has 10
then in final kvp has 20 elements in each list element
 
Notice on line 38, that you are re-adding the same instance of mydict that you instantiated on line 31. C# (like Java) passes objects by reference, not by value (like C and C++).
What that effectively means is that all your dictionary entries in kvp are referring to the same object.

And again, please share with us your real code, not a sketch of your code.

I don't think that this will compile: mylist.count . Note that "count" with a lower case 'c' is not a field of a List<T>.

Nor will this generate a valid executable: void main(). Note that this should be static void Main().

I highly doubt that anything is actually getting added into kvp because mylist will be empty, and therefore the for loop on line 33 will never be entered.
 
Last edited:
Your calculate() returns a new list. Unfortunately, the way it is currently written in post #17, it won't compile. And even if it compiled, it will return an empty list because the for loop will never be entered.
 
You already are doing it with the code you presented. As I said, with the code you presented, the list will just happen to be empty, but it will be a new empty list each time.

Since you like being vague with your code and just present sketches, I will respond with vague pseudocode that involves a lot of hand waving:
C#:
list<T> CreateList()
    let l be a new instance of list<t>
    populate l with instances of t
    return l
 
Last edited:
Likely can't share the code because we now know that that the code is running on the highly vulnerable .NET Framework 4.0 in a highly insecure system that. Furthermore, we also know that the system is vulnerable to SQL injection attacks. By showing us real code bad actors maybe able to figure out exactly which company he/she works for. So it's going to be security by obscurity. *Sigh*
 

Latest posts

Back
Top Bottom