Write a program in Visual Basic.

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

atish_aa

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

IMM

Joined
Feb 1, 2002
Messages
3,257
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
 
Joined
Aug 17, 2001
Messages
7,486
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

Joined
Feb 1, 2002
Messages
3,257
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. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top