mardi 31 mai 2016

return a value depending on condition

assuming i have the following extension method:

public static string sampleMethod(this int num) {
    return "Valid";
}

how can i terminate sampleMethod and show a messagebox if num > 25 ?

if i try the code below i receive a red underline on the sampleMethod and says not all code path returns a value

public static string sampleMethod(this int num) {
    if(num > 25) {
        MessageBox.Show("Integer must not exceed 25 !");
    } else {
        return "Valid String";
    }
}

and if i add throw new Exception("..."); under the MessageBox.Show, everything goes well but the application terminates.

how can i show the MessageBox and terminate the Method instead if the condition is not met ?

thank you

Aucun commentaire:

Enregistrer un commentaire