|
A generic benchmark routine
|
Total Hit (3478) |
You often need to benchmark a piece of code, which you usually do as follows:
«Code LangId=2»
Dim start As Date = Now
' insert here the code to benchmark
Dim elapsed As TimeSpan = Now.Subtract(start)
Console.WriteLine("Total time: {0} secs.", elapsed)
«/Code»
Thanks to delegates, you can w
....Read More |
Rating
|
|
|
Add exception tracing with one line of code
|
Total Hit (3958) |
At times you want to keep a log of all the exception occurred in your application, including those that are correctly caught by a Catch block. At first you might believe that you need to add a call to the LogException procedure from each and every Catch block, which is clearly a nuisance:
«Code L
....Read More |
Rating
|
|
|
Create a unique GUID
|
Total Hit (4273) |
The System.Guid type exposes several shared and instance methods that can help you work with GUIDs, that is, those 128-bit numbers that serve to uniquely identify elements and that are ubiquitous in Windows programming. The most important member is the NewGuid shared method is useful for generating
....Read More |
Rating
|
|
|
Create console apps that return an exit code
|
Total Hit (3344) |
Writing an application that returns an ERRORLEVEL to Dos is quite difficult in VB6, because you are forced to use a Windows API that ends the application immediately, thus preventing orderly release of resources. Conversely, this task is quite simple in VB.NET, thanks to the Exit static method of th
....Read More |
Rating
|
|
|
Determine the size of a structure
|
Total Hit (4960) |
Unlike previous Visual Basic versions, under VB.NET you can't use the Len function to calculate the length of a Structure or a class. However, it is easy to get this information with the SizeOf static method of the System.Runtime.InteropServices.Mashal class, as in:
....Read More |
Rating
|
|
|
Evaluate an expression using the Microsoft Script Control
|
Total Hit (3714) |
Thanks to COM Interop, you can still use the Microsoft Script Control from VB.NET and any other .NET language. To do so, you must add a reference to the Microsoft Script Control library (from the COM page in the Add Reference dialog box): this action creates a new reference named Interop.MSScriptCon
....Read More |
Rating
|
|
|
Exclude code portions with the Conditional attribute
|
Total Hit (3352) |
VB developers have always used the #IF compiler directive to include or esclude portions of code from the application. The problem with this directive is that you can easily exclude a procedure with a single directive, but it isn't easy to discard all the calls to that procedure (which would raise a
....Read More |
Rating
|
|
|
Leverage the improved Shell function
|
Total Hit (3092) |
The version of the Shell function included in VB.NET expands on the original version and supports an additional argument that enables you to specify whether to wait until the shelled program terminates, with an optional timeout. This solves an old problem known to many Visual Basic developers withou
....Read More |
Rating
|
|
|
Providing a default value for optional arguments
|
Total Hit (3367) |
Unlike VB6, VB.NET requires that you specify the default value of any Optional argument. In general you should use a value that is invalid under normal circumstances, so that the called procedure can discern whether the argument has been actually passed or not. For example, you should use -1 as a sp
....Read More |
Rating
|
|
|
Retrieving the hi/low byte/word of a value, and other operations
|
Total Hit (4417) |
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 that allows us to set a value to a
....Read More |
Rating
|
|
|
Use a ParamArray as a true array
|
Total Hit (2795) |
Unlike VB6, in VB.NET the ParamArray keyword defines a true array object, which you can process using any of the methods of the Array class. For example, here' s a function that evaluates the lowest value among those passed to the procedure, using the static Array.Sort method and then taking the ele
....Read More |
Rating
|
|
|
Use the Err.GetException method to create custom exceptions
|
Total Hit (3030) |
The Visual Basic .NET Err.Raise method and the Throw command are (partially) compatible. For example, you can use a Try...End Try block to catch an error raised with the Err.Raise method, and you can use an On Error Resume Next and the Err object to neutralize and inspect an exception object created
....Read More |
Rating
|
|
|
Using a union to retrieve the RGB components of a color
|
Total Hit (3044) |
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 how
....Read More |
Rating
|
|
|
Using aliases to quickly change the type of variables
|
Total Hit (3239) |
It may happen that you have to define a lot of variable of some type, and you want the possibility to later change their type without manually change the declaration of all of them. You can't use a simple Find & Replace, because you don't want to change the type of ALL the variables of that original
....Read More |
Rating
|
|
|
Write applications that take arguments
|
Total Hit (3959) |
C# has a nice feature that VB.NET lacks: the ability to define a Main procedure that takes an array of strings, each one containig one of the arguments passed on the command line. It's easy to mimick this feature in VB, though.
In fact, while the Command function is still supported in VB.NET, you
....Read More |
Rating
|
|
|
Ask a Yes/no question and return a Boolean
|
Total Hit (2918) |
«Code LangId=2»
' Ask a Yes/no question
' returns True if the user replies "Yes"
' Example: MessageBox.Show(AskYesOrNo("Do you like me?", "ME", True))
Function AskYesOrNo(ByVal text As String, ByVal title As String, _
ByVal defaultAnswer As Boolean) As Boolean
Dim defButton As Messa
....Read More |
Rating
|
|
|
CompareList - Compare an argument with a list of values
|
Total Hit (2647) |
«Code LangId=2»' Return the position of the argument in a list of values
' or zero if the argument isn't included in the list
' It works for both regular values and for objects
'
' This handy function can often save you a lengthy Select Case
' statement or a complex series of If...ElseIf block
....Read More |
Rating
|
|
|
CompareValue - Check whether a value is in a list of values
|
Total Hit (2813) |
«Code LangId=2»' Compares a numeric or string value with a list of other values.
' Returns the 1-based index of the matching value, or zero if the
' value doesn't appear in the list.
' String comparisons are case-sensitive.
'
' This function can conveniently replace a Select Case or a list
'
....Read More |
Rating
|
|
|
DisplayExceptionInfo - Displaying error information
|
Total Hit (2887) |
«Code LangId=2»
' A reusable routine that displays error information
' Note: requires Imports System.Reflection
Sub DisplayExceptionInfo(ByVal e As Exception)
' Display the error message.
Console.WriteLine(e.Message)
Dim st As New StackTrace(e, True)
Dim i As Integer
....Read More |
Rating
|
|
|
|
|
|
IsComDLL - Check whether a DLL is an COM self-registering server
|
Total Hit (3136) |
«Code LangId=2»<System.Runtime.InteropServices.DllImport("kernel32")> Shared Function _
LoadLibrary(ByVal path As String) As Integer
End Function
<System.Runtime.InteropServices.DllImport("kernel32")> Shared Function _
GetProcAddress(ByVal hModule As Integer, ByVal procName As String)
....Read More |
Rating
|
|
|
|
KeepInRange - Ensure that a value is in a given range
|
Total Hit (2620) |
«Code LangId=2»
' Keep the first argument in the range [lowLimit, highLimit]
' If the value is adjusted, the fourth (optional) argument is set to True
'
' This function works will all basic data types and with objects
' that implement the IComparable interface
Function KeepInRange(ByVal va
....Read More |
Rating
|
|
|
|
Welcome to Visual Basic .NET
|
Total Hit (2507) |
The goal of this book is to help you come up to speed with the Visual Basic .NET language even if you have never programmed anything before. We will start slowly, and build on what we learn. So take a deep breath, let it out slowly, and tell yourself you can do this. No sweat! No kidding! This secon
....Read More |
Rating
|
|
|
Inheritance and Interfaces : Sample Chapter from Professional VB.NET
|
Total Hit (1223) |
Inheritance is the idea that we can create a class that reuses methods, properties, events, and variables from another class. We can create a class with some basic functionality, then use that class as a base from which to create other, more detailed, classes. All these classes will have the same co
....Read More |
Rating
|
|
|
|
Introduction to Exception Handling in Visual Basic .NET
|
Total Hit (1097) |
This article provides an overview of structured and unstructured exception handling in Visual Basic .NET. It includes considerations that help you choose the right exception-handling alternative, the approaches involved in each alternative, how to create your own exceptions, and the exception object
....Read More |
Rating
|
|