Daily Archives: April 17, 2015

Binary Formatting as Dictionary(of

The following Subroutines will encode as dictionary(of String, String) as a binary element to save to a hard drive. I use this to save Settings that I do not put into a registry.

#Region "Binary Saving / Loading"
Dim SettingsFile as string = My.Application.Info.DirectoryPath & "\Settings.bin"
Private Sub SaveBinary(ByVal d As Dictionary(Of String, String))
Dim MyFormatter As New BinaryFormatter()
Dim MyFile As New FileStream(SettingsFile, FileMode.Create, FileAccess.Write, FileShare.None)
MyFormatter.Serialize(MyFile, d)
MyFile.Close()
End Sub
Private Function LoadBinary() As Dictionary(Of String, String)
Dim d As New Dictionary(Of String, String)
Dim MyFormatter As New BinaryFormatter()
Dim MyFile As New FileStream(SettingsFile, FileMode.Open, FileAccess.Read, FileShare.None)
d = MyFormatter.Deserialize(MyFile)
MyFile.Close()
Return d
End Function
#End Region

Time a function or Procedure in Milliseconds

Sometimes there is a need to evaluate how fast ( or slow ) as certain function, procedure takes to complete. This will help in identify where bottle necks are occurring in your procedures.


Dim starttime As DateTime = Now
Dim mset As New clsLoadSettings(clsLoadSettings.SettingsFiletype.XML)
mset.GetSettings()
mset.Dispose()
Debug.Print("Settings Load Time: " & Now.Subtract(starttime).TotalMilliseconds.ToString)

Custom Sorting Numbers in a Non-Linear Orders

I had a problem where a company wanted an array of pictures from a database custom sorted. The pictures already had ID’s assigned to them from a database index.

The items selected would be of varying lengths each time, and not all images would be selected or used.

Computers are great at sorting numbers ascending and descending, but there is really no simple way to order numbers in a nonlinear order.

So taking those indices and the order they requested, I finally came up with a fairly simple way to reorder the requested items without having to hardcode the silly order in.
Dim order() As Integer = {20, 18, 15, 4, 6, 13, 3, 19, 2, 14, 0, 5, 16, 1, 10, 7, 11, 8, 12, 9, 17}
Dim items() As Integer = {9, 12, 14, 14, 5, 8, 3, 10, 18, 20}
Dim dtsort As New DataTable
TextBox1.Clear()
dtsort.Columns.Add("val", GetType(Integer))
dtsort.Columns.Add("sort", GetType(Integer))

For Each i As Integer In items
dtsort.Rows.Add(i, Array.IndexOf(order, i))
Next

Dim dv As New DataView(dtsort)
dv.Sort = "sort ASC"

For Each row As DataRowView In dv
TextBox1.AppendText(row.Item("val") & vbCrLf)
Next