0

How would I go about doing this? I have tried

case (item.Contains("Phone")):

that doesn't work, and I have tried

case (item.Contains("Phone") == true):

I can just do this without the switch statement by just doing..

if (item.Contains("Phone"))

but that looks kinda of messy, and a switch statement looks cleaner.

Chris
  • 2,953
  • 10
  • 48
  • 118
  • What are you going to do with the result? If it's a simple variable assignment then use the ternary operator. – Michael McGriff Sep 12 '14 at 15:51
  • @MichaelMcGriff, sorry. I am creating a CSV file from a list and while I had it done, I realized that I now have to start picking off information and get rid of the data that I don't need and then write it back to a csv – Chris Sep 12 '14 at 15:52
  • 1
    Switches dont work the way you are trying to use them. You cant switch on expression. This is a common pattern however, with more information I'm sure there is a more elegant way to do it. – crthompson Sep 12 '14 at 15:52
  • 2
    Post all of your code which is messy, how many you have like this? what kind of operation you're performing if it is true? You'll get better answers. – Sriram Sakthivel Sep 12 '14 at 15:57
  • what you are trying to do would work if you were to use an if statement first and then do a switch based on the condition returning true.. I would do this personally using an If Conditional – MethodMan Sep 12 '14 at 16:00
  • @DJKRAZE, the if statements is easier to use there is no doubt about that, it just looks so rudimentary is all – Chris Sep 12 '14 at 16:01
  • `Chris` how about posting some relevant code that way others can give true / better guidance || recommendations do not post half `@$$` Code – MethodMan Sep 12 '14 at 16:03
  • @SriramSakthivel, there isn't much code to post, I have everything from the text file already in a list, it will look "messy" after the 3rd if statement, I was looking for a more elegant way – Chris Sep 12 '14 at 16:03
  • there has to be code show the method or function where you are trying to do the check this is what we are asking do not just post one line of code.. nobody here can see what you are seeing on your end unless you show us.. come one man...!! – MethodMan Sep 12 '14 at 16:04
  • Post what you have. If you have 3 post all of them. I can't understand what you're doing inside the if statement. If you can post it, you may get even better options than what switch does(which you believe is elegant). – Sriram Sakthivel Sep 12 '14 at 16:05
  • 1
    @waither's suggestion is the right one. Take it to codereview. – crthompson Sep 12 '14 at 16:05
  • @DJKRAZE the question was "how to...", and it was quickly said that I can't use a Switch statement for what I need to do. So now that I know then I am left to use if statements and bring it to codereview and see if it can be optimized. I completely understand what you are saying about posting more code, but then it would have went beyond the question – Chris Sep 12 '14 at 16:12
  • 1
    actually you can use a switch statement for what you want to do you would just need an if conditional check first then you could use a switch statement reference this previous `SO` posting for examples good luck http://stackoverflow.com/questions/7175580/use-string-contains-with-switch – MethodMan Sep 12 '14 at 16:15

4 Answers4

3

Switch can be only used if the variable can get only a set of fixed values. Contains method doesn't fit this, as well as you can't use any expression (higher, lower etc.).

If you need functionality like this, you have to use if, or if you know, that the search string will be on some position, you can first get the value and then use switch on it.

Switch doesn't work like if, it's only used similarly for beginners in tutorials to help them understand the concept, but these two language functions are not interchangeable. They're different performance-wise and most of the time you wouldn't be able to convert one to the other.

walther
  • 13,466
  • 5
  • 41
  • 67
  • The positions would be difficult because some of the information can be on 3 lines or 7 lines, so I need to check everything line by using Regex or using String.Contains() – Chris Sep 12 '14 at 15:58
  • @Chris, then I'm afraid `switch` isn't good for your situation. – walther Sep 12 '14 at 15:59
  • So I am pretty much stuck with using if statements, I was hoping for more elegance – Chris Sep 12 '14 at 16:00
  • @Chris, there is plenty of elegant possibilities. If you are making a CSV tho, how about just finding one of many CSV writers that are already available for C#? – crthompson Sep 12 '14 at 16:03
  • 2
    @Chris, the elegance of the code solely depends on the programmer writing it :) If you need optimizing your code, we have a nice stackexchange site for that as well: http://codereview.stackexchange.com/ – walther Sep 12 '14 at 16:04
2

The switch statement doesn't look much cleaner, if you consider the consequences. Take the following hypothetical code which would compile if c# worked as implied in the question, which it does not:

switch
{
    case (item.Contains("Phone")): return 1;
    case (item.Contains("Computer")): return 2;
    case (item.Contains("Car")): return 3;
}

Now, if item contains Phone, Computer and Car, what should be returned? Switch statements have to be simple multiple choice statements where only one answer can be true. That's why they only accept simple types, and not conditions. They work like this:

switch (item)
{
    case "Phone": return 1;
    case "Computer": return 2;
    case "Car": return 3;
}
Peter
  • 5,608
  • 1
  • 24
  • 43
  • 3
    I'm sure it doesn't work. That's the point. How would any languague know what to do with that code? – Peter Sep 12 '14 at 15:56
  • 2
    You really should indicate that this is an anti answer. I'm not sure it should even be posted tho. – crthompson Sep 12 '14 at 15:58
  • @paqogomez The question is "why don't languages work that way". I take the assumption that the language works that way and show why it doesn't make sense. I consider this to be the answer, not an anti answer. – Peter Sep 12 '14 at 16:00
  • @Peter There are langauges that *do* handle that code. C++, for example, supports it just fine, as does VB. Both would translate the code into a series of `if/else if` statements. – Servy Sep 12 '14 at 16:00
  • @Servy No. C++ absolutely doesn't work that way. I don't know about VB, as this would be an absolutely terrible idea, but given that we are taking about VB, I guess anything is possible. – Peter Sep 12 '14 at 16:03
  • 3
    I actually liked this answer in that it doesn't just say 'that won't work', but explained WHY this isn't supported. The only issue is people who just read the code blocks (and clearly missed your 'hypothetical' comment) thought you were giving an answer instead of an explanation. You might consider bolding 'hypothetical code' and adding a comment in the code block that this is for illustration purposes only and will not work. – Rufus L Sep 12 '14 at 16:11
  • I understood what Peter was getting at. – Chris Sep 12 '14 at 16:15
2

switch / case doesn't work in this way. All case labels should be constant which item.Contains("Phone") is not.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
0

If you are running on this using C# 8 you can use a switch expression to validate your results as follows


var item = "This is a Phone device"

var deviceDescription = item switch
{
    string a when a.Contains("Phone") => "Yes it is a Phone",
    string b when b.Contains("Computer") => "Yes, it is a Computer",
    string c when c.Contains("Tablet") => "Yes, it is a Tablet",
    _ => "Item doesn't match any criteria"
};

You can dive into the details here https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/switch-expression

Jesús Jiménez
  • 366
  • 3
  • 5