Home > Articles > Programming > Windows Programming

Using Strings in Visual Basic .NET

  • Print
  • + Share This
Understanding types in Visual Basic .NET and how they are used is important in designing objects that best use what .NET provides. In this article, excerpted from Sams Teach Yourself Object-Oriented Programming with Visual Basic .NET in 21 Days, Richard Simon discusses the string data type in Visual Basic .NET.
This article is excerpted from Sams Teach Yourself Object-Oriented Programming with Visual Basic .NET in 21 Days (Sams Publishing 2002, ISBN 0672321491).
This chapter is from the book

Visual Basic .NET has a wide range of types defined, by default, to use in your applications. It offers the traditional types for handling numbers and strings, and other types are provided by .NET to represent objects and collections.

Understanding the types in Visual Basic .NET and how they are used is important in designing objects that best use what .NET provides. It's also important to understand that all data types used in .NET languages are common between the different .NET programming languages so that objects can be shared among them.

Using Strings

The String data type is actually implemented as a class, not as a structure like the numeric data types. It also has a variable size depending on the implementing platform. For example, some platforms implement a String with 2 bytes per character, whereas others implement it as a single byte per character.

The major difference between using String in previous versions of Visual Basic and in Visual Basic .NET is that the String can't be declared with a fixed length. When a value is assigned to the String, the value's length determines the String's length.

Because strings are implemented by the .NET Framework, they also have different internal characteristics than before. An instance of a String can't be modified after it's created; even though it appears you are modifying the value, in actuality, a new instance of a String is created containing the modification. For example, consider the following code segment:

Dim SL As String = "Books"

SL = SL.Remove(4,1)
SL = SL + "s"

The first statement declares a String variable and assigns a value of "Books". This statement, in turn, allocates the appropriate memory in the String to store the value. The second statement uses the Remove() method to remove the s from the end of the value. It does so by creating a new instance of a String with the modified value and returning a reference to which the SL variable is set. The third statement appends the s back to the end of the value stored in SL. This statement has the same effect as calling a method within String because it also returns a reference to a new instance of a String that contains the modified value.

If you want to actually modify the value within a String, you can use the StringBuilder class. This class allows you to modify a string by inserting and removing characters.

Working with Dates

The Date data type within Visual Basic .NET uses the .NET DateTime structure. It can represent dates and time values ranging from 12:00:00 AM, 1/1/0001, to 11:59:59 PM, 12/31/9999. The time values have a precision of 100 nanoseconds, which make up one tick.

A particular date and time is made up of several ticks since 12:00:00 AM January 1, 0001. As you can imagine, the number to store a date and time can get very large. The DateTime structure can easily handle this large number because it's stored as a 64-bit value.

Calculations with Date variables are straightforward considering that they are represented by an integer value. Therefore, subtracting two Date variables yields the difference in ticks between the two dates. Because ticks aren't too useful without a conversion, the DateTime structure implements different methods for adding and subtracting dates. The results are returned as a TimeSpan structure, which provides methods to interpret the result of a Date calculation.

You assign Date variables to a literal value by enclosing the date literal string within pound (#) characters. For example, a literal value of #12/12/2001 10:52:30 PM# is valid to assign to a Date variable. The date literal should be in a standard date format, such as the one in the previous example. If the date portion is eliminated, January 1, 0001, is used as the date. If the time is eliminated, midnight is the default.

Creating a date string with the ToString() method provides you the flexibility to specify the formatting for the date and time. You can create custom-formatted dates by specifying a formatting string made up of a combination of the values. You also can use one of the predefined date formats. Refer to the .NET online reference for information on the formatting patterns. The following code segment shows how to use the ToString() method, and Figure 1 shows the onscreen result:

Dim Dt As Date = "#12/12/2001 10:52:30 PM#"
MessageBox.Show(Dt.ToString("D"))

Figure 1 Results of the ToString() method.

  • + Share This
  • 🔖 Save To Your Account