Another way would be to use a dictionary
var messageDictionary = new Dictionary<string, string>() {
{"hnummer", "Deze laptop staat al in de lijst"},
{"serienummmer", "Dit serienummer staat al in de lijst"}
}
then something like this (if you want several messages for each "successful" to contain a test, as your code shows).
foreach (var kvp in messageDictionary) {
if (ex.Message.Contains(kvp.Key))
Messagebox.Show(kvp.Value);
}
or if you want to test everything "contains", but get only one aggregated message
var res = string.Empty;
foreach (var kvp in messageDictionary) {
if (ex.Message.Contains(kvp.Key))
res +=kvp.Value;
}
MessageBox.Show(res);
or "don't do it" oneliner way
MessageBox.Show(string.Join("\n", messageDictionary.Select(m => ex.Message.Contains(m.Key) ? m.Value : string.Empty)));
source
share