|
|
|
|
Visual Basic TipsI provide these for reference only. Any code samples work as well as they work in my applications. Which means, they may be wrong and I haven't found any problems. Yet. Table of Contents
How do I check to see if an object is set to nothing?How many will remember to use Is Nothing? I can't tell you how many times I forget this. So, I created a function to remember for me. I am pretty lazy, so I do things to make me more effective. I remember IsNull(), IsMissing()...So, I create functions for IsZeroLength(), IsNullString(), and...IsNothing(). Then I can avoid all the stupid logic constructs need to test variables. The top version is my preference. It is easy to read, easy to debug, and easy for the compiler to optimize. I don't worry about the extra time for the function call. What slows Visual Basic down is string handling. Plain and simple.
Public Function IsNothing(ByRef objReference As Object) As Boolean
If objReference Is Nothing Then
IsNothing = True
Else
IsNothing = False
End If
End Function
Public Function IsNothing(objReference as Object) as Boolean
IsNothing = Not (objReference Is Nothing)
End Function
Or, if you prefer:
If obj Is Nothing Then
Set obj = objSomething
End If
If Not (obj Is Nothing) Then
Set obj = Nothing
End If
Also, notice I am a big believer in parentheses. Back to TopWhat about IsNullString()?Once again, my preferred version is:
Public Function IsNullString(byval strValue as String) as Boolean
If strValue = vbNullString Then
IsNullString = True
Else
IsNullString = False
End If
End Function
You could do: IsNullString = Not (strValue = vbNullString) Yeah right, I have money to make and coding to do. How would I debug that statement above easily? The answer is I could not. Leave the decoding to the intelligence experts. Back to TopAnd IsZeroLength()?This works real well when working with record sets, and I do mean well:
If IsZeroLength(TextBox1) = False Then
!Field1 = TextBox1
Else
!Field1 = Null
End If
Or, if you prefer: !Field1 = IIF(IsZeroLength(TextBox1), Null, TextBox1) Back to TopAvoid IIF()IIF() is extremely slow. Better is If...Then...End If. You can even wrap If...Then...Else into a function, and it will still be faster than IIF():
Public Function My_IIF( _
byref Expression as variant, _
byref TruePart as Variant, _
byref FalsePart as Variant) as Variant
'
If Expression = True Then
My_IIF= TruePart
Else
My_IIF= FalsePart
End If
End Function
Hardcore Visual Basic has an excellent discussion the performance of IIF(). Back to TopPathCompactPath() FunctionTruncates a file path to fit within a given pixel width by replacing path components with ellipses. For displayed text, replaces characters in the middle of the string with ellipses so that the result fits in the specified rectangle. If the string contains backslash (\) characters, DT_PATH_ELLIPSIS preserves as much as possible of the text after the last backslash. SS_PATHELLIPSIS Ellipsis Path C:\My Documents\Business\dBase\Assets\MISAssets2K5.mdb C:\My Documents\Bu...\MISAssets2K5.mdb
Private Declare Function PathCompactPath Lib "shlwapi.dll" Alias "PathCompactPathA" ( _
ByVal hdc As IntPtr, _
ByVal lpszPath As System.Text.StringBuilder, _
ByVal dx As Integer) As Integer
Private Sub Form_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
'
Dim oGraphics As Graphics = Label1.CreateGraphics
Dim hDc As IntPtr = oGraphics.GetHdc
Dim oPath As New System.Text.StringBuilder(g_sDataPath & "\" & g_sDataFile)
PathCompactPath(hDc, oPath, Label1.Width)
Label1.Text = oPath.ToString
oGraphics.ReleaseHdc(hDc)
oGraphics.Dispose()
'if you do not release the device context then you will get
' An unhandled exception of type 'System.InvalidOperationException' occurred in system.drawing.dll
' The object is currently in use elsewhere.
End Sub
PathCompactPath, PathCompactPathEx, DrawText, DrawTextEx, DT_PATH_ELLIPSIS, DrawString, StringFormat, StringFormat.Trimming, StringFormat.Trimming.EllipsisPath, Ellipsis Path, Path Ellipsis, SS_PATHELLIPSIS Back to Top
Created: 26 Oct 2001 01:39:57 -0700
Changed: 12 Jun 2005 17:46:39 -0800 |
|
|