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
Apr 8, 1999

Use TypeName Instead of TypeOf...Is

To write reusable routines that work with multiple types of controls, test the control type using the TypeName function in place of the TypeOf...Is statement. For example, take a look at this routine-you can reuse it in another project only if you also add the RichTextBox control to the Components list:
 
' save the selected text to an open file
' works with TextBox and RichTextBox controls
Sub SaveSelectedText(ctrl As Control, filenum As Integer)
	If TypeOf ctrl Is TextBox Then
		Print #filenum, ctrl.SelText
	ElseIf TypeOf ctrl Is RichTextBox Then
		Print #filenum, RichTextBox1.SelRTF
	End If
End Sub
To avoid this problem and gain additional benefits such as the ability to use a Select Case block, use the TypeName function instead:
 
Sub SaveSelectedText(ctrl As Control, filenum As Integer)
	Select Case TypeName(ctrl)
		Case "TextBox"
			Print #filenum, ctrl.SelText
		Case "RichTextBox"
			Print #filenum, RichTextBox1.SelRTF
	End Select
End Sub
Francesco Balena
 
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