|
A For Each loop that iterates in reverse order
|
Total Hit (11863) |
The For Each loop always iterates orderly on all the elements of an array or a collection (more precisely, on all the elements of a class that implements the IEnumerable interface). What happens if you need to iterate in reverse order, though? If you have an array or an ArrayList you can always refe
....Read More |
Rating
|
|
|
A For Each statement that visits items in random order
|
Total Hit (2783) |
At times you may need to process elements of an array, a collection, or a dictionary object in random order, for example when sampling a population or (more often) when writing a game, such as a card game. Here's a class that you can use in a For Each statement to randomly process all the elements o
....Read More |
Rating
|
|
|
Building arrays on the fly
|
Total Hit (2948) |
VB.NET supports the creation of arrays on-the-fly, by using the New operator. For example, look at the following code, that uses GDI methods to display few connected lines
«Code LangId=2»
' Get the Graphics object from the form.
Dim gr As Graphics = Me.CreateGraphics
' Draw three straight line
....Read More |
Rating
|
|
|
Create zero-elements arrays
|
Total Hit (2639) |
The .NET framework lets you create two types of "empty" arrays: unitialized arrays and arrays that are initialized with zero elements. Uninitialized arrays are actually array variables that are set to Nothing, whereas zero-element arrays are non-Nothing variables that point to arrays with zero eleme
....Read More |
Rating
|
|
|
Store bits and small integers efficiently in a BitVector32
|
Total Hit (3419) |
The System.Collections.Specialized.BitVector32 structure can be used to store up to 32 boolean values, or a set of small integers that can take up to 32 consecutive bits. The BitVector32 is similar to the BitArray class, in that it can hold boolean values that take only one bit each, yet it is more
....Read More |
Rating
|
|
|
Store large Boolean arrays in a BitArray object
|
Total Hit (5939) |
.NET Boolean values require 4 bytes each, as opposed to the 2 byte taken under previous VB versions. When creating very large Boolean arrays this extra memory impacts negatively on the application's performance. The .NET Framework offers a specialized class, the System.Collection.BitArray class, whi
....Read More |
Rating
|
|
|
Understanding passing arrays by reference
|
Total Hit (3621) |
.NET arrays are object types, thus an array variable is actually a pointer to the object where data is actually stored. For this reason, when you pass an array to a procedure the pointer is passed and the called procedure is always able to modify the elements of the array, regardless of whether the
....Read More |
Rating
|
|
|
BinarySearch - Fast search in a sorted array
|
Total Hit (3454) |
«Code LangId=2»
' Binary search in an array of any type
' Returns the index of the matching item, or -1 if the search fails
'
' The arrays *must* be sorted, in ascending or descending
' order (the routines finds out the sort direction).
' LASTEL is the index of the last item to be searched, a
....Read More |
Rating
|
|
|
ArrayDeleteElement - Deleting an element in any type of array
|
Total Hit (2640) |
«Code LangId=2»' 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).
....Read More |
Rating
|
|
|
ArrayInsertElement - Inserting an element in any type of array
|
Total Hit (2693) |
«Code LangId=2»' 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 By
....Read More |
Rating
|
|
|
ArrayListJoin - Merging two ArrayList objects
|
Total Hit (2945) |
«Code LangId=2»' 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")
' a
....Read More |
Rating
|
|
|
FilterDuplicates - Delete duplicate items in an array
|
Total Hit (3271) |
«Code LangId=2»
' Filter out duplicate values in an array and compact
' the array by moving items to "fill the gaps".
' Returns the number of duplicate values
'
' The array is not REDIMed, but you can do it easily using
' the following code:
' a() is a string array
' dups = FilterDu
....Read More |
Rating
|
|
|
|
Efficiently Searching a Sorted Array
|
Total Hit (1627) |
Pop quiz, hotshot: you have an array of strings consisting of the various IP addresses of the visitors of the last 24 hours to your Web site. You want to determine if your site has been visited by the IP address 231.34.124.3. How can you quickly figure out if the string "231.34.124.3" is in the arra
....Read More |
Rating
|
|