Using Data in Programs T
Total Page:16
File Type:pdf, Size:1020Kb
Using Data in Programs HUS FAR IN THE VISUAL BASIC PROJECTS, we have used objects, Tmodified the properties and exercised some methods. We have used events to provide action in the projects. However, we have not used data except for text strings, and have not made any mathematical calcu- lations. That is about to change. We are now going to enter numeric data in the project, and perform some calculations. This will give us much more capability to do something useful than we have done so far. Variables and Constants Broadly speaking, data can be divided into two basic categories: (1) data that is changeable (or will likely change), and (2) data that will not change. In computer-eze, data that either can change or will likely change is called a variable, while data that does not change is called a constant. Most of the data you will deal with will be variables, but constants are also around us. Visual Basic treats them the same in some respects but has important differences. 65 Constants are values that have been given a specific definition, and nothing you can do will change it. For example, there are 7 days in a week, 24 hours in a day, 60 seconds in a minute, 12 months in a year, 12 inches in a foot, 5,280 feet in a mile, and so forth. Nobody can change them, and they are “constant” in their definition. A variable is data that will or could change. Examples are the number of work hours in a work week, rate of pay, scores on exams, price of a movie ticket, number of people attending an event, and so forth. Variable and Constant Names To make our lives easier, we use words to represent both variables and constants to describe the values they represent. If we say “day” we all know it is the same thing as 24 hours, or 1440 minutes. Or, a “mile” is the same thing as 5,280 feet or 1760 yards. Both day and mile are names for constants, and we all learned long ago what the actual values are. On the other hand, if I say “pay-rate” we don’t know what value that represents unless we define it. After it has been defined (or described), we can then use pay-rate the same way we use day or mile to describe a numeric We must amount. In computer-eze, we use pay-rate, day, or mile as a placeholder use an for a numeric value. We call that placeholder a variable or constant name. appropriate container Likewise, in math and Algebra we learned that you can use about to hold any letter to represent any value as long as it is defined someplace. For different example, we can say that “A = 15” and then we can use the letter “A” to types of represent the value 15. In this case, “A” is a variable name for the value data. 15. Visual Basic uses variable names in the same way as Algebra, or as English does for common, fixed values. Visual Basic uses variable names to represent memory locations inside the computer. Those memory locations are containers that hold data. We are normally not concerned where the exact location is in memory, but we need to know what the location is called. We refer to these memory locations as Identifiers. To help you conceptualize a memory identifier, think of an identifer as a container. We are used to using various types of containers to contain different items. For example, we have cardboard boxes, paper bags, metal or plastic buckets, glass bottles, fiber baskets, air-tight tanks, etc., and they hold different items. They are all containers. You would not think of put- ting a liquid in a basket or paper bag, but could put solid items in a basket. We know a liquid needs a bottle, and sand needs a bucket. We must like- wise use an appropriate container to hold different types of data. 66 Data Types Data includes both numeric data and alphabetic data. However, we all learned long ago that these two types of data are not the same. We can add and subtract with numbers, but not with alphabetic characters. Num- bers are not all the same either. Some numbers have decimal points and others do not. Numbers with decimal points are called Decimals, and whole numbers (numbers without decimals) are called Integers. Alphabetic characters are called Strings. To be successful in programming, you need to understand that the data types are different, and you need to know the type of data you are using. Visual Basic recognizes 12 different types of data, but normally only four are significantly different and important at this level. The four data types you need to understand for this class are Integer, Decimal, String, and Boolean. They are described below. Table 4.1 Common Data Type Description Use Data Types in Integer Whole numbers Counting and computing Visual Basic Decimal Numbers with decimals Computations String Any character Text characters, punctua- tion, and numbers not used in computations Boolean On or off state True or False values Even though all data is contained electronically in the computer, its identifiers are different sizes, and we need to assign the right type of identifier to the appropriate data. The different data types need different amounts of memory space to be stored properly. For example, an Integer requires 4 bytes of storage while a Decimal number requires 16 bytes of storage. A Boolean value only needs 2 bytes of storage while String data needs a varying amount of storage space. The other data types recognized by VB include the following: Table 4.2 Other Data Type Use Data Types in Single Single-precision numbers with 6 digits of accuracy Visual Basic Double Double-precision numbers with 14 digits of accuracy Long Very large whole numbers Short Small whole numbers Date Date values through Dec 31, 9999 Char Single Unicode characters (including non-English language characters) Byte Binary data Object The default data type; could be used for any data 67 Each of the above data types has certain restrictions. For example, the Short data type is restricted to Integer values ranging from -32,768 to +32,767 while the Integer data type allows values to plus or minus 2.1 billion. If your Integer value will be larger than 2.1 billion, you must use the Long data type. Likewise, most decimal applications fit within the Decimal data type. However, if you need very high levels of decimal precision, then use Single or Double. If you are planning a trip to Mars, then you probably should use Double to get the highest degree of precision. If you are counting grains of sand on the beach, you probably should use Long. Byte is used to transfer binary data between various programming languages, such as transferring from Visual Basic to C++ or C#. The Object data type should only be used if the type of data is unknown, or is likely to change in data type frequently. It is the least efficient data type available, and should only be used when nothing else works. Technically, the Single data type is more efficient than the Decimal data type. However, Single, Double, and Long are usually used in scientific applications while business-use usually uses Integer and Decimal data types. Refer to Appendix A for details on all data types. Declaring Variable Names The most important thing about a variable name is that it must be representative of the data it contains. You cannot assume that you will remember what the value of the data actually is, so the name must identify it for you. You might not even know what the value is. For those reasons, the variable name “X” is the worst name possible -- it doesn’t describe anything. Visual Basic has some “Naming Rules” to keep in mind when naming variables or constants: Names can contain alphabetic characters, numbers, and the underscore, but no other punctuation. Names must begin with an alphabetic character. Names cannot contain a space or a period. Names cannot be a reserved word, but reserved words can be contained inside a name, such as ClassFriend. (A reserved word is a word that has a special meaning in Visual Basic, such as Sub, Private, Dim, Close, etc. Refer to Appendix B for a partial list of reserved words.) Names are not case sensitive. Names such as Quantity, quantity, and QUANTITY are all considered the same variable name. There is no real limit to the number of characters used. 68 Most experienced programmers like to use prefixes to identify the data type in variable and constant names. Variable or constant name pre- fixes are similiar to prefixes used to identify controls. A suggested listing of prefixes for variables and constants are as follows: Table 4.3 Variable and Data Type Prefix Example Constant Name Integer int intCount Prefixes Decimal dec decNetAmount Boolean bln blnMale String str strAcctNum As with control prefixes, variable name prefixes should be lower case with the main part of the name starting with a capital letter. Using prefixes to designate the data types is highly encouraged to avoid “Type- Mismatch” errors, such as trying to compute a zip code. The important thing is the spelling and capitalization used because it will appear that way throughout the program. It is also important to assign the right type of prefix as it is named. Consider the following when assigning a data type: If a value could be used in calculations and does not have a decimal, use the Integer data type.