Why Microsoft, why'd ya do it? 
Author Message
 Why Microsoft, why'd ya do it?

VB for Dos has a neat command called 'swap'. As
some of you might guess it will swap the contents
of two variables with each other.  WHY didn't MS
bother to put this command in VB-Windoze?  This
saves a little space for sorts and those types of
things when you don't need to put a buffer or temp
variable in.

Is there a VB-win equivalent?

Chris
"Don't eat anything bigger than your head."



Fri, 08 Aug 1997 12:37:00 GMT  
 Why Microsoft, why'd ya do it?

Quote:

>VB for Dos has a neat command called 'swap'.
>Is there a VB-win equivalent?
>Chris

How bout...

Sub MySwap( Var1 As Variant, Var2 As Variant)
Dim Temp As Variant

   Temp = Var1
   Var1 = Var2
   Var2 = Temp

End Sub

       _---_   /
    YY()))))\ /| John Tarr                |    Just an ArmaDELLo


      mm  mm



Sat, 09 Aug 1997 03:37:13 GMT  
 Why Microsoft, why'd ya do it?

|> VB for Dos has a neat command called 'swap'. As
|> some of you might guess it will swap the contents
|> of two variables with each other.  WHY didn't MS
|> bother to put this command in VB-Windoze?  This
|> saves a little space for sorts and those types of
|> things when you don't need to put a buffer or temp
|> variable in.
|>
|> Is there a VB-win equivalent?

I noticed the same thing and no, there doesn't seem to be
a VB/WIN equivalent.

It's not that big a deal as a home grown SUB to do the
same thing consists of ~5 lines of code:

SUB Swap(This as variant, That as variant)
  DIM temp as variant
  let temp = This
  let This = That
  let That = temp
End Sub

OK, so it was 6 . . .

Note that if you are only going to be using Swap for a specific
data type, you might want to change all of the 'variants' to the
data type that you will be using. Thus saving yourself some memory.

--

Tom Hoag                                To be or not to be,


My opinions are my own, not my employers.



Sat, 09 Aug 1997 03:43:35 GMT  
 Why Microsoft, why'd ya do it?
This is a hack, but....

var1 = var1 XOR var2
var2 = var1 XOR var2
var1 = var1 XOR var2

Does the swap in the same amount of lines as using a temp variable but
using only two variables.

Mark Ventimiglia



Sun, 10 Aug 1997 01:18:18 GMT  
 Why Microsoft, why'd ya do it?

: >
: >VB for Dos has a neat command called 'swap'.
: >Is there a VB-win equivalent?
: >Chris

: How bout...

: Sub MySwap( Var1 As Variant, Var2 As Variant)
: Dim Temp As Variant

:    Temp = Var1
:    Var1 = Var2
:    Var2 = Temp

: End Sub

You also do this without using a temporary variable. It's muchless readable,
but much more fun. This is only for integers becau it clearly won't work
for strings. But I am told that it will work for strings in C.
I don't know about that.

Sub MySwap( Var1 As integer, Var2 as integer)

        var1 = var1 + var2
        var2 = var1 - var2
        var1 = var1 - var2

 End Sub

Shit code, but a nice party piece :-)

--
 Richard Dalton.

 I wish I could stand on a busy corner,
 and bid passers by to toss me their
 wasted hours.



Sat, 09 Aug 1997 18:28:30 GMT  
 Why Microsoft, why'd ya do it?

Quote:


>|> VB for Dos has a neat command called 'swap'. As
>|> some of you might guess it will swap the contents
>|> of two variables with each other.  WHY didn't MS
>|> bother to put this command in VB-Windoze?  This
>|> saves a little space for sorts and those types of
>|> things when you don't need to put a buffer or temp
>|> variable in.
>|>
>|> Is there a VB-win equivalent?
>I noticed the same thing and no, there doesn't seem to be
>a VB/WIN equivalent.
>It's not that big a deal as a home grown SUB to do the
>same thing consists of ~5 lines of code:
>SUB Swap(This as variant, That as variant)
>  DIM temp as variant
>  let temp = This
>  let This = That
>  let That = temp
>End Sub

Instead of writing a separate function, why not just do it inline?
You do NOT need a temporary variable to do a swap.  Simply XOR three times!

Example:  Say you wanna swap X and Y:
X = X XOR Y
Y = X XOR Y
X = X XOR Y

And that's it!



Sun, 10 Aug 1997 14:49:14 GMT  
 Why Microsoft, why'd ya do it?

Quote:

>VB for Dos has a neat command called 'swap'. As
>some of you might guess it will swap the contents
>of two variables with each other.  WHY didn't MS
>bother to put this command in VB-Windoze?

There is a technique for swapping two variables WITHOUT the use of a third
one that is not widely known.  I give it to 'C' classes that I teach as a
"baffler" type question.  Here goes...

assuming A and B are your two integer type variables

 A = A Xor B
 B = A Xor B
 A = A Xor B

will swap the contents of the two variables.  Amazing, isn't it?

Regards,
Phillip Thomas



Mon, 11 Aug 1997 05:35:38 GMT  
 Why Microsoft, why'd ya do it?

Quote:

>There is a technique for swapping two variables WITHOUT the use of a third
>one that is not widely known.  I give it to 'C' classes that I teach as a
>"baffler" type question.  Here goes...
>assuming A and B are your two integer type variables
> A = A Xor B
> B = A Xor B
> A = A Xor B

Of course, this will also work (IN C/C++ ONLY) for any two variables of the
same type. So you can swap two strings with the same code.

Even better, it can all be compressed into a single line of code:
        A ^= B ^= A ^= B;

:-)

Dave.

--
David Rawling
Computing Systems Administrator
School of Economic and Financial Studies
Macquarie University, NSW 2109, Australia.


Phone :   +61-2-850-8477
FAX   :   +61-2-850-8586



Fri, 15 Aug 1997 23:24:41 GMT  
 Why Microsoft, why'd ya do it?

Quote:
>Even better, it can all be compressed into a single line of code:
>    A ^= B ^= A ^= B;

     Yeah. Too bad you would need an additional four lines of documentation
wiht this method......

Pennsylvania: Those aren't potholes, they are speed bumps.
QWicKeSST - The ultimate database QWK reader, and NO limits.

Quote:
>> SlipStream Jet v1.006A (Alpha) - (C) 1995 PBE



Sat, 16 Aug 1997 12:04:06 GMT  
 Why Microsoft, why'd ya do it?

Quote:
>>Even better, it can all be compressed into a single line of code:
>>        A ^= B ^= A ^= B;
>     Yeah. Too bad you would need an additional four lines of documentation
>wiht this method......

Not really, Chad.  The above technique is quite well known among the C
community.  Perhaps you haven't see it before?


Sun, 17 Aug 1997 17:46:25 GMT  
 Why Microsoft, why'd ya do it?

Quote:


>>>Even better, it can all be compressed into a single line of code:
>>>    A ^= B ^= A ^= B;
>>     Yeah. Too bad you would need an additional four lines of documentation
>>wiht this method......
>Not really, Chad.  The above technique is quite well known among the C
>community.  Perhaps you haven't see it before?

The only place I would expect to see this kind of code is in one of the
annual obfuscated code competitions. If you're really *that* concerned
with efficiency you should never have forsaken assembly language.

Just *maybe*, if you put this code into a function called SwapIntegers or
somesuch, then documented it, it *might* be kosher. OTOH, that'd be less
efficient than inline code, because it uses memory for the stack. Oh just
let me at them registers and leave memory for the applications types!

--
Luke Webber

* Note: The opinions expressed by Luke Webber are in no way supported *
*       by his employers, Luke Webber Consulting Services             *



Mon, 18 Aug 1997 14:20:01 GMT  
 Why Microsoft, why'd ya do it?

Quote:

>The only place I would expect to see this kind of code is in one of the
>annual obfuscated code competitions. If you're really *that* concerned
>with efficiency you should never have forsaken assembly language.
>Just *maybe*, if you put this code into a function called SwapIntegers or
>somesuch, then documented it, it *might* be kosher. OTOH, that'd be less
>efficient than inline code, because it uses memory for the stack. Oh just
>let me at them registers and leave memory for the applications types!

The point here, Luke, is that this will not only swap integers, but also two
characters, or two single precision floating points, or two double precision
... (well, you get the idea). The two items to be swapped just have to be the
same size. And that is quite useful.

Dave.
--
David Rawling
Computing Systems Administrator
School of Economic and Financial Studies
Macquarie University, NSW 2109, Australia.


Phone :   +61-2-850-8477
FAX   :   +61-2-850-8586



Tue, 19 Aug 1997 04:40:52 GMT  
 Why Microsoft, why'd ya do it?

Quote:


> >The only place I would expect to see this kind of code is in one of the
> >annual obfuscated code competitions. If you're really *that* concerned
> >with efficiency you should never have forsaken assembly language.

> >Just *maybe*, if you put this code into a function called SwapIntegers or
> >somesuch, then documented it, it *might* be kosher. OTOH, that'd be less
> >efficient than inline code, because it uses memory for the stack. Oh just
> >let me at them registers and leave memory for the applications types!

> The point here, Luke, is that this will not only swap integers, but also
> two characters, or two single precision floating points, or two double
> precision .... (well, you get the idea). The two items to be swapped just
> have to be the  same size. And that is quite useful.

Swap floating points variables with A ^= B ^= A ^= B; you sure?
What language? I don't think 'C' will like it.

---------------------------------------------------------------------
Roger Barker       (Escapee from the 8080 programmers' rest home)
Boston, UK



Wed, 20 Aug 1997 05:57:33 GMT  
 Why Microsoft, why'd ya do it?

: The point here, Luke, is that this will not only swap integers, but also two
: characters, or two single precision floating points, or two double precision
: ... (well, you get the idea). The two items to be swapped just have to be the
: same size. And that is quite useful.

Two structures!  Two pointers!  How cool.

Jerry
: Dave.
: --
: David Rawling
: Computing Systems Administrator
: School of Economic and Financial Studies
: Macquarie University, NSW 2109, Australia.


: Phone :   +61-2-850-8477
: FAX   :   +61-2-850-8586



Tue, 19 Aug 1997 02:54:43 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. why why why why

2. Why why why why?

3. Why oh why doesn't this work?

4. Why Why Why

5. ActiveX wont deploy to Win 2K - why, Why WHY

6. VB6 ListView: Why oh why oh why...

7. Why, Why, Why!!!!!!!!!!!

8. !Excel/VB: why why why?

9. Why,Why,Why are javabeans completely ignored?

10. VB6 ListView: Why oh why oh why...

11. ********* Null + 'String' = Null !!!!!!! why, why, why!!!!!!!!

12. Syntax: xxx = yyy (Fix(Val(ya ya ya))) ???

 

 
Powered by phpBB® Forum Software