|
Process string faster with the StringBuilder class
|
Total Hit (2928) |
You can think of a StringBuilder object as a buffer that can contain a string with the ability to grow from zero characters to the buffer's current capacity. Until you exceed that capacity, the string is assembled in the buffer and no object is allocated or released. If the string becomes longer tha
....Read More |
Rating
|
|
|
Reducing string memory usage with the Intern method
|
Total Hit (3091) |
A .NET application can optimize string management by maintaining an internal pool of string values known as intern pool. If the value being assigned to a string variable coincides with one of the strings already in the intern pool, no additional memory is created and the variable receives the addres
....Read More |
Rating
|
|
|
Right-align formatted strings
|
Total Hit (3446) |
The String.Format function supports many formatting options, but none allows you to display right-aligned columns of numbers, as in:
1.00
12.00
123.00
1,234.00
However, you can easily create a helper function that works like String.Format, takes an additional length argument,
....Read More |
Rating
|
|
|
Trimming strings
|
Total Hit (3129) |
Visual Basic .NET strings expose three trim methods: TrimStart, TrimEnd, and Trim - which trim one or more characters from the beginning, the end, or both the beginning and end of the string. They therefore work like VB6's LTrim, RTrim, and Trim functions, with an important difference: you can decid
....Read More |
Rating
|
|
|
An enhanced VB DatePart function
|
Total Hit (4684) |
Here is an enhanced version of VB DatePart function: the first parameter is now an enumerated type, so easier to use. DatePartEx is 4-5 times faster than DatePart and supports also two new intervals: MonthName and WeekdayName.
|
Rating
|
|
|
Benchmarks with millisecond accuracy
|
Total Hit (3871) |
The Timer function returns a value which is only accurate to about 55 milliseconds, therefore it is not very useful for doing accurate benchmarks. If you need a better resolution you may try out this function:
|
Rating
|
|
|
Beware of slashes when formatting dates
|
Total Hit (2938) |
You probably use the Format function to format date, for example:
«Code LangId=1»
? Format(Now, "mm/dd/yyyy") ' => 08/01/2001
? Format(Now, "mm+dd+yyyy") ' => 08+01+2001
«/Code»
It seems that this is everything you need to know, right? However, the story is quite different
....Read More |
Rating
|
|
|
Create a system timer using AddressOf and a callback function
|
Total Hit (4098) |
The Timer control is great when you want to periodically execute a piece of code while the program is doing something else. However, it also has a couple of shortcomings: (1) it requires a parent form, so you can't use it directly inside a BAS module, and (2) it's Interval property can't be higher t
....Read More |
Rating
|
|
|
Evaluate the number of days remaining in the current year
|
Total Hit (2928) |
The DatePart() function can return the number of days from the beginning of the current year, but there is no VB built-in function that returns the number of days left to the end of the year. You can work around this with the DateDiff function:
«Code LangId=1»
' aDate contains a valid date
Days
....Read More |
Rating
|
|
|
High-precision timing with the QueryPerformanceCounter API function
|
Total Hit (4880) |
While VB Timer functions is sufficiently precise for most tasks, it doesn't provide the highest possible resolution most modern computer can provide. If your hardware supports high-resolution counters, you can do a much better job with a pair of API functions:
«Code LangId=1»
Private Type LARGE_
....Read More |
Rating
|
|
|
Retrieve Time Zone information
|
Total Hit (7147) |
The GetTimeZoneInformation API returns a TIME_ZONE_INFORMATION variable that contains several pieces of information about system's time date and time setting. The first Long value in this structure holds the "distance" (in minutes) from Greenwich standard time, so it's quite easy to create a GetTime
....Read More |
Rating
|
|
|
Test for Leap Year
|
Total Hit (3298) |
As long as you trust VB's IsDate function (and you can, trust me), here's the shorted and fastest method to check if a year is a leap year or not:
«Code LangId=1»
Function IsLeapYear(year As Integer) As Boolean
IsLeapYear = IsDate("2/29/" & CStr(year))
End Function
«/Code»
Here's is anot
....Read More |
Rating
|
|
|
The age of a person
|
Total Hit (2845) |
You can quickly evaluate the age of a person using the following function:
«Code LangId=1»
Function Age(BirthDate As Date, Optional CurrentDate As Variant) As Integer
If IsMissing(CurrentDate) Then CurrentDate = Now
Age = Year(CurrentDate) - Year(BirthDate)
End Property
«/Code»
If
....Read More |
Rating
|
|
|
The beginning or end of previous week
|
Total Hit (3807) |
For reporting, many times you need to figure out what date last week began and ended. Use the code below to figure that out:
|
Rating
|
|
|
The number of days in a month
|
Total Hit (3863) |
You need just one-line function to evaluate the number of days in a month (while taking leap years into account, of course). The trick is to use the DateDiff and DateSerial functions to calculate the difference between the first day of the current month and the first day of the subsequent month :
....Read More |
Rating
|
|
|
Tricks with DateSerial
|
Total Hit (3219) |
The DateSerial function has an interesting feature: it doesn't raise errors when you pass it an invalid month or day number. Instead, it evaluates the date as if the arguments were valid. For example, DateSerial(2000, 1, 32) returns the Date value of February 1, 2000. This behavior can (and should)
....Read More |
Rating
|
|
|
Use Sleep API to add pauses to your programs
|
Total Hit (4035) |
Don't use an empty For...Next loop to add pauses to your programs; instead, use the Sleep API function, that releases the CPU and lets other apps in the system effectively multitask:
«Code LangId=1»
Declare Sub Sleep Lib "kernel32" (ByVal milliseconds As Long)
' pause for 5 seconds
Sleep 5000
....Read More |
Rating
|
|
|
Parsing and validating string dates
|
Total Hit (2830) |
If you have a string variable that should specify a date (asked in input to the user, for example), you can parse the string and get a Date variable by using the Date.Parse method. If the input string is not in a valid format, this method throws an exception though. Follows a routine that parses a s
....Read More |
Rating
|
|
|
|
Check whether a string array contains an item (without a loop)
|
Total Hit (2632) |
To determine whether a String array contains a given item it seems that you can't avoid writing a loop. However, you can do it with just one line of code, using the new VB6 Join function:
«Code LangId=1»
' ARR is an array of string, SEARCH is the value to be searched
Found = InStr(1, vbNullChar
....Read More |
Rating
|
|
|
Count Sort, a special case of indexed sort
|
Total Hit (2935) |
CountSort is yet another sort algorithm, which can be applied only under very particular conditions but that, when such conditions are met, turns to be the fastest of the lot. Count Sort can be used when the key is of Integer type, or when it is of Long type but the range of values is not too large.
....Read More |
Rating
|
|
|
Polymorphic array procedures
|
Total Hit (2330) |
You can create "polymorphic" routines that work with any type of array (except arrays of fixed-length strings, or UDTs) by using Variant parameters. Take for example the following code:
«Code LangId=1»
Function Sum(arr As Variant) As Variant
Dim i As Long
For i = LBound(arr) To UBound(
....Read More |
Rating
|
|
|
Quickly clear a portion of an array
|
Total Hit (2860) |
The fastest way to clear an array is to ReDim (if the array is dynamic) or Erase it (if the array is Static). However, if you want to clear a portion of an array, it seems that you must code a For-Next loop.
If you are dealing with numeric arrays, there is a faster alternative, based on the ZeroM
....Read More |
Rating
|
|
|
Quickly initialize Variant and String arrays
|
Total Hit (3458) |
Visual Basic doesn't provide any way to declare an array and initialize its elements at the same time. In most cases you end up with setting individual elements one by one, as in:
«Code LangId=1»
Dim strArray(0 To 3) As String
strArray(0) = "Spring"
strArray(1) = "Summer"
strArray(2) = "Fall"
....Read More |
Rating
|
|
|
Scan all the items in a multi-dimensional array with only one loop
|
Total Hit (2456) |
It seems that you need two nested For loops to iterate over all the elements of a 2-dimensional array, and three loops for a 3-dimensional array, and so on. However, the For Each loop offers you a neat and concise solution, as this code proves:
«Code LangId=1»
' a 2-dimensional array of Strings
....Read More |
Rating
|
|
|
Simple variables are always faster than array elements
|
Total Hit (2789) |
Reading and writing an item of an array is always slower than accessing a simple variable. Therefore, if you need to repeatedly use the same array item in a loop, you should assign it to a temporary variable and use that variable instead. I've included an example of this technique that scans an Inte
....Read More |
Rating
|
|
|
Sorting on multiple keys
|
Total Hit (2339) |
Frequently you need to sort arrays of records using multiple keys. This may be required since one single key does not uniquely identify a record (e.g. you may need both LastName and FirstName to select a given employee in a large company where people with same name work), or it may be necessary for
....Read More |
Rating
|
|
|
Speed up searches with hash tables
|
Total Hit (3047) |
You probably know that there are basically two methods to search a value in an array: the brute force approach (i.e. linear searching) and the binary search. Both of them have disadvantages: when the array counts N items, linear searching requires N/2 comparisons on the average for successful search
....Read More |
Rating
|
|
|
The number of dimensions of an array
|
Total Hit (4103) |
Using "pure" VB, the only way to build a generic routine that returns the number of dimensions of an array passed as an argument is using a loop that repeatedly tests the LBound (o UBound) function until it fails:
«Code LangId=1»
Function ArrayDims(arr As Variant) As Integer
Dim i As Intege
....Read More |
Rating
|
|
|
Undocumented trick to speed up functions that return array
|
Total Hit (2668) |
VB6 functions can return an array. Unlike regular functions that return scalar values or objects, however, you can't use the name of the function as a local variable where to store intermediate result, and you are forced to work with a temporary local array, and then assign this array to the Functio
....Read More |
Rating
|
|