How can I use a variable that has been initialized in a when-expression?











up vote
0
down vote

favorite












I have to initialize copyToDay in a when-expression to get the specific day string, but how can I use it any further? I need the right string in a following function, but Android Studio says copyToDay hasn't been initialized.
Here's my code:



    val onCheckListener = View.OnClickListener {
it as CheckedTextView
it.isChecked = !it.isChecked

when(it){
copyHoursMonday -> copyToDay = R.string.MONDAY
copyHoursTuesday -> copyToDay = R.string.TUESDAY
copyHoursWednesday -> copyToDay = R.string.WEDNESDAY
copyHoursThursday -> copyToDay = R.string.THURSDAY
copyHoursFriday -> copyToDay = R.string.FRIDAY
copyHoursSaturday -> copyToDay = R.string.SATURDAY
copyHoursSunday -> copyToDay = R.string.SUNDAY
}
//Here's the error with copyToDay
saveCopy(initialStartInMinutes, initialEndInMinutes, isHappyHour, d, copyToDay)
}

d.copyHoursMonday.setOnClickListener(onCheckListener)
d.copyHoursTuesday.setOnClickListener(onCheckListener)
d.copyHoursWednesday.setOnClickListener(onCheckListener)
d.copyHoursThursday.setOnClickListener(onCheckListener)
d.copyHoursFriday.setOnClickListener(onCheckListener)
d.copyHoursSaturday.setOnClickListener(onCheckListener)
d.copyHoursSunday.setOnClickListener(onCheckListener)


Is there some way to get copyToDay from the when-expression?
Thanks for for the help!










share|improve this question




























    up vote
    0
    down vote

    favorite












    I have to initialize copyToDay in a when-expression to get the specific day string, but how can I use it any further? I need the right string in a following function, but Android Studio says copyToDay hasn't been initialized.
    Here's my code:



        val onCheckListener = View.OnClickListener {
    it as CheckedTextView
    it.isChecked = !it.isChecked

    when(it){
    copyHoursMonday -> copyToDay = R.string.MONDAY
    copyHoursTuesday -> copyToDay = R.string.TUESDAY
    copyHoursWednesday -> copyToDay = R.string.WEDNESDAY
    copyHoursThursday -> copyToDay = R.string.THURSDAY
    copyHoursFriday -> copyToDay = R.string.FRIDAY
    copyHoursSaturday -> copyToDay = R.string.SATURDAY
    copyHoursSunday -> copyToDay = R.string.SUNDAY
    }
    //Here's the error with copyToDay
    saveCopy(initialStartInMinutes, initialEndInMinutes, isHappyHour, d, copyToDay)
    }

    d.copyHoursMonday.setOnClickListener(onCheckListener)
    d.copyHoursTuesday.setOnClickListener(onCheckListener)
    d.copyHoursWednesday.setOnClickListener(onCheckListener)
    d.copyHoursThursday.setOnClickListener(onCheckListener)
    d.copyHoursFriday.setOnClickListener(onCheckListener)
    d.copyHoursSaturday.setOnClickListener(onCheckListener)
    d.copyHoursSunday.setOnClickListener(onCheckListener)


    Is there some way to get copyToDay from the when-expression?
    Thanks for for the help!










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have to initialize copyToDay in a when-expression to get the specific day string, but how can I use it any further? I need the right string in a following function, but Android Studio says copyToDay hasn't been initialized.
      Here's my code:



          val onCheckListener = View.OnClickListener {
      it as CheckedTextView
      it.isChecked = !it.isChecked

      when(it){
      copyHoursMonday -> copyToDay = R.string.MONDAY
      copyHoursTuesday -> copyToDay = R.string.TUESDAY
      copyHoursWednesday -> copyToDay = R.string.WEDNESDAY
      copyHoursThursday -> copyToDay = R.string.THURSDAY
      copyHoursFriday -> copyToDay = R.string.FRIDAY
      copyHoursSaturday -> copyToDay = R.string.SATURDAY
      copyHoursSunday -> copyToDay = R.string.SUNDAY
      }
      //Here's the error with copyToDay
      saveCopy(initialStartInMinutes, initialEndInMinutes, isHappyHour, d, copyToDay)
      }

      d.copyHoursMonday.setOnClickListener(onCheckListener)
      d.copyHoursTuesday.setOnClickListener(onCheckListener)
      d.copyHoursWednesday.setOnClickListener(onCheckListener)
      d.copyHoursThursday.setOnClickListener(onCheckListener)
      d.copyHoursFriday.setOnClickListener(onCheckListener)
      d.copyHoursSaturday.setOnClickListener(onCheckListener)
      d.copyHoursSunday.setOnClickListener(onCheckListener)


      Is there some way to get copyToDay from the when-expression?
      Thanks for for the help!










      share|improve this question















      I have to initialize copyToDay in a when-expression to get the specific day string, but how can I use it any further? I need the right string in a following function, but Android Studio says copyToDay hasn't been initialized.
      Here's my code:



          val onCheckListener = View.OnClickListener {
      it as CheckedTextView
      it.isChecked = !it.isChecked

      when(it){
      copyHoursMonday -> copyToDay = R.string.MONDAY
      copyHoursTuesday -> copyToDay = R.string.TUESDAY
      copyHoursWednesday -> copyToDay = R.string.WEDNESDAY
      copyHoursThursday -> copyToDay = R.string.THURSDAY
      copyHoursFriday -> copyToDay = R.string.FRIDAY
      copyHoursSaturday -> copyToDay = R.string.SATURDAY
      copyHoursSunday -> copyToDay = R.string.SUNDAY
      }
      //Here's the error with copyToDay
      saveCopy(initialStartInMinutes, initialEndInMinutes, isHappyHour, d, copyToDay)
      }

      d.copyHoursMonday.setOnClickListener(onCheckListener)
      d.copyHoursTuesday.setOnClickListener(onCheckListener)
      d.copyHoursWednesday.setOnClickListener(onCheckListener)
      d.copyHoursThursday.setOnClickListener(onCheckListener)
      d.copyHoursFriday.setOnClickListener(onCheckListener)
      d.copyHoursSaturday.setOnClickListener(onCheckListener)
      d.copyHoursSunday.setOnClickListener(onCheckListener)


      Is there some way to get copyToDay from the when-expression?
      Thanks for for the help!







      android kotlin






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 10:28









      Zoe

      10.2k73475




      10.2k73475










      asked Nov 8 at 10:08









      Franzi Notz

      62




      62
























          4 Answers
          4






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          You need an else branch in your when statement. That way the compiler knows that your variable has been initialised in every possible branch.
          For example:



                when(it){
          copyHoursMonday -> copyToDay = R.string.MONDAY
          copyHoursTuesday -> copyToDay = R.string.TUESDAY
          copyHoursWednesday -> copyToDay = R.string.WEDNESDAY
          copyHoursThursday -> copyToDay = R.string.THURSDAY
          copyHoursFriday -> copyToDay = R.string.FRIDAY
          copyHoursSaturday -> copyToDay = R.string.SATURDAY
          else -> copyToDay = R.string.SUNDAY
          }





          share|improve this answer




























            up vote
            4
            down vote













            It's a scope issue. The variable might not have been initialized. it is, in this case, the view you're using. Now, before I continue, I want to mention one thing: The compiler doesn't know whether or not you set that click listener for multiple views or not. As a result, the compiler thinks "If this is any other view, it will not be initialized".



            What you can do, is adding an else to the when:



            else -> return@OnClickListener;


            This means, in any other case, it returns. It doesn't execute the code that depends on the value, and it also won't be initialized.



            Alternatively, you can assign a different value in the else brach. Either is fine.



            Also, you can optimize your when statement. Since you assign the same value on every branch, you can simply do:



            val copyToDay = when(it){
            copyHoursMonday -> R.string.MONDAY
            copyHoursTuesday -> R.string.TUESDAY
            copyHoursWednesday -> R.string.WEDNESDAY
            copyHoursThursday -> R.string.THURSDAY
            copyHoursFriday -> R.string.FRIDAY
            copyHoursSaturday -> R.string.SATURDAY
            copyHoursSunday -> R.string.SUNDAY
            else -> return@OnClickListener
            }





            share|improve this answer






























              up vote
              3
              down vote













              Use when as an expression:



              val copyToDay = when(it){
              copyHoursMonday -> R.string.MONDAY
              copyHoursTuesday -> R.string.TUESDAY
              //...
              }





              share|improve this answer























              • copyHoursSunday is a View, not an enum. While this would work if the statement was exhaustive, there's still a chance the view passed is none of those. So there's still a tiny chance it's not initialized, which the compiler complains about
                – Zoe
                Nov 8 at 10:19




















              up vote
              0
              down vote













              The reason why it is saying its not initialised is because when your code run if it variable is true copyToDay variable will be initialised..but if it is false copyToDay will not be initialised.
              Hence you need to cater for the false scenario.The best is to give it a default value just above the when(it){}






              share|improve this answer





















                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                 

                draft saved


                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205480%2fhow-can-i-use-a-variable-that-has-been-initialized-in-a-when-expression%23new-answer', 'question_page');
                }
                );

                Post as a guest
































                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                2
                down vote



                accepted










                You need an else branch in your when statement. That way the compiler knows that your variable has been initialised in every possible branch.
                For example:



                      when(it){
                copyHoursMonday -> copyToDay = R.string.MONDAY
                copyHoursTuesday -> copyToDay = R.string.TUESDAY
                copyHoursWednesday -> copyToDay = R.string.WEDNESDAY
                copyHoursThursday -> copyToDay = R.string.THURSDAY
                copyHoursFriday -> copyToDay = R.string.FRIDAY
                copyHoursSaturday -> copyToDay = R.string.SATURDAY
                else -> copyToDay = R.string.SUNDAY
                }





                share|improve this answer

























                  up vote
                  2
                  down vote



                  accepted










                  You need an else branch in your when statement. That way the compiler knows that your variable has been initialised in every possible branch.
                  For example:



                        when(it){
                  copyHoursMonday -> copyToDay = R.string.MONDAY
                  copyHoursTuesday -> copyToDay = R.string.TUESDAY
                  copyHoursWednesday -> copyToDay = R.string.WEDNESDAY
                  copyHoursThursday -> copyToDay = R.string.THURSDAY
                  copyHoursFriday -> copyToDay = R.string.FRIDAY
                  copyHoursSaturday -> copyToDay = R.string.SATURDAY
                  else -> copyToDay = R.string.SUNDAY
                  }





                  share|improve this answer























                    up vote
                    2
                    down vote



                    accepted







                    up vote
                    2
                    down vote



                    accepted






                    You need an else branch in your when statement. That way the compiler knows that your variable has been initialised in every possible branch.
                    For example:



                          when(it){
                    copyHoursMonday -> copyToDay = R.string.MONDAY
                    copyHoursTuesday -> copyToDay = R.string.TUESDAY
                    copyHoursWednesday -> copyToDay = R.string.WEDNESDAY
                    copyHoursThursday -> copyToDay = R.string.THURSDAY
                    copyHoursFriday -> copyToDay = R.string.FRIDAY
                    copyHoursSaturday -> copyToDay = R.string.SATURDAY
                    else -> copyToDay = R.string.SUNDAY
                    }





                    share|improve this answer












                    You need an else branch in your when statement. That way the compiler knows that your variable has been initialised in every possible branch.
                    For example:



                          when(it){
                    copyHoursMonday -> copyToDay = R.string.MONDAY
                    copyHoursTuesday -> copyToDay = R.string.TUESDAY
                    copyHoursWednesday -> copyToDay = R.string.WEDNESDAY
                    copyHoursThursday -> copyToDay = R.string.THURSDAY
                    copyHoursFriday -> copyToDay = R.string.FRIDAY
                    copyHoursSaturday -> copyToDay = R.string.SATURDAY
                    else -> copyToDay = R.string.SUNDAY
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 8 at 10:14









                    nsndvd

                    362111




                    362111
























                        up vote
                        4
                        down vote













                        It's a scope issue. The variable might not have been initialized. it is, in this case, the view you're using. Now, before I continue, I want to mention one thing: The compiler doesn't know whether or not you set that click listener for multiple views or not. As a result, the compiler thinks "If this is any other view, it will not be initialized".



                        What you can do, is adding an else to the when:



                        else -> return@OnClickListener;


                        This means, in any other case, it returns. It doesn't execute the code that depends on the value, and it also won't be initialized.



                        Alternatively, you can assign a different value in the else brach. Either is fine.



                        Also, you can optimize your when statement. Since you assign the same value on every branch, you can simply do:



                        val copyToDay = when(it){
                        copyHoursMonday -> R.string.MONDAY
                        copyHoursTuesday -> R.string.TUESDAY
                        copyHoursWednesday -> R.string.WEDNESDAY
                        copyHoursThursday -> R.string.THURSDAY
                        copyHoursFriday -> R.string.FRIDAY
                        copyHoursSaturday -> R.string.SATURDAY
                        copyHoursSunday -> R.string.SUNDAY
                        else -> return@OnClickListener
                        }





                        share|improve this answer



























                          up vote
                          4
                          down vote













                          It's a scope issue. The variable might not have been initialized. it is, in this case, the view you're using. Now, before I continue, I want to mention one thing: The compiler doesn't know whether or not you set that click listener for multiple views or not. As a result, the compiler thinks "If this is any other view, it will not be initialized".



                          What you can do, is adding an else to the when:



                          else -> return@OnClickListener;


                          This means, in any other case, it returns. It doesn't execute the code that depends on the value, and it also won't be initialized.



                          Alternatively, you can assign a different value in the else brach. Either is fine.



                          Also, you can optimize your when statement. Since you assign the same value on every branch, you can simply do:



                          val copyToDay = when(it){
                          copyHoursMonday -> R.string.MONDAY
                          copyHoursTuesday -> R.string.TUESDAY
                          copyHoursWednesday -> R.string.WEDNESDAY
                          copyHoursThursday -> R.string.THURSDAY
                          copyHoursFriday -> R.string.FRIDAY
                          copyHoursSaturday -> R.string.SATURDAY
                          copyHoursSunday -> R.string.SUNDAY
                          else -> return@OnClickListener
                          }





                          share|improve this answer

























                            up vote
                            4
                            down vote










                            up vote
                            4
                            down vote









                            It's a scope issue. The variable might not have been initialized. it is, in this case, the view you're using. Now, before I continue, I want to mention one thing: The compiler doesn't know whether or not you set that click listener for multiple views or not. As a result, the compiler thinks "If this is any other view, it will not be initialized".



                            What you can do, is adding an else to the when:



                            else -> return@OnClickListener;


                            This means, in any other case, it returns. It doesn't execute the code that depends on the value, and it also won't be initialized.



                            Alternatively, you can assign a different value in the else brach. Either is fine.



                            Also, you can optimize your when statement. Since you assign the same value on every branch, you can simply do:



                            val copyToDay = when(it){
                            copyHoursMonday -> R.string.MONDAY
                            copyHoursTuesday -> R.string.TUESDAY
                            copyHoursWednesday -> R.string.WEDNESDAY
                            copyHoursThursday -> R.string.THURSDAY
                            copyHoursFriday -> R.string.FRIDAY
                            copyHoursSaturday -> R.string.SATURDAY
                            copyHoursSunday -> R.string.SUNDAY
                            else -> return@OnClickListener
                            }





                            share|improve this answer














                            It's a scope issue. The variable might not have been initialized. it is, in this case, the view you're using. Now, before I continue, I want to mention one thing: The compiler doesn't know whether or not you set that click listener for multiple views or not. As a result, the compiler thinks "If this is any other view, it will not be initialized".



                            What you can do, is adding an else to the when:



                            else -> return@OnClickListener;


                            This means, in any other case, it returns. It doesn't execute the code that depends on the value, and it also won't be initialized.



                            Alternatively, you can assign a different value in the else brach. Either is fine.



                            Also, you can optimize your when statement. Since you assign the same value on every branch, you can simply do:



                            val copyToDay = when(it){
                            copyHoursMonday -> R.string.MONDAY
                            copyHoursTuesday -> R.string.TUESDAY
                            copyHoursWednesday -> R.string.WEDNESDAY
                            copyHoursThursday -> R.string.THURSDAY
                            copyHoursFriday -> R.string.FRIDAY
                            copyHoursSaturday -> R.string.SATURDAY
                            copyHoursSunday -> R.string.SUNDAY
                            else -> return@OnClickListener
                            }






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 8 at 12:47

























                            answered Nov 8 at 10:16









                            Zoe

                            10.2k73475




                            10.2k73475






















                                up vote
                                3
                                down vote













                                Use when as an expression:



                                val copyToDay = when(it){
                                copyHoursMonday -> R.string.MONDAY
                                copyHoursTuesday -> R.string.TUESDAY
                                //...
                                }





                                share|improve this answer























                                • copyHoursSunday is a View, not an enum. While this would work if the statement was exhaustive, there's still a chance the view passed is none of those. So there's still a tiny chance it's not initialized, which the compiler complains about
                                  – Zoe
                                  Nov 8 at 10:19

















                                up vote
                                3
                                down vote













                                Use when as an expression:



                                val copyToDay = when(it){
                                copyHoursMonday -> R.string.MONDAY
                                copyHoursTuesday -> R.string.TUESDAY
                                //...
                                }





                                share|improve this answer























                                • copyHoursSunday is a View, not an enum. While this would work if the statement was exhaustive, there's still a chance the view passed is none of those. So there's still a tiny chance it's not initialized, which the compiler complains about
                                  – Zoe
                                  Nov 8 at 10:19















                                up vote
                                3
                                down vote










                                up vote
                                3
                                down vote









                                Use when as an expression:



                                val copyToDay = when(it){
                                copyHoursMonday -> R.string.MONDAY
                                copyHoursTuesday -> R.string.TUESDAY
                                //...
                                }





                                share|improve this answer














                                Use when as an expression:



                                val copyToDay = when(it){
                                copyHoursMonday -> R.string.MONDAY
                                copyHoursTuesday -> R.string.TUESDAY
                                //...
                                }






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Nov 8 at 10:29

























                                answered Nov 8 at 10:17









                                s1m0nw1

                                23.4k53696




                                23.4k53696












                                • copyHoursSunday is a View, not an enum. While this would work if the statement was exhaustive, there's still a chance the view passed is none of those. So there's still a tiny chance it's not initialized, which the compiler complains about
                                  – Zoe
                                  Nov 8 at 10:19




















                                • copyHoursSunday is a View, not an enum. While this would work if the statement was exhaustive, there's still a chance the view passed is none of those. So there's still a tiny chance it's not initialized, which the compiler complains about
                                  – Zoe
                                  Nov 8 at 10:19


















                                copyHoursSunday is a View, not an enum. While this would work if the statement was exhaustive, there's still a chance the view passed is none of those. So there's still a tiny chance it's not initialized, which the compiler complains about
                                – Zoe
                                Nov 8 at 10:19






                                copyHoursSunday is a View, not an enum. While this would work if the statement was exhaustive, there's still a chance the view passed is none of those. So there's still a tiny chance it's not initialized, which the compiler complains about
                                – Zoe
                                Nov 8 at 10:19












                                up vote
                                0
                                down vote













                                The reason why it is saying its not initialised is because when your code run if it variable is true copyToDay variable will be initialised..but if it is false copyToDay will not be initialised.
                                Hence you need to cater for the false scenario.The best is to give it a default value just above the when(it){}






                                share|improve this answer

























                                  up vote
                                  0
                                  down vote













                                  The reason why it is saying its not initialised is because when your code run if it variable is true copyToDay variable will be initialised..but if it is false copyToDay will not be initialised.
                                  Hence you need to cater for the false scenario.The best is to give it a default value just above the when(it){}






                                  share|improve this answer























                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    The reason why it is saying its not initialised is because when your code run if it variable is true copyToDay variable will be initialised..but if it is false copyToDay will not be initialised.
                                    Hence you need to cater for the false scenario.The best is to give it a default value just above the when(it){}






                                    share|improve this answer












                                    The reason why it is saying its not initialised is because when your code run if it variable is true copyToDay variable will be initialised..but if it is false copyToDay will not be initialised.
                                    Hence you need to cater for the false scenario.The best is to give it a default value just above the when(it){}







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 8 at 10:14









                                    malvern dongeni

                                    11




                                    11






























                                         

                                        draft saved


                                        draft discarded



















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205480%2fhow-can-i-use-a-variable-that-has-been-initialized-in-a-when-expression%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest




















































































                                        Popular posts from this blog

                                        Landwehr

                                        Reims

                                        Schenkenzell