# Write a program in Visual Basic.

Discussion in 'Software Development' started by atish_aa, Feb 1, 2005.

Not open for further replies.

Joined:
Apr 29, 2004
Messages:
261
Please write a program in VB to swap the values of 2 variables without using a third variable.

2. 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

3. 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```

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