❔ Select next/previous item from a list when function called
So I have a list of colors, I want to check if the current color is equal to an array item, find the position of that item in the array, then set it to the next item in the array.
Basically a rainbow loop, so it returns red on first run, green on second run, blue on third run, red on fourth run
newColor
is not yet defined but will be defined as the resulting next color in the array7 Replies
I just can't really figure out how to go about it, don't know much C#, and I should probably have gone to sleep many hours ago
you can do
% is the modulo operator
what do u mean by
check if current color is equal to an array item
I'm not even quite sure myself tbh (needa sleep)
But works as intended
Thanks!
u can use
colors.FindIndex(c => c == current);
to get the index of the current color in the list
Hi @RamuneNeptune I'd just like to propose an alternate approach:
This code creates an infinite loop of your chosen colors and then just gets the next one every time you ask. It should be more efficient as you don't have to redeclare the list of colours every time you call the function, nor do you have to find where you were each time. It would also let you repeat colors if you wished, which your existing solution cannot.
On the other hand if you think that's too weird a solution try this on for size
This is also efficient and easy to maintain
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.