Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Aug 6, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Why Does The Typename Of A Form Return The Form's Name, And Not Its Type?

You create a routine that uses the Typename() function to find out the type of an object, and based on that type, performs certain actions. However, when used in a Form object, the value returned by Typename() is not the generic object class "Form." It is, instead, the specific object class "Form1." Try running this sample:
 Private Sub Form_Load()
	Dim frm as Form
	set frm = Me
	Debug.Print TypeName(frm), frm.Name 
	'<< prints: Form1 
End Sub
You expect TypeName(frm) to resolve to Form instead of Form1 but it doesn't. That is because the type of the object, in this case, is the form class itself, Form1. That you assigned it to a Form object does nothing except to prevent you from calling any of the Form1 objects methods and properties directly in code. Similarly, a control never gives a type name of "Control." Instead, the type name is always something like "CommandButton" or "PictureBox." Interestingly, while the TypeName function returns the most specific name, the If-TypeOf syntax will match either the generic class type or the specific object class. In other words, both of these If statements would return True:
 Set frm = Me
If TypeOf frm Is Form1 Then Debug.Print "Form1"
If TypeOf frm Is Form Then Debug.Print "Form" 
'<< both stmts are true
A. Nicklas
Comment and Contribute






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



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