Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Faster string comparison with the Is operator

Total Hit ( 3204)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


.NET strings are objects, so what you store in a String variable is actually a reference to a String object allocated in the managed heap. Even though the VB.NET compiler attempts to hide the object nature of strings as much as it can - for example, you don't need to declare a String object with a New operator - in many cases being aware of their object nature can help you in making the most out of strings, as well as avoid some subtle programming mistakes. For example, consider this code:

Click here to copy the following block
Dim s1 As String = "One"
Dim s2 As String = "Two"
Dim res As String

' Randomly assign Res from s1 or s2
If Rnd < 0.5 Then
  res = s1
Else
  res = s2
End If
' ...
' ...
' later we test the Res variable
If res = s1 Then
  ' Res is equal to s1
Else
  ' Res is equal to s2
End If

The above code is the kind of code that a VB6 developer would write, but it can be improved remarkably by recognizing that strings are objects. In fact, the Res variable contains either a reference to the same object pointed to by the s1 variable or the object pointed to by the s2 variable, so you can make the test faster by using the Is operator:

Click here to copy the following block
If res Is s1 Then
  ' Res is equal to s1
Else
  ' Res is equal to s2
End If

Because the VB.NET compiler creates a single string object for all the string constants that have the same value, the same kind of optimization can be applied when constant strings are involved. Consider this code:

Click here to copy the following block
Dim color As String
' get a random color value, in the range 0 to 2
Select Case New Random().Next(0, 2)
  Case 0 : color = "Black"
  Case 1 : color = "White"
  Case Else : color = "Unknown"
End Select
' ...
' ...
' later we test the color
If color Is "Black" Then    ' instead of the = operator
  ' color is black
ElseIf color Is "White" Then  ' instead of the = operator
  ' color is white
Else
  ' color is unknown
End If


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.