• No results found

Date subtraction

In document xHarbour Language Reference Guide (Page 30-42)

-Minus operator: add values, concatenate values and unary negative.

Syntax

<nNumber1> - <nNumber2>

<dDate1> - <dDate2>

<dDate> - <nNumber>

<cString1> - <cString2>

<hHash1> - <hHash2>

Arguments

<nNumber1>

<nNumber1> is a numeric value from which the value of <nNumber2> is subtracted.

<dDate1>

<dDate1> is a date value from which the date value <dDate2> is subtracted.

<dDate>

<dDate> is a date value from which <nNumber> days are subtracted.

<cString2>

<cString2> is a character string to add to the end of <cString1> after all trailing blanks from

<cString1> are removed.

Description

Depending on the data types of the operands, the Minus operator performs the following operations:

Unary negative sign

when the Minus operator precedes a numeric operand, it performs the equivalent of multiplying the operand by -1. This changes the operand's sign from plus to minus and vice versa.

Numeric subtraction

When both operands are numeric values, the right operand <nNumber2> is subtracted from the left operand <nNumber1> and the result is a numeric value.

Date subtraction

When the left operand is a date value and the right operand is a numeric, the value of <nNumber> is subtracted as number of days from <dDate>. The returned value is a date.

When both operands are of Date data type, the operator calculates the difference in days between left and right operand and returns a numeric value.

When the left operand is a Numeric value and the right operand is a Date, a runtime error is raised sine this is not allowed.

-String concatenation

If both operands are character data types, the value of <cString2> is joined to <cString1>, returning a character string. Trailing blanks in <cString1> are removed and appended to the end of the result string.

Hash operation

If both operands are hashes, a set-oriented operation is performed so that the resulting hash value contains unique keys of the left operand which are not contained in the right operand (refer to thehash operator).

Info

See also: %,*,**,+,--,/,= (compound assignment)

Category: Character operators,Mathematical operators,Operators

LIB: xhb.lib

DLL: xhbdll.dll

Example

// The example demonstrates variations with the - operator:

PROCEDURE Main

// Unary negative sign (also included is the positive sign)

? 1 - - 1 // result: 2

? 1 + - 1 // result: 0

? 1 + + 1 // result: 2

// Subtraction

? CtoD("01/20/2005") - 5 // result: 01/15/2005

? 10 - 2 // result: 8

// String concatenation

? "A " + "B " + "C " // result: "A B C "

? "A " - "B " - "C " // result: "ABC "

RETURN

--Decrement operator (unary): Prefix / postfix decrement

Syntax

-- <Variable>

<Variable>

--Arguments

<Variable>

<Variable> is the name of a memory or field variable of Numeric or Date data type. When

<Variable> is a field variable, it must be specified with an alias name or must be declared as field variable using theFIELDstatement.

Description

The decrement operator decreases the value of its operand by one. When used in an expression, the position of the operator is important for the result of the expression.

When the operator appears to the left of the operand (prefix notation), the operand's value is first decremented and then used in the expression.

When the operator appears to the right of the operand (postfix notation), the operand's value is first used in the expression and then decremented.

Info

See also: ++,-,:=,= (compound assignment) Category: Mathematical operators,Operators

LIB: xhb.lib

DLL: xhbdll.dll

Example

// The example demonstrates the use of the -- operator and // outlines the importance of prefix and postfix notation.

PROCEDURE Main

LOCAL nValue1 := 0 LOCAL nValue2

? nValue1 -- // result: 0

? nValue1 // result: -1

? -- nValue1 // result: -2

? nValue1 // result: -2

nValue2 := -- nValue1

? nValue1 // result: -3

? nvalue2 // result: -3

nValue2 := nValue1

--? nValue1 // result: -4

? nvalue2 // result: -3

RETURN

->

->

Alias operator (binary): identifies a work area.

Syntax

<aliasName> -> <fieldName>

(<nWorkArea>) -> <fieldName>

<aliasName> -> ( <expr,...> ) (<nWorkArea>) -> ( <expr,...> ) FIELD -> <fieldName>

MEMVAR -> <varName>

Arguments

<aliasName>

This is the symbolic alias name of a work area as specified with the ALIAS option of theUSE command.

<fieldName>

The field name whose value is retrieved from the work area specified with <aliasName>.

<nWorkArea>

Instead of using a symbolic name, a work area can be identified by its numeric ordinal position

<nWorkArea>. This value can be obtained by calling theSelect()function. The numeric work area must be enclosed in parentheses () when using it with the alias operator.

<expr,...>

One or more comma separated expressions to execute in the work area identified with

<aliasName> or <nWorkArea>.

FIELD

The FIELD keyword is a reserved alias name that identifies a field variable in the current work area.

MEMVAR

The MEMVAR keyword is a reserved alias name that identifies a memory variable of type PRIVATE or PUBLIC. An abbreviation of this alias is M->.

Description

The alias operator "points" to an unselected work area identified with its symbolic name <aliasName>

or its ordinal number <nWorkArea>. This allows for retrieving values of field variables from

unselected work areas or executing expressions in the specified work area. The alias operator implicitly selects the work area specified with the left operand and then executes the instructions of the right operand. When the operation is complete, the original work area becomes current again.

FIELD and MEMVAR are reserved alias names that allow for resolving name conflicts between memory and field variables. If memory and field variables with the same symbolic name exist,

unaliased variables are resolved as field variables. This behaviour is influenced by the compiler switch

->

/v. It is, however, strongly recommended to avoid identical variable names for memory and field vraiables, and to use the alias operator to identify a variable unambiguously.

Info

See also: DbSelectArea(),FIELD,FieldName(),FieldPos(),FieldGet(),MEMVAR,Select(),USE Category: Special operators,Operators

LIB: xhb.lib

DLL: xhbdll.dll

Example

// The example demonstrates usage of the alias operator.

// The last aliased expression generates a runtime error.

PROCEDURE Main LOCAL nArea

USE Customer ALIAS Cust // current work area

? FIELD->Lastname // result: Miller

nArea := Select() // work area number

SELECT 100 // select free work area

? Used() // result: .F.

? Cust->Lastname // result: Miller

? Cust->(Recno()) // result: 1

? (nArea)->Lastname // result: Miller

? (nArea)->(LastRec()) // result: 1576

? FIELD->Lastname // runtime error USE

RETURN

.AND.

.AND.

Logical AND operator (binary).

Syntax

<lCondition1> .AND. <lCondition2>

Arguments

<lCondition>

<lCondition1> and <lCondition2> are logical expressions to AND.

Description

The logical .AND. operator yields the result of a logical AND operation with left and right operand.

The result is only .T. (true) when the value of both operands is also .T. (true), in all other cases the result is .F. (false).

When multiple .AND. operators appear in an expression, the result is already determined when one operand has the value .F. (false). In this case, remaining .AND. operators are not evaluated for optimization reasons. This so called shortcut optimization can be switched off using the compiler switch /z.

Info

See also: & (bitwise AND),.OR.,.NOT.

Category: Logical operators,Operators

LIB: xhb.lib

DLL: xhbdll.dll

Example

// This example shows results of .AND. operations // using different operands:

PROCEDURE Main

? .T. .AND. .T. // result: .T.

? .T. .AND. .F. // result: .F.

? .F. .AND. .T. // result: .F.

? .F. .AND. .F. // result: .F.

RETURN

.NOT.

.NOT.

Logical NOT operator (unary).

Syntax

! <lCondition>

.NOT. <lCondition>

Arguments

<lCondition>

<lCondition> is a logical expression to logically negate.

Description

The .NOT. operator (alternatively "!" as an abbreviation) is a unary logical operator that negates the value of its operand. This yields the logical inverse of <lCondition>.

Info

See also: .AND.,.OR.

Category: Logical operators,Operators

LIB: xhb.lib

DLL: xhbdll.dll

Example

// The example shows the logical inverse of the operands:

PROCEDURE Main

? .NOT. (.T.) // result: .F.

? .NOT. (.F.) // result: .T.

? ! 1 > 2 // result: .T.

RETURN

.OR.

.OR.

Logical OR operator (binary).

Syntax

<lCondition1> .OR. <lCondition2>

Arguments

<lCondition>

<lCondition1> and <lCondition2> are logical expressions.

Description

The logical .OR. operator yields the result of a logical OR operation with left and right operand. The result is only .T. (true) when the value of one operand is also .T. (true), in all other cases the result is .F. (false).

When multiple .OR. operators appear in an expression, the result is already determined when one operand has the value .T. (true). In this case, remaining .OR. operators are not evaluated for optimization reasons. This so called shortcut optimization can be switched off using the compiler switch /z.

Info

See also: .AND.,.NOT.,| (bitwise OR) Category: Logical operators,Operators

LIB: xhb.lib

DLL: xhbdll.dll

Example

// This example shows results of .OR. operations // using different operands:

PROCEDURE Main

? .T. .OR. .T. // result: .T.

? .T. .OR. .F. // result: .T.

? .F. .OR. .T. // result: .T.

? .F. .OR. .F. // result: .F.

RETURN

/

/

Division operator (binary): divides numeric values.

Syntax

<nNumber1> / <nNumber2>

Arguments

<nNumber1>

<nNumber1> is the dividend.

<nNumber2>

<nNumber2> is the divisor.

Description

This operator returns the division of <nNumber1> by <nNumber2> as a numeric value.

Info

See also: %

Category: Mathematical operators,Operators

LIB: xhb.lib

DLL: xhbdll.dll

Example

// The example shows the use of the "/" operator:

PROCEDURE Main

? 3 / 3 // result: 1

? 3 /-2 // result: -1.50

? -3 / 2 // result: -1.50

? -3 /-2 // result: 1.50

? 3 / 0 // result: 0

RETURN

:

:

Send operator (unary): sends a message to an object.

Syntax

<object>:<message>[ ( [<params,...>] ) ]

Arguments

<object>

<object> is a variable holding an object to which the message is sent.

<message>

<message> is the name of an instance variable or method. When a method is called on the object, the message name must be followed with parentheses.

<params,...>

<params,...> is an optional comma separated list of parameters passed to a method.

Description

A class defines instance variables and methods for its objects. Accessing an object's instance variable or executing one of its methods requires the object to receive a corresponding message. This is the task of the Send operator (:).

To access an instance variable, the message is sent without following parentheses. As a result, the object returns the value of the instance variable that corresponds to the message.

Methods, in contrast, are invoked by adding parentheses to the message and passing an optional list of parameters to the method. Parameters are listed inside the parentheses.

The double send operator :: has a special meaning within the context of the program code of methods.

It is an abbreviation for self:, i.e. :: sends messages to the object that executes a method.

Note that only messages are understood by an object that are declared in the object's class. In addition, only those instance variables and methods are accessible that are visible in the current program context.

If either an instance variable/method is not declared or currently not visible, the message is not understood by the object and a runtime error is generated.

Info

See also: CLASS,DATA,METHOD (Implementation) Category: Special operators,Operators

Header: hbclass.ch

LIB: xhb.lib

DLL: xhbdll.dll

Example

// The example demonstrates the use of the single and // double send operator. Note that the double send operator // is only used within the program code of a method.

:

PROCEDURE Main

LOCAL obj := MyClass():new( "Hello World" )

obj:display() // shows: Hello World

obj:value := "Hi there"

obj:display() // shows: Hi there

RETURN

CLASS MyClass EXPORTED:

DATA value

METHOD new( cString ) CONSTRUCTOR METHOD display

ENDCLASS

METHOD new( cString ) CLASS MyClass ::value := cString

RETURN self

METHOD display CLASS MyClass

? ::value RETURN self

:=

:=

Inline assignment to a variable.

Syntax

<Variable> := <Expression>

Arguments

<Variable>

<Variable> is the name of a variable of any type.

<Expression>

<Expression> is any valid expression whose result is assigned to <Variable>.

Description

The := operator assigns the value of <Expression> to a variable. It is the preferred assignment operator since it can be used to initialize variables within their declaration statements. This is not permitted with the simple assignment operator "=".

If the variable does not exist when the assignment operation is processed, a PRIVATE variable is automatically created and gets assigned the value of <Expression>.

Info

See also: ++,--,= (assignment),= (compound assignment) Category: Assignment operators

LIB: xhb.lib

DLL: xhbdll.dll

Example

// The example demonstrates the use of the inline assignment // operator in various situations:

PROCEDURE Main

LOCAL dDate := StoD("20050701") // initializes variables LOCAL nMonth := Month( dDate ) // in declaration statement

nDay := 12 // creates

nYear := nPreviousYear := 2000 // PRIVATE variables // assignment within expression

IF (nMonth := Month(dDate)) == 7

? "July"

ENDIF

? nMonth // result: 7

// note the removed parentheses and

// different return value of the expression IF nMonth := Month(dDate) == 7

? "July"

ENDIF

<

<

Less than operator (binary): compares the size of two values.

Syntax

<Expression1> < <Expression2>

Arguments

<Expression>

The values of <Expression1> and <Expression2> must have the same data type and are compared.

Description

The "less than" operator compares two values of the same data type. It returns .T. (true) when the left operand is smaller than the right operand, otherwise the return value is .F. (false).

Only the simple data types Character, Date, Logic, Memo and Numeric can be compared. The complex data types Array, Code block, Hash, Object and the value NIL cannot be used with the "less than"

operator.

In document xHarbour Language Reference Guide (Page 30-42)