toggle between 0 and 1

R

Robert Crandal

Is there a binary operator in Visual Basic
that will toggle a variable (initially set to 0)
between 0 and 1???

I know I could use a bunch of if-then statements
to toggle my variable between 0 and 1, but isnt
there a binary operator that can set my variable
to 0 if it equals 1, and set it to 1 if it equals 0???

thankx
 
R

Rick Rothstein

Assuming your variable's name is ZeroOne, executing this statement will
toggle it from 1 to 0 or 0 to 1 depending on its current value...

ZeroOne = 1 - ZeroOne
 
J

Jacob Skaria

You can use the NOT operator as shown below

Dim blnToggle As Boolean

'Reset
blnToggle = Not blnToggle
MsgBox -blnToggle

'Reset again
blnToggle = Not blnToggle
MsgBox -blnToggle
 
B

Bernd P

Hello Jacob,

Sorry, did not see your "-" directly after the MSGBOX ...

Regards,
Bernd
 
R

Rick Rothstein

Others have shown you other ways to toggle between 0 and 1 (although I kind
of think the subtraction method I proposed is the easiest to understand and
implement). And this method lends itself to generalizing as well. To toggle
a variable V between any two numbers N1 and N2, you would just use this line
of code...

V = N1 + N2 - V

Think about it... if V equals N1, then the above line returns N2 and if V
equals N2, then the above line returns N1... simple, right? This is exactly
what I used for my initial response to you where N1 equaled 0 and N2 equaled
1. Now, if one of your numbers is not zero, then there might be a little
extra work involved to implement this (same problem if you use If..Then
statements as well)... all numeric variables start off defaulted to zero, so
you can use the above line immediately when one of the numbers is zero; but
when both numbers are not zero, then you have to get your variable V
initialized to one of the numbers before you can start toggling them.
 
R

Robert Crandal

Hi Rick. I usually do my programming with C++ or Intel
assembly language, which I know have bit flipping operators.
I was just curious if VBA had something similar.

However, your solution is very interesting because it can
be generalized. However, if I wanted to toggle between
5 different values, it would involve a "little extra work"
of the if-then statements right?? In cases like that, would
it be better to use a "select-case" statement??

BTW, I always appreciate your expert advice. thank you!
 
B

Bernd P

Hello,

In general - for more than 2 values - I would load all different
values into an array:
Dim i As Long, n As Long
ReDim dA(0 to n-1) As Double
dA(0) = 1# 'First value
....
dA(n-1) = n 'n-th value

i = 0

Now toggle with
i = (i+1) mod n
and use dA(i)

Regards,
Bernd
 
R

Rick Rothstein

For all of the following discussion, let's say the 5 numbers to toggle
between in the listed order are these... 5, 123, 73, 10, 9 and the toggle
variable is named V (where V is assumed to be initialized to one of the
variables in the list).

1. If..Then blocking is not really that bad to implement...

If V = 5 Then
V = 123
ElseIf V = 123 Then
V = 73
ElseIf V = 73 Then
V = 10
ElseIf V = 10 Then
V = 9
ElseIf V = 9 Then
V = 5
End If

2. Select..Case blocking can be made more compact...

Select Case V
Case 5: V = 123
Case 123: V = 73
Case 73: V = 10
Case 10: V = 9
Case 9: V = 5
End Select

3. Bernd's array method is also a feasible way to go...

' These statements go in the module's (General)(Declarations) section
Option Base 0
Dim Arr As Variant
Static Index As Long

' These statements go in your toggle subroutine
Arr = Array(5, 123, 73, 10, 9)
Index = (Index + 1) Mod UBound(Arr)
V = Arr(Index)

4. A one-liner method involves setting up a string with all the numbers
expanded to the same number of digits as the longest number in the
toggle sequence (using leading zeroes) with any non-digit character
between them (if that number has a decimal point in it, consider it
as a digit). So, for our example, this is how to set it up so the
toggle subroutine is a one-liner...

' Declare V as a Long or Double depending on what the actual values
' are and put it in the module's (General)(Declarations) section
Dim V As Long

' Initialize a toggle sequence constant named TS for this example
' in the code module's (General)(Declarations) section; note that
' the first number is repeated at the end.
Const TS As String = "005,123,073,010,009,005"

' This one-liner statement goes in your toggle subroutine
V = Mid(TS, InStr(TS, Format(V, "000")) + 4, 3)

The value being added (4 in this case) is one greater than the
number of digits in the largest toggle sequence value (123 has
3 digits, so we add 4... the extra 1 is so we will skip over the
non-digit delimiter (a comma in my example), the 3 (number of
characters returned from the Mid function call) is equal to the
number of digits in the largest toggle sequence value.

--
Rick (MVP - Excel)



Robert Crandal said:
Hi Rick. I usually do my programming with C++ or Intel
assembly language, which I know have bit flipping operators.
I was just curious if VBA had something similar.

However, your solution is very interesting because it can
be generalized. However, if I wanted to toggle between
5 different values, it would involve a "little extra work"
of the if-then statements right?? In cases like that, would
it be better to use a "select-case" statement??

BTW, I always appreciate your expert advice. thank you!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top