locked
Dictionary - How to return Dictionary value as a method or function. RRS feed

  • Question

  • User-1024101449 posted

    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[1].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[0], n); // So Sub1 will be the variables
    }

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


    }

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

    Tuesday, July 18, 2017 12:22 PM

Answers

  • User1068175894 posted

    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[1].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[0], n); // So Sub1 will be the variables
    	}
    	
    	retCondition = TempSub // Error
    	return retCondition;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 18, 2017 1:31 PM

All replies

  • User1068175894 posted

    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[1].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[0], n); // So Sub1 will be the variables
    	}
    	
    	retCondition = TempSub // Error
    	return retCondition;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 18, 2017 1:31 PM
  • User-335504541 posted

    Hi gani7787,

    You could refer to my answer in your previous post:

    https://forums.asp.net/p/2125211/6153447.aspx

    Best Regards,

    Billy

    Wednesday, July 19, 2017 5:32 AM