October 28, 2002

ArrayDeleteElement – Deleting an element in any type of array

‘ A generic routine that deletes an element in any type of array.’ Example: deleting the 2nd element of the array arr’ ArrayDeleteElement(arr, 1) Sub ArrayDeleteElement(ByVal arr As Array, ByVal index As Integer) ‘ Shift elements from arr(index+1) to arr(index). Array.Copy(arr, index + 1, arr, index, UBound(arr) – index) ‘

ArrayListJoin – Merging two ArrayList objects

‘ A reusable function that merges two ArrayList objects’ Example:’ Dim al As New ArrayList()’ al.Add(“Jack”)’ al.Add(“Mary”)’ val.Add(“Bob”)’ al.Add(“Tom”)’ ‘ Dim al2 As New ArrayList()’ al2.Add(“Frank”)’ al2.Add(“Lee”)’ al2.Add(“Nick”)’ ‘ al = ArrayListJoin(al, al2)Function ArrayListJoin(ByVal al1 As ArrayList, ByVal al2 As ArrayList) As _ ArrayList ‘ Note how we avoid time-consuming

ArrayInsertElement – Inserting an element in any type of array

‘ A generic routine that inserts an element in any type of array.’ Example: inserting an entry of value 5 between the 1st and 2nd entry of the ‘ array arr’ ArrayInsertElement(arr, 1, 5)Sub ArrayInsertElement(ByVal arr As Array, ByVal index As Integer, _ Optional ByVal newValue As Object = Nothing)

Retrieving the hi/low byte/word of a value, and other operations

As explained in the tip about the ARGBColor structure (look at the end of this tip for the link), we can define a structure and have its fields that point to the same memory address, but that read a different number of bytes. This makes easier to define a structure

Using a union to retrieve the RGB components of a color

In VB.NET you can create what in C is called a union, i.e. a particular structure where you can access the same memory with different names. Here’s how you declare such a structure by using the StructLayout attribute, and specifying that you want to define the structure layout explicitly, namely