5.1. String Variables

You already know that data used in a computer program falls into two broad categories: 1) numeric data and 2) textual data. Chapter 3 presented the value types that are used to store numeric data in a program. String variables are used to store textual data.

Data entered by a user into a program's textbox object is textual data. However, if you want to manipulate the user's input, you must first convert the textual data to numeric data by using the data type's TryParse() method. For example, if you want to convert the content of the txtOperand1 textbox object into an int data type, you use the following statement:

flag = int.TryParse(txtOperand1.Text, out val);

This statement converts the textual data typed into the txtOperand1 textbox object into an int data type, storing the integer result in variable val. What you may not have realized is that the Text property of the textbox object is actually a string variable! The purpose of the TryParse() method is to convert the string data into a numeric data type.

In this chapter, I want to discuss string variables as places to store textual data. Textual data, as a general rule, are not meant to be manipulated mathematically. Instead, you want to keep the data in a text format by storing it in a string variable. It's common to use textual data for storing names, addresses, phone numbers, zip codes, customer IDs, and so on. Note that some textual data actually are pure numbers, such as a zip code. How do you decide ...

Get Beginning C# 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.