 # Dictionary - How to return Dictionary value as a method or function. [Answered]RSS

## 2 replies

Last post Jul 19, 2017 05:32 AM by Billy Liu

• ### Dictionary - How to return Dictionary value as a method or function.

Hi,

How to return TempSub value as method or function.

See my full code.

double strReturnvalue = GetMarks(A,B,C,D,E,F,str); // Main and returns Tempsub value

public static double GetMarks(double A,double B,double C,double D,double E,double F,String str)

double retCondition = 0.0;
str = "Sub1 = A+B-C+D-A,Sub2=D+E,Sub3=F+G";
string[] words1 = str.Split('#');
Dictionary<string, double> TempSub = new Dictionary<string, double>();
for (int i = 0; i < words1.Length; i++)
{
double n = 0.0;
string[] words2 = words1[i].Split('='); // Again splitting the values for Sub1 = A+B // Second split

string[] words3 = words2.Split('+');
for (int j = 0; j < words3.Length; j++)
{
double m = 0.0;
double sub = 0.0;
string[] words4 = words3[j].Split('-');
for (int k = 0; k < words4.Length; k++)
{
switch (words4[k].Trim())
{
case "A":
sub = 10;
break;
case "B":
sub = 20;
break;
case "C":
sub = 30;
break;
case "D":
sub = 180;
break;
default:
sub = 0;
break;
}
if (k == 0)
{
m += sub;
}
else
{
m -= sub;
}
}
n += m;
}
TempSub.Add(words2, n); // So Sub1 will be the variables
}

retCondition = TempSub ??????? // Error
return retCondition;

}

How to return TempSub value to method or function..?

• ### Re: Dictionary - How to return Dictionary value as a method or function.

change the return type of your method to Dictionary<string,double>:

`public static Dictionary<string, double> GetMarks(double A,double B,double C,double D,double E,double F,String str)`

then change the strReturnValue and retCondition variable declarations to a dictionary as well:

`var strReturnvalue = GetMarks(A,B,C,D,E,F,str); // Main and returns Tempsub value`

and

`Dictionary<string, double> retCondition;`

The whole thing:

```var strReturnvalue = GetMarks(A, B, C, D, E, F, str); // Main and returns Tempsub value

public static Dictionary < string, double > GetMarks(double A, double B, double C, double D, double E, double F, String str) {
Dictionary < string, double > retCondition;
str = "Sub1 = A+B-C+D-A,Sub2=D+E,Sub3=F+G";
string[] words1 = str.Split('#');
Dictionary < string, double > TempSub = new Dictionary < string, double > ();
for (int i = 0; i < words1.Length; i++) {
double n = 0.0;
string[] words2 = words1[i].Split('='); // Again splitting the values for Sub1 = A+B // Second split

string[] words3 = words2.Split('+');
for (int j = 0; j < words3.Length; j++) {
double m = 0.0;
double sub = 0.0;
string[] words4 = words3[j].Split('-');
for (int k = 0; k < words4.Length; k++) {
switch (words4[k].Trim()) {
case "A":
sub = 10;
break;
case "B":
sub = 20;
break;
case "C":
sub = 30;
break;
case "D":
sub = 180;
break;
default:
sub = 0;
break;
}
if (k == 0) {
m += sub;
} else {
m -= sub;
}
}
n += m;
}
TempSub.Add(words2, n); // So Sub1 will be the variables
}

retCondition = TempSub // Error
return retCondition;
}```

-----------------
• ### Re: Dictionary - How to return Dictionary value as a method or function.

Jul 19, 2017 05:32 AM|Billy Liu|LINK

Hi gani7787,