TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET VarType Function (VariantType Enum)

Use the VarType built-in Function and the VariantType Enum.
VarType determines the type of a variable in VB.NET. This variable could be of type Object or a more derived type. With the VarType function and the VariantType enumeration, we test the type of variables such as Integer types.Integer
Example. First, this program uses a Dim of type Integer. It then calls VarType on this variable. We print the String representation of the VariantType result. We show the GetType function, which is separate.

Finally: We test the VariantType variable in an If-statement. We display a helpful message.

VB.NET program that uses VarType function Module Module1 Sub Main() ' Integer. Dim value As Integer = 5 ' Get VarType. Dim type As VariantType = VarType(value) ' Write string representation. Console.WriteLine(type.ToString()) ' Show GetType method. Console.WriteLine(value.GetType().ToString()) ' You can check the VariantType against constants. If type = VariantType.Integer Then Console.WriteLine("It's an integer!") End If End Sub End Module Output Integer System.Int32 It's an integer!
Change. What happens if you change the "Dim value As Integer" line to "Dim value As String"? The result of the program will be different. Also, the If-statement will not evaluate to True and the final line will not be printed to the output.
Changed line in program: VB.NET Dim value As String = "cat" Output String System.String
We see that the VarType can be used on a variable of type Object. Add another statement that casts the first variable to the Object type. VarType then resolves the Object variable's type to its most derived type (String).
New lines in program: VB.NET ' Integer. Dim a As String = "cat" ' Cast to Object. Dim value As Object = a Output String System.String
Discussion. What happens inside the VarType function? The function basically calls the GetType function, which we also used in the above program. Then it converts the result of GetType to a String representation.Strings
Summary. The VarType function is based on the GetType function, but it presents a more Visual Basic oriented type system to you. It will return the best type for variables such as Integers, which can simplify your VB.NET programs.

Note: It is most useful when you must resolve the most derived type from an Object or other base type.

© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf