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 abalta · Oct 25, 2014 at 08:31 PM · javascriptswitchwarningcase

js switch case warning

I wrote a simple code below but I get a warning that is "assets/Scripts/Global.js(5,17): BCW0023: WARNING: This method could return default value implicitly."

 #pragma strict
 
 public static class Global{ 
 
 public function checkValueOfTag(str_tag : String) : int{
     switch (str_tag)
     {
     case "num0":
         return 0;
     case "num1":
         return 1;
     case "num2":
         return 2;
     case "num3":
         return 3;
     case "num4":
         return 4;
     case "num5":
         return 5;
     case "num6":
         return 6;
     case "num7":
         return 7;
     case "num8":
         return 8;
     case "num9":
         return 9;
     default:
         return 0;
     }    
 }
 
 }

Comment
Add comment · Show 2
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 MrSoad · Oct 25, 2014 at 09:04 PM 0
Share

Have you tried this with the :

  default:
      return 0;

commented out to see if this warning disappears?

avatar image abalta · Oct 25, 2014 at 10:42 PM 0
Share

I found a solution. I changed to function this. It should work.

 public function checkValueOfTag(str_tag : String) : int{
 
         var result : int = 0;
     
         switch (str_tag)
         {
             case "num0":
                 result = 0;
                 break;
             case "num1":
                 result = 0;
                 break;
             case "num2":
                 result = 0;
                 break;
             case "num3":
                 result = 0;
                 break;
             case "num4":
                 result = 4;
                 break;
             case "num5":
                 result = 5;
                 break;
             case "num6":
                 result = 6;
                 break;
             case "num7":
                 result = 7;
                 break;
             case "num8":
                    result = 8;
                 break;
             case "num9":
                 result = 9;
                 break;
             default:
                 result = 0;
                 break;
         }
         return result;    
     }

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Kiwasi · Oct 25, 2014 at 10:56 PM

To explain the warning. The compiler is not sure if every path in your code returns a value. The warning is saying that there is a possibility that the function won't return a value, and the default will be used instead. The default for int is 0.

The typical way to deal with this is to add a return at the very end of your function. Note that this is a warning, not an error. You can ignore it if you choose. Your code in the comments does make the warning go away, but it's not the most efficient way to do so. Local variables will need to be allocated.

 function DoStuff () : int {
     // Stuff
     return 0;
 }
Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Switch case using enum javascript 2 Answers

Light switch trigger 3 Answers

Syntax of switch + case insensitive? 2 Answers

Play anim, javascript 1 Answer

Setting Scroll View Width GUILayout 1 Answer


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