Original Post
Hello community,
I am having a minor issue in my code, I have replaced an Integer variable with the std::vector.size() in a comparison statement (please see below):
[source lang="cpp"]if(selectedOption_ > (optionsList_.size() - 1))
{
selectedOption_ = (optionsList_.size() - 1);
}
else if(selectedOption_ < firstSelectableOption_)
{
// Highlighted option does not trigger as going less than 0.
selectedOption_ = firstSelectableOption_;
}[/source]
Before, I compared SelectedOption to an Integer (numOptions) and this worked as I expected, if it went greater than said number then it would be reset to numOptions, else if it when less than 0(firstSelectableOption), it would be set to zero.
Once I replaced it with size(), if the SelectedOption goes less than zero, it triggers the 'greater-than' clause and sets the SelectedOption equal to whatever the size()-1 is. I have looked through the debugger and the numbers are coming back as expected: SelectedOption becomes -1, and size in this instance being 2. So what is going on here. The quick-fix solution I am using is this:
[source lang="cpp"]if(selectedOption_ < firstSelectableOption_)
{
// Highlighted option does not trigger as going less than 0.
selectedOption_ = firstSelectableOption_;
}
else if(selectedOption_ > (optionsList_.size() -1))
{
selectedOption_ = (optionsList_.size() - 1);
}[/source]
This ensures that the check for going less than zero occurs first, which solves the issue I am having, but does not answer the question it puts forward.
In short, is there something I am missing that would be firing the if-else in reverse order. Also, I have checked and am not setting size equal to something different elsewhere.
Thanks in advance for your time,
Regards,
Stitchs.
I am having a minor issue in my code, I have replaced an Integer variable with the std::vector.size() in a comparison statement (please see below):
[source lang="cpp"]if(selectedOption_ > (optionsList_.size() - 1))
{
selectedOption_ = (optionsList_.size() - 1);
}
else if(selectedOption_ < firstSelectableOption_)
{
// Highlighted option does not trigger as going less than 0.
selectedOption_ = firstSelectableOption_;
}[/source]
Before, I compared SelectedOption to an Integer (numOptions) and this worked as I expected, if it went greater than said number then it would be reset to numOptions, else if it when less than 0(firstSelectableOption), it would be set to zero.
Once I replaced it with size(), if the SelectedOption goes less than zero, it triggers the 'greater-than' clause and sets the SelectedOption equal to whatever the size()-1 is. I have looked through the debugger and the numbers are coming back as expected: SelectedOption becomes -1, and size in this instance being 2. So what is going on here. The quick-fix solution I am using is this:
[source lang="cpp"]if(selectedOption_ < firstSelectableOption_)
{
// Highlighted option does not trigger as going less than 0.
selectedOption_ = firstSelectableOption_;
}
else if(selectedOption_ > (optionsList_.size() -1))
{
selectedOption_ = (optionsList_.size() - 1);
}[/source]
This ensures that the check for going less than zero occurs first, which solves the issue I am having, but does not answer the question it puts forward.
In short, is there something I am missing that would be firing the if-else in reverse order. Also, I have checked and am not setting size equal to something different elsewhere.
Thanks in advance for your time,
Regards,
Stitchs.