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!
add a comment |
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!
add a comment |
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!
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!
edited Nov 8 at 10:28
Zoe
10.2k73475
10.2k73475
asked Nov 8 at 10:08
Franzi Notz
62
62
add a comment |
add a comment |
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
}
add a comment |
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
}
add a comment |
up vote
3
down vote
Use when as an expression:
val copyToDay = when(it){
copyHoursMonday -> R.string.MONDAY
copyHoursTuesday -> R.string.TUESDAY
//...
}
copyHoursSundayis 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
add a comment |
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){}
add a comment |
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
}
add a comment |
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
}
add a comment |
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
}
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
}
answered Nov 8 at 10:14
nsndvd
362111
362111
add a comment |
add a comment |
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
}
add a comment |
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
}
add a comment |
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
}
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
}
edited Nov 8 at 12:47
answered Nov 8 at 10:16
Zoe
10.2k73475
10.2k73475
add a comment |
add a comment |
up vote
3
down vote
Use when as an expression:
val copyToDay = when(it){
copyHoursMonday -> R.string.MONDAY
copyHoursTuesday -> R.string.TUESDAY
//...
}
copyHoursSundayis 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
add a comment |
up vote
3
down vote
Use when as an expression:
val copyToDay = when(it){
copyHoursMonday -> R.string.MONDAY
copyHoursTuesday -> R.string.TUESDAY
//...
}
copyHoursSundayis 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
add a comment |
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
//...
}
Use when as an expression:
val copyToDay = when(it){
copyHoursMonday -> R.string.MONDAY
copyHoursTuesday -> R.string.TUESDAY
//...
}
edited Nov 8 at 10:29
answered Nov 8 at 10:17
s1m0nw1
23.4k53696
23.4k53696
copyHoursSundayis 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
add a comment |
copyHoursSundayis 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
add a comment |
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){}
add a comment |
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){}
add a comment |
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){}
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){}
answered Nov 8 at 10:14
malvern dongeni
11
11
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password