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 ToString Overrides Example

Use an Overrides Function for ToString. Allow a Class to print information when ToString is called.
ToString is often useful in VB.NET programs. It allows us to provide a custom ToString function on a VB.NET class. By using an Overrides ToString function, we specify how an instance renders itself as a String.ClassStrings
Example. This program introduces the Perl class. This class, by default, inherits from the Object class. On the Object class (not shown) there exists an Overridable Function ToString.

Class: The Perl class here provides the implementation that overrides ToString.

Object: When Perl is used as an Object, its custom ToString implementation is still used.

New: When we construct the Perl instance, its fields are set to 2 and 3. It is passed to the Console.WriteLine subroutine as an Object.

Console: In Console.WriteLine, the ToString function is called on that Object. The Overrides Function ToString implementation is used.

Console
VB.NET program that overrides ToString Module Module1 Class Perl Dim _a As Integer Dim _b As Integer Public Sub New(ByVal a As Integer, ByVal b As Integer) _a = a _b = b End Sub Public Overrides Function ToString() As String Return String.Format("[{0}, {1}]", _a, _b) End Function End Class Sub Main() Dim p As Perl = New Perl(2, 3) Console.WriteLine(p) End Sub End Module Output [2, 3]
Discussion. In the VB.NET language, we see Overridable functions and Overrides functions. These are semantically equivalent to virtual and override methods in the C# language. They have the same meaning.

Opinion: The term "overridable" may be a clearer description of a virtual function. The term virtual is less obvious.

FunctionSub
Summary. We looked at how you can provide the ToString function in your VB.NET program. This function returns a String. You can use any logic you wish to construct the String. It should use the Overrides modifier.

And: This will cause it to be used as the implementation for less derived type references such as Object.

© 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