How to write a generic swap function in VB.NET

1 Answer

0 votes
Imports System

' ------------------------------------------------------------
' generic_swap
' ------------------------------------------------------------
' A fully generic VB.NET swap function implemented using generics.
'
' Parameters:
'   a, b  - variables passed ByRef so their values can be swapped
'
' This function uses a temporary variable and assignment, which is the
' idiomatic and type‑safe way to swap objects in VB.NET. It works for:
'   - built‑in types (Integer, Double, etc.)
'   - String
'   - Structures / Classes
'   - user‑defined types
' ------------------------------------------------------------
Module SwapUtils
    Public Sub GenericSwap(Of T)(ByRef a As T, ByRef b As T)
        Dim temp As T = a
        a = b
        b = temp
    End Sub
End Module

' ------------------------------------------------------------
' VB.NET equivalent of the C++ struct Point
' ------------------------------------------------------------
Structure Point
    Public X As Integer
    Public Y As Integer

    Public Sub New(x As Integer, y As Integer)
        Me.X = x
        Me.Y = y
    End Sub

    Public Overrides Function ToString() As String
        Return "(" & X & "," & Y & ")"
    End Function
End Structure

' ------------------------------------------------------------
' Print arrays of integers
' ------------------------------------------------------------
Module Utils
    Public Sub PrintIntArray(arr As Integer())
        For Each v In arr
            Console.Write(v & " ")
        Next
        Console.WriteLine()
    End Sub
End Module

Module Program
    Sub Main()

        Console.WriteLine("=== TEST 1: Swap integers ===")
        Dim x As Integer = 10
        Dim y As Integer = 20
        Console.WriteLine("Before: x=" & x & ", y=" & y)
        SwapUtils.GenericSwap(x, y)
        Console.WriteLine("After:  x=" & x & ", y=" & y & Environment.NewLine)

        Console.WriteLine("=== TEST 2: Swap doubles ===")
        Dim a As Double = 3.14
        Dim b As Double = 2.71
        Console.WriteLine("Before: a=" & a & ", b=" & b)
        SwapUtils.GenericSwap(a, b)
		Console.WriteLine("After:  a=" & a & ", b=" & b & Environment.NewLine)

        Console.WriteLine("=== TEST 3: Swap structs ===")
        Dim p1 As New Point(1, 2)
        Dim p2 As New Point(9, 8)
        Console.WriteLine("Before: p1=" & p1.ToString() & ", p2=" & p2.ToString())
        SwapUtils.GenericSwap(p1, p2)
        Console.WriteLine("After:  p1=" & p1.ToString() & ", p2=" & p2.ToString() & Environment.NewLine)

        Console.WriteLine("=== TEST 4: Swap array elements ===")
        Dim arr As Integer() = {1, 2, 3, 4, 5}
        Console.Write("Before: ")
        Utils.PrintIntArray(arr)
        SwapUtils.GenericSwap(arr(1), arr(3))
        Console.Write("After:  ")
        Utils.PrintIntArray(arr)
        Console.WriteLine()

        Console.WriteLine("=== TEST 5: Swap strings ===")
        Dim str1 As String = "Hello"
        Dim str2 As String = "World"
        Console.WriteLine("Before: str1=""" & str1 & """, str2=""" & str2 & """")
        SwapUtils.GenericSwap(str1, str2)
        Console.WriteLine("After:  str1=""" & str1 & """, str2=""" & str2 & """" & Environment.NewLine)

    End Sub
End Module


' ------------------------------------------------------------
' run:
'
' === TEST 1: Swap integers ===
' Before: x=10, y=20
' After:  x=20, y=10
'
' === TEST 2: Swap doubles ===
' Before: a=3.14, b=2.71
' After:  a=2.71, b=3.14
'
' === TEST 3: Swap structs ===
' Before: p1=(1,2), p2=(9,8)
' After:  p1=(9,8), p2=(1,2)
'
' === TEST 4: Swap array elements ===
' Before: 1 2 3 4 5 
' After:  1 4 3 2 5 
'
' === TEST 5: Swap strings ===
' Before: str1="Hello", str2="World"
' After:  str1="World", str2="Hello"
' ------------------------------------------------------------

 



answered 5 hours ago by avibootz
...