c# - Finding the maximum value of an array -


i want find maximum result of string "differenceasstring".
need convert patientinfo[itemcountinteger].differenceasstring int in order code work, put statement

convert.toint32.patientinfo[itemcountinteger].differenceasstring 

i error :

system.convert.toint32(string, int)' 'method', not valid in given context

private void button3_click(object sender, eventargs e) {     int maximun = findmax();     messagebox.show(" maximun is" + convert.tostring(maximun)); }  private int findmax() {      = convert.toint32.patientinfo[itemcountinteger].differenceasstring;     int max = a[0];     (int = 1; < a.length; i++)     {         if (a[i] > max) max = a[i];     }     return max; }  public class patient {     public string patientidstring;     public string firstnamestring;     public string lastnamestring;     public string datestring;     public string differenceasstring; }  patient[] patientinfo = new patient[10];  private void button1_click(object sender, eventargs e) {     try     {         foreach (patient patientinfoindex in patientinfo)          patientinfo[itemcountinteger].patientidstring = textbox1.text;         patientinfo[itemcountinteger].firstnamestring = textbox2.text;         patientinfo[itemcountinteger].lastnamestring = textbox3.text;         patientinfo[itemcountinteger].datestring = datetimepicker1.text;         patientinfo[itemcountinteger].differenceasstring= difference.days.tostring();          string names = patientinfo[itemcountinteger].patientidstring + "  " + patientinfo[itemcountinteger].firstnamestring + " " + patientinfo[itemcountinteger].lastnamestring;         listbox1.items.add(names);         itemcountinteger++;         listbox1.selecteditem = names;     }     catch     {         messagebox.show("contacts limited 20. please delete contacts prior adding more.");     } } 

try instead: assuming have array called "patientinfo"

private patient findmaxpatient() {     int max = 0;     patient maxpatient = patientinfo[0];     (int itemcountinteger = 0; itemcountinteger < patientinfo.length; itemcountinteger++)     {            int = 0;        patient patientdata = patientinfo[itemcountinteger];        if (int.tryparse(patientdata.differenceasstring, out a))        {           if (a > max)            {              max = a;              maxpatient = patientdata ;           }        }     }     return maxpatient; } 

then, print function becomes:

patient maximumpatient = findmaxpatient(); messagebox.show(string.format("maximum difference is: {0} patient {1} {2}", maximumpatient.differenceasstring, maximumpatient.firstnamestring, maximumpatient.lastnamestring ); 

first, when converting string int should use "parse" instead of "convert.toint32". technically should use tryparse in case have bad input, if sure numbers code above ok. need iterating on source array, not "a" array (which doesn't make sense since "int" not array).

update:

changed code use "tryparse" in case have null/non integer strings. solve exception if strings no good, still want figure out why had null input in first place. if array null, need post more code showing array created/assigned.

update 2:

modified function return patient object , added sample print function.

please let me know if can clarify anything!


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -