# Write a program in Visual Basic.

Status
This thread has been Locked and is not open to further replies. The original thread starter may use the Report button to request it be reopened but anyone else with a similar issue should start a New Thread. Watch our Welcome Guide to learn how to use this site.

#### atish_aa

Please write a program in VB to swap the values of 2 variables without using a third variable.

#### IMM

I won't write any vb for you but this kind of algorithm works
a = a xor b
b = a xor b
a = a xor b

In c:
a ^= b ^= a ^= b;
-------------
There are 10 kinds of people -- those who read binary and those who don't

#### Mosaic1

Using IMM's example, it works great in VB.
Code:
``````Private Sub Command2_Click()
Dim a As Integer, b As Integer
a = 99
b = 33
a = a Xor b
b = a Xor b
a = a Xor b

MsgBox a & " " & b

End Sub``````
What the heck. This is easier than Posting in Security! LOL I hope this isn't your homework. Here's another simple logic.

Code:
``````Private Sub Command1_Click()
Dim a As Integer, b As Integer
a = 99
b = 14

b = a + b  'b =  99 + 14 = 113
a = b - a  'a   113 - 99 = 14
b = b - a  'b = 113 - 14 = 99

MsgBox "A = " & a
MsgBox "B = " & b

End Sub``````

#### IMM

That xor thing is probably as old as I am -- and I'm older than Fortran

Status
This thread has been Locked and is not open to further replies. The original thread starter may use the Report button to request it be reopened but anyone else with a similar issue should start a New Thread. Watch our Welcome Guide to learn how to use this site.

As Seen On