4

I am trying to create a prefix text value for a project. I am using switch case for that. When the user select the relevant radio button we should give the prefix value.

what should I give after the "switch ()"

The value coming from user selection is a boolean value. The output is string.

any help..

 public string officePostfix()
  {
   string postfix = null;
     switch (...)
        {
            case SheetMgrForm.Goldcoast = true:
                postfix = "QLD";
                break;
            case SheetMgrForm.Melbourne = true:
                postfix = "MEL";
                break;
            case SheetMgrForm.Sydney = true:
                postfix = "SYD";
                break;
            case SheetMgrForm.Brisbane = true:
                postfix = "BIS";
                break;
        }
        return postfix;
     }
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
user1445894
  • 47
  • 1
  • 1
  • 5
  • 10
    I think your design is incorrect. You should be using an "area" enum or something (something being an ID from the database, or short code that identifies the entry), rather than boolean fields for each area. – Adriaan Stander May 21 '13 at 05:22
  • further this sort of thing looks like it should be done using a dictionary... – Aron May 21 '13 at 05:28
  • Why don't you simply use if and else statements? – fhnaseer May 21 '13 at 05:31

5 Answers5

5

That's not how switch works. You can't put an arbitrary boolean expression in each case (assuming you mean == and not =, btw).

You need to use a set of if-else blocks, like:

if (SheetMgrForm.Goldcoast) {
    postfix = "QLD";
} else if (SheetMgrForm.Melbourne) {
    postfix = "MEL";
} else if ......

However, it looks like your design could use some rework. Having all those separate booleans is clumsy.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
  • 1
    Although this will work I cannot express my distain at seeing how many programmers insist on using a if-else-if-else-if-else-... construct where a perfectly good enumerable type would have done or a mapping would have been more appropriate. – cyborg May 21 '13 at 05:54
4

Another approach is to use an enumeration to define your areas, then map these to a resource file. This makes it more maintainable, e.g. localization or further extension in the future, and could be useful if the list is long, i.e. automate the creation of the enum and resources via T4, or you have to query a web service, or whatever...

For example, say you have an AreaPostfixes resource file and an Area enumeration.

public enum Area
{
     Goldcoast,
     Melbourne,
     // ...
}

public static class AreaExtensions
{  
    public static string Postfix(this Area area)
    {
        return AreaPostfixes.ResourceManager.GetString(area.ToString());
    }
}

// AreaPostfixes.resx
Name       Value
Goldcoast  QLD
Melbourne  MEL

// Usage
public string GetPostfix(Area area)
{
    return area.Postfix();
}

That removes any need for switches, etc., the only thing you need to ensure is that there is a 1:1 mapping for each enum and resource. I do this via unit tests but it's easy enough to place an Assert or throw an exception if GetString returns null in the Postfix extension method.

si618
  • 16,580
  • 12
  • 67
  • 84
3

You can do this with property patterns in C# 8:

string officePostfix(SheetMgrForm test) => 
    test switch
    {
        { GoldCoast: true } => "QLD",
        { Melbourne: true } => "VIC",
        { Sydney: true } => "NSW",
        { Brisbane: true } => "BIS",
        _ => string.Empty
    };

https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#property-patterns

Craig Curtis
  • 853
  • 10
  • 24
  • 2
    Note that it is not possible to use switch expressions as statements ~ they must be assigned or returned [see](https://stackoverflow.com/questions/62033131/c-sharp-8-switch-expression-for-void-methods) – WerWet Aug 23 '20 at 15:16
2

switch does not support this. The cases must be constant. The cases must also be unique.

You need to either turn your boolean properties into an enum and switch off of that or change your switch into a standard if statement

http://msdn.microsoft.com/en-us/library/06tc147t(v=vs.110).aspx

TyCobb
  • 8,909
  • 1
  • 33
  • 53
0

This would probably return your desired result, but it's sort of an ugly brute force method. The case part is expecting some kind of constant value. In this case, you would have "case true:" or "case false:" (though I'm not sure what you would do with this, in this instance).

public string officePostfix()
{
    string postfix = null;
    switch (SheetMgrForm.Goldcoast == true)
    {
        case true:
            postfix = "QLD";
            break;
    }
    switch(SheetMgrForm.Melbourne == true)
    { 
        case true:
            postfix = "MEL";
            break;
    } 
    switch (SheetMgrForm.Sydney == true)
    {
        case true:
            postfix = "SYD";
            break;
    }     
    switch(SheetMgrForm.Brisbane == true)
    {
        case true:
            postfix = "BIS";
            break;
    }
        return postfix;
}
Jake Hathaway
  • 146
  • 1
  • 5