locked
Is there Swap function in VB.Net? RRS feed

  • Question

  • Dear Friends,

    I was reading about "Bubble Sort" and related articles. 

    In one of that I found "Swap" function in C++ in a sample code. 

    Just want to know is there a Swap function in VB.Net? I couldn't find any suggestion when I type Swap in VB 2010.

    If so any reason for it only available in VC++ and Not in VC# or in VB.Net ?

    Thanks

    Kind Regards

    VKSBK


    A Real Novice Programmer !

    Monday, April 28, 2014 10:43 AM

Answers

  • Hi, you can create a Swap funciton, please see below:

        Public Sub Swap(Of T)(ByRef d1 As T, ByRef d2 As T)
            Dim d = d2
            d2 = d1
            d1 = d
        End Sub

    and below code show you how to use it:

            Dim i1 As Integer = 3
            Dim i2 As Integer = 4
            Swap(Of Integer)(i1, i2) 'swap i1 and i2, i1 will be 4, i2 will be 3
    
            Dim s1 As String = "3"
            Dim s2 As String = "4"
            Swap(Of String)(s1, s2) 'swap s1 and s2, s1 will be "4", s2 will be "3""

    • Proposed as answer by Reed KimbleMVP Monday, April 28, 2014 1:48 PM
    • Marked as answer by V.K.S.B.K Wednesday, April 30, 2014 10:03 AM
    Monday, April 28, 2014 11:03 AM
  • It's my impression that functions are written by programmers. So if you want a function to perform in VB the same way it would in C++ then you would need to write it to do so I would guess. If it's possible as C++ can do things VB can not do.

    Below are some conversions of a C++ swap function using a C++ to VB converter.

    'C++ TO VB CONVERTER TODO TASK: The original C++ template specifier was replaced with a VB generic specifier, which may not produce the same behavior:
    'ORIGINAL LINE: template <class T>
    Private Sub swap(Of T)(ByRef a As T, ByRef b As T)
      Dim c As New T(a)
      a = b
      b = c
    End Sub

    'C++ TO VB CONVERTER TODO TASK: The original C++ template specifier was replaced with a VB generic specifier, which may not produce the same behavior:
    'ORIGINAL LINE: template <class T>
    Private Sub swap(Of T)(ByRef a As T, ByRef b As T)
      Dim c As New T(std.move(a))
      a = std.move(b)
      b = std.move(c)
    End Sub

    'C++ TO VB CONVERTER TODO TASK: The original C++ template specifier was replaced with a VB generic specifier, which may not produce the same behavior:
    'ORIGINAL LINE: template <class T, UInteger N>
    Private Sub swap<T, UInteger N>(ByVal a() As T, ByVal b() As T)
      For i As UInteger = 0 To N - 1
    	  swap(a(i), b(i))
      Next i
    End Sub

    'C++ TO VB CONVERTER TODO TASK: The original C++ template specifier was replaced with a VB generic specifier, which may not produce the same behavior:
    'ORIGINAL LINE: template< class T >
    Private Sub MySwap(Of T)(ByRef a As T, ByRef b As T)
       Dim c As New T(a)
       a = b
       b = c
    End Sub
    

    Generics in the .NET Framework


    La vida loca

    • Marked as answer by V.K.S.B.K Wednesday, April 30, 2014 10:03 AM
    Monday, April 28, 2014 11:07 AM
  • There is actually a SWAP keyword in QuickBasic, which was never carried over to Visual Basic.  Instead, you would write a swap routine, consisting of 3 lines of code and making use of a temporary variable.  It can be used with either strings or numbers.  This problem is frequently presented to students beginning a programming class. 

            Dim strA, strB, strTemp As String
            Dim intA, intB, intTemp As Integer
            strA = "apple"
            strB = "banana"
            intA = 5
            intB = 8
            MessageBox.Show("B is: " & strB & " " & intB, "A is: " & strA & " " & intA)
            strTemp = strA      'swap strings
            strA = strB
            strB = strTemp
            intTemp = intA      'swap numbers
            intA = intB
            intB = intTemp
            MessageBox.Show("B is now: " & strB & " " & intB, "A is now: " & strA & " " & intA)


    Solitaire


    • Edited by Solitaire Tuesday, April 29, 2014 1:40 AM
    • Marked as answer by V.K.S.B.K Wednesday, April 30, 2014 10:03 AM
    Tuesday, April 29, 2014 1:39 AM
  • I'd would create the following extension method.

    Public Module Exts
    
      <System.Runtime.CompilerServices.Extension()>
      Sub SwapWith (Of T )(ByRef thisObj     As T,
                           ByRef withThisObj As T)
        Dim tempObj = thisObj
        thisObj = withThisObj
        withThisObj = tempObj
      End Sub
    
    End Module

    This then permit the following style of coding.

    Dim a = 1
    Dim b = 2
    a.SwapWith(b)
    
    Dim s0 = "Banana"
    Dim s1 = "Apple"
    s0.SwapWith(s1)

    • Edited by AdamSpeight2008 Wednesday, April 30, 2014 12:31 AM
    • Marked as answer by V.K.S.B.K Wednesday, April 30, 2014 10:05 AM
    Wednesday, April 30, 2014 12:30 AM

All replies

  • Hi, you can create a Swap funciton, please see below:

        Public Sub Swap(Of T)(ByRef d1 As T, ByRef d2 As T)
            Dim d = d2
            d2 = d1
            d1 = d
        End Sub

    and below code show you how to use it:

            Dim i1 As Integer = 3
            Dim i2 As Integer = 4
            Swap(Of Integer)(i1, i2) 'swap i1 and i2, i1 will be 4, i2 will be 3
    
            Dim s1 As String = "3"
            Dim s2 As String = "4"
            Swap(Of String)(s1, s2) 'swap s1 and s2, s1 will be "4", s2 will be "3""

    • Proposed as answer by Reed KimbleMVP Monday, April 28, 2014 1:48 PM
    • Marked as answer by V.K.S.B.K Wednesday, April 30, 2014 10:03 AM
    Monday, April 28, 2014 11:03 AM
  • It's my impression that functions are written by programmers. So if you want a function to perform in VB the same way it would in C++ then you would need to write it to do so I would guess. If it's possible as C++ can do things VB can not do.

    Below are some conversions of a C++ swap function using a C++ to VB converter.

    'C++ TO VB CONVERTER TODO TASK: The original C++ template specifier was replaced with a VB generic specifier, which may not produce the same behavior:
    'ORIGINAL LINE: template <class T>
    Private Sub swap(Of T)(ByRef a As T, ByRef b As T)
      Dim c As New T(a)
      a = b
      b = c
    End Sub

    'C++ TO VB CONVERTER TODO TASK: The original C++ template specifier was replaced with a VB generic specifier, which may not produce the same behavior:
    'ORIGINAL LINE: template <class T>
    Private Sub swap(Of T)(ByRef a As T, ByRef b As T)
      Dim c As New T(std.move(a))
      a = std.move(b)
      b = std.move(c)
    End Sub

    'C++ TO VB CONVERTER TODO TASK: The original C++ template specifier was replaced with a VB generic specifier, which may not produce the same behavior:
    'ORIGINAL LINE: template <class T, UInteger N>
    Private Sub swap<T, UInteger N>(ByVal a() As T, ByVal b() As T)
      For i As UInteger = 0 To N - 1
    	  swap(a(i), b(i))
      Next i
    End Sub

    'C++ TO VB CONVERTER TODO TASK: The original C++ template specifier was replaced with a VB generic specifier, which may not produce the same behavior:
    'ORIGINAL LINE: template< class T >
    Private Sub MySwap(Of T)(ByRef a As T, ByRef b As T)
       Dim c As New T(a)
       a = b
       b = c
    End Sub
    

    Generics in the .NET Framework


    La vida loca

    • Marked as answer by V.K.S.B.K Wednesday, April 30, 2014 10:03 AM
    Monday, April 28, 2014 11:07 AM
  • If you are read about other program language then you should know for what they are made.

    Somebody who is writing drivers will always tell that Intel Assembler or C are the best for that on a Windows OS.

    Somebody who is creating HTML pages will always tell that JavaScript is the best program languages

    Somebody who has  to create administrative applications will always tell that managed code languages (VB, C#, Java) are the best for that.

    However, You can create with VB a bubble sort. Moreover, you can create things like your own multiplier function and not use the *,

    If it is wise? No, the given methods are almost forever better. 

     

    Success
    Cor

    Monday, April 28, 2014 5:05 PM
  • There is actually a SWAP keyword in QuickBasic, which was never carried over to Visual Basic.  Instead, you would write a swap routine, consisting of 3 lines of code and making use of a temporary variable.  It can be used with either strings or numbers.  This problem is frequently presented to students beginning a programming class. 

            Dim strA, strB, strTemp As String
            Dim intA, intB, intTemp As Integer
            strA = "apple"
            strB = "banana"
            intA = 5
            intB = 8
            MessageBox.Show("B is: " & strB & " " & intB, "A is: " & strA & " " & intA)
            strTemp = strA      'swap strings
            strA = strB
            strB = strTemp
            intTemp = intA      'swap numbers
            intA = intB
            intB = intTemp
            MessageBox.Show("B is now: " & strB & " " & intB, "A is now: " & strA & " " & intA)


    Solitaire


    • Edited by Solitaire Tuesday, April 29, 2014 1:40 AM
    • Marked as answer by V.K.S.B.K Wednesday, April 30, 2014 10:03 AM
    Tuesday, April 29, 2014 1:39 AM
  • Dear Friends ..

    Thanks for explaining me.. and showing me the Code for "Swap".

    Kind Regards

    VKSBK


    A Real Novice Programmer !

    Tuesday, April 29, 2014 11:48 AM
  • Two unrelated but related things. 

    VKSBK please mark the response that best answered  your question as THE answer to close the question.

    MrMonkeyBoy, where on earth did you find a C++ to VB converter?

    Renee


    "MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me

    Tuesday, April 29, 2014 12:41 PM
  • Two unrelated but related things. 

    VKSBK please mark the response that best answered  your question as THE answer to close the question.

    MrMonkeyBoy, where on earth did you find a C++ to VB converter?

    Renee


    "MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me

    At Tangible but I used their free demo converter(s) which can only do up to 100 lines at a time.

    However it will sometimes ask you for header files so that it can convert code. And I tried converting some C++ code of less than 100 lines but once it had all the header file locations it would not convert the code because it was somehow over 100 lines. Which leads me to believe that something in the header files becomes inclusive in the 100 lines to be converted.


    La vida loca

    Tuesday, April 29, 2014 6:00 PM
  • I'd would create the following extension method.

    Public Module Exts
    
      <System.Runtime.CompilerServices.Extension()>
      Sub SwapWith (Of T )(ByRef thisObj     As T,
                           ByRef withThisObj As T)
        Dim tempObj = thisObj
        thisObj = withThisObj
        withThisObj = tempObj
      End Sub
    
    End Module

    This then permit the following style of coding.

    Dim a = 1
    Dim b = 2
    a.SwapWith(b)
    
    Dim s0 = "Banana"
    Dim s1 = "Apple"
    s0.SwapWith(s1)

    • Edited by AdamSpeight2008 Wednesday, April 30, 2014 12:31 AM
    • Marked as answer by V.K.S.B.K Wednesday, April 30, 2014 10:05 AM
    Wednesday, April 30, 2014 12:30 AM
  • That figures...but such a converter is nice!!!

    Renee


    "MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me

    Wednesday, April 30, 2014 3:32 AM
  • There is actually a SWAP keyword in QuickBasic, which was never carried over to Visual Basic.  Instead, you would write a swap routine, consisting of 3 lines of code and making use of a temporary variable.  It can be used with either strings or numbers.  This problem is frequently presented to students beginning a programming class. 

            Dim strA, strB, strTemp As String
            Dim intA, intB, intTemp As Integer
            strA = "apple"
            strB = "banana"
            intA = 5
            intB = 8
            MessageBox.Show("B is: " & strB & " " & intB, "A is: " & strA & " " & intA)
            strTemp = strA      'swap strings
            strA = strB
            strB = strTemp
            intTemp = intA      'swap numbers
            intA = intB
            intB = intTemp
            MessageBox.Show("B is now: " & strB & " " & intB, "A is now: " & strA & " " & intA)


    Solitaire


    A number of posters have all given the obvious solution - copying one of the values to a temporary variable and then reassigning the values to the appropriate variables. This method is fine as long as you're just swapping around integers. However, if you start swapping large strings, objects, file records or database records, you'll find that this approach bogs down quickly as the size of the object increases. 

    You brought up the SWAP keyword from QuickBASIC (also present as far back as GW-BASIC and other variants of MBASIC). The SWAP keyword offered some major performance benefits over the method described here, and it wasn't just because it was machine code - it used a slightly different algorithm. Instead of swapping the values among the variables, it swapped the pointers to the variables. This meant that no matter how large the string, object or record was, SWAP was just moving around integers of one type or another (depending on the system in use).

    The benefits are obvious. All other things being equal, moving around smaller amounts of data can always be accomplished more quickly than moving around large chunks of data. Also, this method is scalable - the efficiency never changes no matter how large the objects get. The method described here by the other posters is not scalable and suffers greatly as the size of the object increases.
    • Edited by SpeedRacer370Z Tuesday, February 13, 2018 1:57 AM Additional thoughts
    Tuesday, February 13, 2018 1:37 AM
  • Yea in the forums persons give often replies on a part of a question which they are interesting in. 

    The question was about a bubble sort and the OP told he had seen a "swap" function. 

    However, for the bubble sort is an interface in .Net and nobody told that, but found "swap" more interesting.

    https://msdn.microsoft.com/en-us/library/system.collections.icomparer(v=vs.110).aspx

    That is how it often goes in forums. 

    And then an own built method is marked as answer. 


    Success Cor

    Tuesday, February 13, 2018 9:50 AM