switch statement - C# Parameter not returning new value -


i have method takes 2 string parameters , allows second changed if necessary. utilizing switch case, want change value of 1 of variables passed new value ("invalid.format@email.com").

                    string email ="jibberishtext";                     string url = ...                       validatedata("email", email);                     if (!email.contains("invalid"))                         senddata(objacctqryout, "net");  

...

    private static string validatedata(string mode, string field)         {    ...             electronicaddressaddupdateresponse er = myclient.electronicaddressaddupdate(upd, true, appid, pass);  switch (mode)             {                 case "email":                      if (er.result.equals("success"))                         return field;                     else                         field = "invalid.format@email.com";                      return field;                  case "url":                  ...                 case "phone":                  ...                  case "fax":                  ...              }             return field;         } 

my question is-- in validatedata method, second email variable goes being populated string contained in email "jibberishtext," instead of reflecting new returned string in variable field. can explain why happening? thanks.

just change line:

validatedata("email", email); 

to:

email = validatedata("email", email); 

you returning modified version of email string, needs re-assigning email.

if want improve upon this, i'd assign value new variable instead, record fact it's validated:

var validatedemail = validatedata("email", email); 

finally, validatedata method not one. takes mode parameter and, depending on value of parameter, performs series of largely unrelated functions. rule of thumb: method should 1 thing. if have if or switch on parameter, split method individual ones handle each case.


Comments