The IsNullOrEmpty method was added to the String class in the 2.0 Framework, and it makes testing for an empty or null string as easy as a single function call. Instead of having to test the String object if it’s null then checking if it’s empty, this can now be done in one easy method call.
VB.NET
Dim s As String
If String.IsNullOrEmpty(s) Then
Console.WriteLine(“String is null or empty.”)
Else
Console.WriteLine(String.Format(“String = {0}”, s))
EndIf
This isn’t exactly an earth shattering discovery, but it’s useful nonetheless.
I recently completed a project at work that included the requirement to monitor data files on 30 or more different Windows NT4 machines for changes. Naturally I thought of using the FileSystemWatcher class in the System.IO namespace. As anyone who has used the FileSystemWatcher has come to realize, it can be unreliable. Specifically, if one of the NT4 machines were rebooted, the watcher would “lose” its ability to capture the file modification events I needed. What was my solution? Well, it’s a bit of a hack. Actually, it’s a really big hack. I used the Timer class in the System.Threading namespace to restart the watchers at a specified interval and to check for changes to the files that I might have missed. The files don’t change very often, so the solution has worked so far. I also can’t install anything on the NT4 machines, so this is what I was forced to do. Come on. I’m not the only one to hack something together like this. Am I?
In the first part of my series on Generics in VB.NET I discussed the advantages of generic collections. In this installment I’ll cover the Nullable Generic Structure.
First, let’s cover some background. A type is nullable if it can be assigned a value or Nothing (null in C#). For example a String object is nullable because it can be assigned either a value or Nothing. On the other hand, value types are not nullable. An Integer cannot be assigned Nothing. To put it simply, reference types are nullable. Value types are not.
Now that we’re experts at nullable and value types in .NET, let’s cover the usefulness of the nullable generic structure. At times it’s useful to know if a value has been assigned to a variable that you cannot initialize as Nothing, but you don’t want to initialize it with the default value. Using the nullable structure can help simplify your code in certain instances. Let’s take a look at two examples.
Without using the nullable structure:
Dim value1 As Integer
‘do some work
…
If value1 = 0 Then
‘the value wasn’t changed, but we can’t be completely sure it wasn’t actually set to 0
‘because an Integer is initialized to 0
Else
‘we’re sure the value was changed, so we’ll continue processing
End If
Now using the nullable structure:
Dim value1 As Nullable(Of Integer)
‘do some work
…
If Not value1.HasValue Then
‘because the variable doesn’t have a value,
‘we’re sure the value hasn’t been set
Else
‘we’re sure the value was changed, so we’ll continue processing
End If
Now of course these examples are contrived, but I think they prove the point. If you want to check if a value type has been assigned a value, consider using the nullable generic structure. It will give you an easy way to “assign” and test for null in a value type.
Tags: Generics, VB.NET
Dim, ReDim, ReDim Preseve, Repeat. Sound familiar? If so, maybe you want to look into generic collections. In the System.Collections namespace the ArrayList class will give you a dynamically allocated list of objects that’s easy to use. You won’t need to worry about ReDim’ing the array if you need to add items, and you won’t have to deal with array indexing issues when adding/removing items.
Example using an array.
‘create the string array with an initial size
Dim values As String(1)
‘add a value
values(0) = “test 1″
‘…. do some work ….
‘now we need to add another value to the array,
’so we need to do a ReDim
ReDim Preserve values(2)
values(1) = “test 2″
Now let’s rewrite the example using an ArrayList
‘create the arraylist
Dim values As New ArrayList()
‘add a value
values.Add(”test 1″)
‘…. do some work ….
‘now add another value, but there’s no need to ReDim
values.Add(”test 2″)
For me, there’s one drawback to ArrayList. Type safety. If you want compile time checking of the object types you’re adding to a collection, look at the generic List(Of Object) class in the System.Collections.Generic namespace. You’ll get type safety and a performance boost by using the generic List object, not to mention Intellisense support.
‘create the list
Dim values As New List(Of String)
values.Add(”test 1″)
You aren’t limited to a simple list either. There’s a sorted list, a dictionary, and a sorted dictionary. For me, the choice is easy. I use generic collections where I might have used an array before. That’s not saying I never use a simple array any more, but generic collections are definitely a good addition to my toolkit.
Tags: Generics, VB.NET