Scrigroup - Documente si articole

     

HomeDocumenteUploadResurseAlte limbi doc
AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows
WordXml

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

Building commands, functions, and expressions

Fox pro



+ Font mai mare | - Font mai mic



Building commands, functions, and expressions

Now that you're comfortable entering and executing commands in the Command window, it's time to look at how commands, functions, and expressions are built in VFP.

Commands



A command is a string of text that instructs VFP to perform an action. Most commands can be executed from the Command window and from within a program, but some, such as logic structure keywords like DO CASE or ENDIF, can be used only in programs. Commands consist of a required keyword or phrase followed by one or more optional keywords and phrases. Commands can be typed in any combination of case, and many keywords can be abbreviated to the first four letters. As the language has grown, this practice has fallen out of style both because the first four letters are not unique in more and more cases, and because this capability doesn't work with the object-oriented notation used with forms and classes. The order of optional keywords generally doesn't matter (there are one or two annoying exceptions documented in Help), and so all of the following commands are identical:

WAIT window nowait 'This is a message'
wait wind nowa 'This is a message'
wait WIND nowa 'This is a message'
wait wind 'This is a message' NOWAIT

The maximum length of a command is 8,192 characters, and because most monitors available today won't display an 8,000-character string on a single line, it's common practice to split a long command into multiple lines, ending each partial line with the VFP "line continuation" character-the semicolon. This makes even more sense when you break up commands or expressions into logical segments. The first command in each of the next two examples is an illustration of a poor way to display a command; the second is a much more readable, and thus recommended, method:

select cNameFirst, cNameLast, cNameOrg, dLastCall, cTypeRep ;

from COMPANY, REPS ;

where nRegionID inlist(1,2,3,4,5) ;

order by cTypeRep, cNameLast into cursor csrNew

The Fundamentals of Visual FoxPro 6.0

select cNameFirst, cNameLast, cNameOrg, dLastCall, cTypeRep ;

from COMPANY, REPS ;

where nRegionID inlist(1,2,3,4,5) ;

order by cTypeRep, cNameLast ;

into cursor csrNew

m.lFinalAmt = iif(m.lWasSold, ;

nJobAmt * m.nMarkup + iif(m.lWasInstalled, m.nAmtInstall, 0), ;

m.nAmtProposed * m.nFakeChargeup)

m.lFinalAmt = ;

iif(m.lWasSold, ;

nJobAmt * m.nMarkup + iif(m.lWasInstalled, m.nAmtInstall, 0), ;

m.nAmtProposed * m.nFakeChargeup)

You can use the semicolon to continue commands across multiple lines in the Command window; while useful, it can also cause consternation if you're not paying attention. Typing a command, a semicolon, and then pressing the Enter key will not generate any results-because VFP thinks you're in the middle of a command, and it's waiting for you to finish the command with another line that doesn't end with a semicolon.

Variables

A variable is a temporary location in memory used to store a piece of data during interactive use and program execution. Using variables in VFP is remarkably easy and extraordinarily flexible. First of all, unlike other languages, VFP takes care of all memory management. You don't have to declare memory space, make sure your variables are sized properly, or release them in order to free up memory for a future operation. VFP does all this for you. VFP also does not require you to define the data type of a variable-which is both a blessing and a curse. It's a blessing because it's considerably less work; it's a curse because it's easy to make programming errors based on the assumption that a variable is the wrong data type. For those of you used to strongly typed languages, it's kind of like getting out on the high wire without a safety net, although with somewhat less serious consequences. Fortunately, our community has adopted a number of standard techniques to reduce the risks of being weakly typed.

Variable names can be up to 254 characters long, although in practice most programmers get tired of using variable names longer than 120 or 130 characters. (Okay, I'm just kidding here-the point is that you're not limited to using cryptic names because of name-length restrictions.)

To create a variable (often called a "memory variable" in the VFP world, although I don't know why, since there wasn't ever such a thing as a "hard disk variable"), simply enter in the Command window a command like the following:

m.cX = 'Herman'
m.dBirth =

m.nAmount = 99.50
m.iCounter = 45
m.lLovesRockAndRoll = .t.

At this point, you've declared five variables, each of different types. The first is a character string, the second is a date value, the third is a numeric value, the fourth is an integer, and the fifth is a logical variable.

You'll notice that I didn't type the entire variable in the same case. Instead, for increased readability, each "word" in the variable name (except for the first character after the "m.", which I'll get to in a second) was capitalized. While "m.dBirth" and "m.dbirth" may seem to be pretty much the same, as names get long, it's easy to make a mistake. Consider:

m.namountinstallationtaxdefault

versus

m.nAmountInstallationTaxDefault

The first one kinda makes you think that Mary Poppins and Bert should include it in a song, doesn't it?

Note that the first character of each variable name is a mnemonic for the type of data it holds. Visual FoxPro doesn't enforce this-you could just as easily enter commands like:

m.cX = 1234.56789 m.dBirth = 'January 12'm.nAmount = .f.

and VFP wouldn't be any the wiser. The variable, m.cX, even though it starts with a "c", would be a numeric variable, m.dBirth would be a character variable, and m.nAmount would be a logical variable.

However, this convention helps you from writing code like so:

m.nAmount = m.nAmount + 'No data entered'

Even though you might be new to VFP, you can already guess that this line would cause an error. The important thing to remember is that Visual FoxPro will let you enter this line and will compile it without complaining. For all VFP knows, you had already assigned another text string to the original m.nAmount variable, with the end result that the command above would just be concatenating two text strings.

The other component of a variable name that might be unfamiliar to you is the "m." prefix. Strictly speaking, this prefix is not part of the variable name. Instead, it explicitly identifies the name as a variable; if a variable has the same name as a field in an open and currently selected table, using the name without specifying the prefix will always return the value in the field. Preceding the variable name with "m." ensures that the value will be that of the variable.

The Fundamentals of Visual FoxPro 6.0

Suppose you have a table, COMPANY, with a field named cNameComp, and a single record where the value of the cNameComp field is "Benefit Evaluations, Inc." The following line will assign the string "Interactive Displays" to the memory variable m.cNameComp:

m.cNameComp = 'Interactive Displays'

However, entering the following command in the Command window will not return
"Interactive Displays" but rather "Benefit Evaluations, Inc." because that's the value in the
table:

? cNameComp

Because you precede the name with an "m.", the following command will return "Interactive Displays" instead:

? m.cNameComp

If this theory is uncomfortable, turn down the corner of this page so you can come back to it once you're used to working with tables. If you let this idea go by without totally understanding it, your future will hold some long debugging sessions.

Operators

Visual FoxPro has all of the standard operators you'd expect in a programming language, including addition, subtraction, multiplication, division, exponentiation, comparison (>, <, =), modulus (%), and "contained in" ($). String concatenation is performed with the plus (+) and minus (-) operators. The minus operator will trim trailing spaces from the expression preceding the minus, while the plus operator will not.

m.cName = 'Herman' + ' ' + 'Werke'
m.nAmount = m.nAmount - 100
m.nSquareFeet = m.nSide ^ 2
m.nRemainingDonuts = m.nNumDonuts % m.nNumPeople
m.lSteveIsIn = 'Steven Tyler' $ 'Steven Tyler, Joe Perry, Brad Whitford, Tom
Hamilton'

Functions

A function is a predefined Visual FoxPro keyword that performs an operation and returns a value. In most cases, the function will take one or more parameters, some of which may be optional. Parameters are included in the parentheses that follow a function, and multiple parameters are separated by commas. Parameters can be constants, variables or other expressions; there are limits to the number of levels that can be nested, but for most practical purposes, you shouldn't worry about them.

All limits can be found in the Help file. Open Visual FoxPro Documentation,

Using Visual FoxPro, Programmer's Guide, Appendix, and then read the "Visual FoxPro System Capacities" help topic.

Functions can be categorized into more than a dozen broad areas, including string manipulation, date and time handling, mathematical operations, database, table and field mechanisms, file management, low-level file handling, and array and memory variable manipulation.

Specific functions are covered in detail later in this chapter.

Expressions

An expression is the result of the combination of one or more constants, variables, and functions, all combined with operators. The general syntax of an expression is of the form:

expression operator expression

where each side of the operator must be of the same type. In other words, you can add a string to a string or compare two dates to determine which is more recent, but you can't put a logical on one side and a number on the other.

The following lines each represent a single expression:

'I am a string'123.45 100 + 200 m.nAge * 12m.nAmount > 999.99 str(m.nAmount,10,2)'Your account balance is ' + str(m.nAmount,10,2)

The first two expressions are character and numeric constants, respectively. The third is an arithmetic expression that evaluates to the numeric value 300. The fourth is another arithmetic expression that multiplies a memory variable and a numeric constant. The fifth appears to be a comparison of two expressions, but the whole string evaluates to a logical value-which might be either true or false, depending on the value of m.nAmount.

The second to the last expression uses a function to convert a numeric variable to a text string (the 10 and the 2 control how long the text string will be and how many decimal places will be in the return value). The last expression concatenates a character constant with the function expression from the previous line.

You can also use the AND, OR, and NOT keywords to combine and negate multiple expressions. These expressions are called compound expressions. For example, the OR operator will return a logical true if one side of an expression or the other is true:

? (m.nAge > 40) or (m.cHairColor = 'Grey')

The Fundamentals of Visual FoxPro 6.0

You could store the value of this compound expression to a memory variable just as easily:

m.lIsAFossil = (m.nAge > 40) or (m.cHairColor = 'Grey')

The AND operator will return a logical true only if both sides of an expression are true:

m.lIsASexSymbol = (m.cHairColor = 'Bald') and (m.lHasPocketProtector = .t.)

I'd like to point out that the parentheses around each of the two expressions aren't required-I've included them to enhance readability. Second, you don't have to equate a logical expression with a .t. or .f., because the logical expression itself is-yes-a logical expression and can be evaluated. Thus, the following expression is just as valid:

m.lIsASexSymbol = m.cHairColor = 'Bald' and m.lHasPocketProtector

Finally, Visual FoxPro short circuits the evaluation of AND and OR expressions so that only the first condition may be evaluated if that's all that's necessary. For example, if the first part of an AND expression is false, the rest is ignored, since whether the rest is true or false doesn't matter. Similarly, if the first part of an OR expression is true, the rest is ignored because, again, it doesn't matter.

All the parts of an expression must be of the same type. When concatenating multiple expressions with AND or OR, each of the parts must evaluate to a logical value. It's common to run into a "data type mismatch" or an "operator/operand type mismatch" error message-this is the case when part of an expression doesn't match the rest. To resolve it, break down each part separately and make sure it's returning the correct type of data.



Politica de confidentialitate | Termeni si conditii de utilizare



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 847
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved