Tuesday, October 22, 2019
Using the ToString Method in Visual Basic .NET
Using the ToString Method in Visual Basic .NET The ToString method is one of the fundamental methods in the root of the entire .NET Framework. That makes it available in every other object. But, since its overridden in most objects, the implementation is often very different in different objects. And that makes a number of tricks with ToString possible. Displaying the Bits in a Number If you have a series of bits in, for example, a Char variable, this tip shows you how to display them as 1s and 0s (the binary equivalent). Suppose you have ... Dim MyChar As Char a character selected at random just to get a series of eight bits MyChar $ The easiest way I know of is to use the ToString method of the Convert class. For example: Console.WriteLine(Convert.ToString(Convert.ToInt16(MyChar), 2)) This gives you ... 100100 ... in the Output window. There are 36 overridden methods of the ToString method in the Convert class alone. Click Here to display the illustrationClick the Back button on your browser to return In this case, the ToString method does a radix conversion based on the value of the second parameter which can be 2 (binary), 8 (octal), 10 (decimal) or 16 (hexadecimal). Formatting Strings With the ToString Method Heres how to use ToString to format a date: Dim theDate As Date #12/25/2005# TextBox1.Text theDate.ToString(MMMM d, yyyy) And adding culture information is easy! Suppose you want to display the date from a structure in, say, Spain. Just add a CultureInfo object. Dim MyCulture As _ Ã Ã Ã New System.Globalization.CultureInfo(es-ES) CultureDateEcho.Text _ Ã Ã Ã theDate.ToString(MMMM d, yyyy, MyCulture) The result is: diciembre 25, 2005 The culture code is a property of the MyCulture object. The CultureInfo object is an example of a provider. The constant es-ES isnt being passed as a parameter; an instance of the CultureInfo object is. Search the VB.NET Help system for CultureInfo to see the list of supported cultures.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.