1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Write a program in Visual Basic.

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

Thread Status:
Not open for further replies.
Advertisement
  1. atish_aa

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

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

    Mosaic1

    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
     
  4. IMM

    IMM

    Joined:
    Feb 1, 2002
    Messages:
    3,257
    That xor thing is probably as old as I am -- and I'm older than Fortran :)
     
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 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/325535

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice