Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Beginner
Feb 24, 2000

Create a Safer Mid Function

If you often write complex string-parsing and manipulation algorithms, the last thing you want is to add more checks to ensure your string positions are not negative. Avoid the hassle by using this function when you need to use Mid. It wraps around native VB functionality and handles this common error case:
 
Public Function FlexiMid(From As String, ByVal Start _
	As Long, Optional Length As Long = -1) As String
	If Start < 1 Then Start = 1
	If Length = -1 Then ' they want the rest of it
		FlexiMid = Mid$(From, Start)
	Else ' just give what they want
		FlexiMid = Mid$(From, Start, Length))
	End If
End Function
Once you paste this function into your program—I recommend a module, so you can access it from anywhere—you can use it as you would Mid. In fact, once I wrote this, I ran a search-and-replace on my project to start using it throughout.
Konstantin Komissarchik
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date