-3

Thank you in advance for your precious help. Here is my problem, I would like to compare the first two characters of MirePrincipale (which is a string) to the following character strings either "MS" or "VI" or "PF". I can only do it with only one string "MS" not the rest . Here is my code !!

if ( MirePrincipal.Substring(0, 2) == "MS" || "VI" || "PF")
    return this.MireSecondaire + " " + "(" + MirePrincipal + ")";
else
    return "Mire Principale is note a MS VI OR PF";
MindSwipe
  • 7,193
  • 24
  • 47

3 Answers3

4

You can, for example, create an array then check if it contains what you search for:

if(new[] { "MS", "VI", "PS" }.Contains(MirePrincipal.Substring(0, 2)))
SᴇM
  • 7,024
  • 3
  • 24
  • 41
1

That's not how || works. Using an if, you'll have to repeat MirePrincipal.Substring(0, 2) for each == test.

You can avoid that using switch instead:

switch (MirePrincipal.Substring(0, 2)) {
    case "MS":
    case "VI":
    case "PF":
        return this.MireSecondaire + " " + "(" + MirePrincipal + ")";
    default:
        return "Mire Principale is not a MS VI OR PF";
        // No 'e' here −−−−−−−−−−−−−−−^
}

Side note: I recommend being consistent with using this. If MirePrincipal is a property (not a local variable) like MireSecondaire is, either consistently use this. with them (my recommendation) or consistently don't use this. with them, don't mix and match. (If MirePrincipal is a local variable, normal naming guidelines would say it shouldn't start with a capital M, so it should be mirePrincipal.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

This is a common beginner mistake (which I also made).

Basically, your if statement reads correctly in natural english (or whatever language), but it is incorrect. See it reads as "if (the first two characters of MirePrincipal equal "MS" or "VI" or "PF") then...", but that's not correct. You need to tell the computer "if (the first two characters of MirePrincipal equal "MS" or the first two characters of MirePrincipal equal "VI" ..."

So you'd need to do it like this:

if (MirePrincipal.Substring(0, 2) == "MS" || MirePrincipal.Substring(0, 2) == "VI" ||
    MirePrincipal.Substring(0, 2) == "PF")
{
    return this.MireSecondaire + " " + "(" + MirePrincipal + ")";
}

Or, as SᴇM pointed out, you could use LINQ and do it his way, but for a new programmer (which you look like you are [no worries, we were all new at some point]) I'd recommend the verbose if statement way

MindSwipe
  • 7,193
  • 24
  • 47
  • 1
    OP could also store the result of `MirePrincipal.Substring(0, 2)` into another `string` (with a shorter name say `prefix`) that would make `prefix == "MS" || prefix == "VI" || prefix == "PF"` quite shorter and as - if not better - readable – Rafalon Jan 08 '20 at 15:17
  • @Rafalon you're absolutely correct. It will also improve performance a bit as it doesn't call `Substring(0, 2)` 3 times. I'll update my answer once I get back to a PC – MindSwipe Jan 13 '20 at 09:41