Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
 Dictionary<string, int[,]> Matrices = new Dictionary<string, int[,]
//Get the values of dictionary in an array
 public object[] Tab_Mat(Dictionary<string, int[,]> a)
        {
            object[] ruleArray = new object[a.Count];
            int num = 0;
            foreach (KeyValuePair<string, int[,]> rule in a)
                ruleArray[num++] = rule.Value;
            return ruleArray;
        }
//Get the sum of the values of the dictionary
public int[,] sum_global_Mat(Dictionary<string, int[,]> config)
        {
            var a = Tab_Mat(config);
            var newArray = Array.ConvertAll(a, item => (int[,])item);
            int[,] m = new int[newArray[0].GetLength(0), newArray[0].GetLength(1)];
            for ( int i=0;i<a.Length;i++)
            {
                for (int h = 0; h < m.GetLength(0); h++)
                    for (int j = 0; j < m.GetLength(1); j++)
                        
                        m[h,j] =(int)a[i]+(int)a[i+1] ;
            }
            return m;
        }

>();

What I have tried:

Hello evry one i am a biginner in C#,
I tried to do the sum of values of a dictionary ,knowing that this values are Matrix,but it give me the following error :

'System.InvalidCastException'
Posted
Updated 8-Aug-18 23:57pm
v2
Comments
OriginalGriff 9-Aug-18 3:57am    
"it seems to not work" is probably the most useless problem report we get - and we get it a lot. It tells us nothing about what is happening, or when it happens.
So tell us what it is doing that you didn't expect, or not doing that you did.
Tell us what you did to get it to happen.
Tell us any error messages.
Use the "Improve question" widget to edit your question and provide better information.

1 solution

Quote:
'System.InvalidCastException'
Helps a bit, but knowing which line would make our job easier...

At a guess, it's this line:
m[h,j] =(int)a[i]+(int)a[i+1] ;
and it implies that a does not contain integers. So start by looking at what exactly Tab_Mat does return, then use the debugger to find out exactly what is in a[i] and a[i + 1] when the problem occurs.
We can't do that for you: we don't have any access to your code while it's running!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900