Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AVividLight · Apr 07, 2012 at 11:15 PM · c#errorswitch

Switch statement error

Hey guys,

I just "discovered" the switch statement today, and have been trying to figure it out. For some reason, the example I made is not working, and I would like to know why. If anyone can see what's wrong, please post an answer! The code, as well as the error, are bellow.

 error CS0152: The label `case -2:' already occurs in this switch statement


         switch(DateTime.Today.Month)
     {
     
     case 1:
         Debug.Log ("case 1");
         season = 3;
         world.renderer.material = winterGround;
         break;
     case 2 - 4:
         Debug.Log ("case 2 - 4");
         season = 0;
         world.renderer.material = springGround;
         break;
     case 5 - 7:
         Debug.Log ("case 5 - 7");
         season = 1;
         world.renderer.material = summerGround;
         break;
     case 8 - 10:
         Debug.Log ("case 8 - 10");
         season = 2;
         world.renderer.material = autumnGround;
         break;
     case 11 - 12:
         Debug.Log ("case 11 - 12");
         season = 3;
         world.renderer.material = winterGround;
         break;
     default:
         Debug.Log ("There is an error!");
         break;
     }



Thanks for your help!

-Gibson

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Hagbard · Apr 08, 2012 at 12:18 AM

"case 2 - 4:" is read as "case 2 minus 4", which translates to -2. same for 5 - 7, etc. A case can only contain one value. But you can do the following:

 switch(DateTime.Today.Month)
 {

 case 1:
    Debug.Log ("case 1");
    season = 3;
    world.renderer.material = winterGround;
    break;
 case 2:
 case 3:
 case 4:
    Debug.Log ("case 2 - 4");
    season = 0;
    world.renderer.material = springGround;
    break;
 case 5:
 case 6:
 case 7:
 ...
 break;
 ...
 }
 

If you then call case 2 it will go through all the code until the next break statement. So case 2, case 3, and case 4 will all execute the same code.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AVividLight · Apr 08, 2012 at 02:24 AM 0
Share

Thanks for your answer, I really appreciate it!

avatar image
1

Answer by rutter · Apr 08, 2012 at 12:20 AM

Switches are very handy, but I think you might be misunderstanding one crucial detail: switches will only check for equality against individual constant values, not ranges or variables.

You set a number of cases using literal expressions:

 case 2 - 4:
 case 5 - 7:
 case 8 - 10:

I've never seen that done, before, but I believe the compiler is simplifying all of those expressions. And what do they have in common? All of the expressions I quoted will evaluate to the same number:

 case -2:
 case -2:
 case -2:
 case -2:

Recognize that number from your error message?

Within a switch, each case value must be unique.

You may have an easier time with other control structures (array indexing, binary searching, a simple series of if-else blocks, or so on), but that seems to be beyond the scope of your original question.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rutter · Apr 08, 2012 at 12:33 AM 1
Share

Hagbard's solution might also work, if you're not in C#. I forgot about that method. :3

avatar image AVividLight · Apr 08, 2012 at 02:23 AM 0
Share

Thanks for the answer, and for your other ideas! I would use an if else, but the only reason that I'm using a switch statement is to learn it.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Switch statement not working C# (answered) 1 Answer

Multiple Cars not working 1 Answer

Sync with visual studio 2010 error 1 Answer

Some problems after switch the game for iOS mode 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges