How to randomize an array in VB.net

Do you want to randomize the items in an array with VB.net? Add these 2 functions below to your code to get it working. The reason there’s 2 functions you must add is because the first function is using the second function to make it all work.

To use it, you just type: Shuffle(YourArray, 5)

The number is just how many times it’s going to shuffle the array, you can use whatever you want here.

Here is the code for the 2 functions:

Public Function Shuffle(ByRef arrayToBeShuffled As Array, ByVal numberOfTimesToShuffle As Integer)
        Dim rndPosition As New Random(DateTime.Now.Millisecond)
        For i As Integer = 1 To numberOfTimesToShuffle
            For i2 As Integer = 1 To arrayToBeShuffled.Length
                Swap(arrayToBeShuffled(rndPosition.Next(0, arrayToBeShuffled.Length)), arrayToBeShuffled(rndPosition.Next(0, arrayToBeShuffled.Length)))
            Next i2
        Next i
        Return ("")
End Function

Public Function Swap(ByRef arg1 As Object, ByRef arg2 As Object)
        Dim strTemp As String
        strTemp = arg1
        arg1 = arg2
        arg2 = strTemp
        Return ("")
End Function

Remove duplicates from array in VB.net

Remove duplicates from array in VB.net

Here’s a function that will remove duplicate items in a array. The code is for VB.net.

The code:

    Function RemoveDuplicates(ByVal initialArray As String())
        Dim i As Integer
        Dim j As Integer
        Dim newArray(0) As String

        For i = 0 To UBound(initialArray)
            For j = 0 To UBound(initialArray)
                If Not initialArray(i) = "" Then
                    If Not j = i Then
                        If initialArray(i) = initialArray(j) Then
                            initialArray(j) = ""
                        End If
                    End If
                End If
            Next
        Next

        j = 0
        For i = 0 To UBound(initialArray)
            If Not initialArray(i) = "" Then
                ReDim Preserve newArray(j)
                newArray(j) = initialArray(i)
                j += 1
            End If
        Next

        Return newArray

    End Function

VB.net: Update Listbox with Time,Text then Autoscroll

When making applications in VB.net I often use a Listbox to give the application user some kind of status updates when a application have to loop through a huge amount of data.
It’s a nice way to be able to tell if it’s still working and doesn’t seem like a non-responding application that have crashed.

Therefor I wrote this little code snippet, it’s basically Sub I got inside my elite-module.vb (//irony) which is filled with handy custom functions, suns and such that I tend to use often in my projects.

Anyway, instead of using ~4 lines of code every time to add some text as a update the Listbox, then autoscrolls down to show the new status item and also do a UI refresh.
If there’s intense CPU load the UI might not respond so well, so we force it to redraw.

Here’s the code snippet:

    ' UpdateStatus (Made for "Status updates". It adds/update the the Current Time + Your text string. Then Scrolls down to new item & Refresh Listbox UI.
    ' Example of usage: UpdateStatus(ListBox1, "I just added a new status update...")

    Public Sub UpdateStatus(ByRef ListBoxName As ListBox, StatusComment As String)

        ListBoxName.Items.Add(TimeOfDay.ToString("H:mm:ss") & ": " & StatusComment)
        ListBoxName.TopIndex = ListBoxName.Items.Count - 1
        ListBoxName.Refresh()
        Return
    End Sub

If you don’t want to use the 24-hour time format, you can replace that bit of code with TimeOfDay.ToString(“h:mm:ss tt“) which will give you a output in this format: “7:22:14 pm”

I hope you find it useful.