Skip to content

Move Command

See Also: Understanding File Buffers and DDO Field Buffers, Add, Decrement, Increment, Subtract, SFormat, Append, Expression Syntax

Purpose

To assign a value to a variable.

Syntax

Move {expression} to {variable}

What It Does

The Move command allows you to copy data from any constant, field, window, or variable into any other field, window, or variable. The arguments may be of different types and/or classes. All necessary conversions will be done automatically, except where a value is "impossible" for the variable type. Examples of "impossible" values are 12/31 for a Date, or A1587 for a Number. No combination of characters is impossible for a String type variable.

Where data is impossible for the type of variable, DataFlex will declare an Error 51 "Bad format of expression (operand)", or other error explicitly naming the incompatibility and continue with execution. "Impossible" values for Numbers and Dates result in a value of zero in the variable.

Where data is too long for the declared length of the variable, the moved data is simply truncated on the right, and execution continues without error. Where this condition is an undesired possibility, it should be trapped with appropriate commands. On the other hand, this function represents a valid and often convenient way of accomplishing truncation where it is desired.

Examples

Example 1

Number nNum
String sString
Move "California" to sString

In this example, the String constant "California" is moved to the string variable sString. If that variable were other than a string type variable, this command would report an error at runtime, because the value "California" cannot be a number.

Example 2

String sActive
Move Vendor.Name to sActive

In this example, the value of Fieldname in the database table Vendor is moved to the string variable sActive.

Example 3

Move 07/12/1999 to Sales.Order_date

In this example, the literal date 07/12/1999 is moved to the field order_date in the database table Sales. If Field order_date were not of type Date, this command would trigger an error.

Example 4

Move (Sales.Ord_date + 30) to Sales.Due_date

In this example, the value of Field due_date in the database table Sales, plus 30, is moved to Field due_date in the same table.

Example 5: Initializing Struct Variable Members

In the example below, Move is used to initialize members of the myBillingAddress struct variable.

Struct tUSAddress
    String sFirstName
    String sLastName
    String sAddressLine1
    String sAddressLine2
    String sCity
    String sState
    Integer iZipCode
End_Struct

Procedure CreateBillingAddress
    tUSAddress myBillingAddress
    // initialize myBillingAddress
    Move "John" to myBillingAddress.sFirstName
    Move "Smith" to myBillingAddress.sLastName
    Move "Data Access Worldwide" to myBillingAddress.sAddressLine1
    Move "14000 SW 119 Ave" to myBillingAddress.sAddressLine2
    Move "Miami" to myBillingAddress.sCity
    Move "FL" to myBillingAddress.sState
    Move "33186" to myBillingAddress.iZipCode
End_Procedure

Example 6: Copying Struct Variable Members

Assume that the example below is a continuation of the sample above, using the same struct declaration for tUSAddress. Here, Move is used to copy the value of myBillingAddress to myShippingAddress. Using Move to copy struct variables uses a deep memberwise copy operation to copy each struct variable member value from the first struct variable to the second struct variable.

Procedure CreateShippingAddress tUSAddress myBillingAddress
    tUSAddress myShippingAddress
    Move myBillingAddress to myShippingAddress
End_Procedure

Example 7: Re-initializing Struct Variable Members

There is a quick way to re-initialize array variables to their original (blank) values. Simply move an un-initialized array variable of the same type:

Struct tTest
    Integer iTest
    String sTest
End_Struct

Procedure Test
    tTest localTest blankTest
    // initialize localTest with values
    Move 104 to localTest.iTest
    Move "Fred" to localTest.sTest
    // re-initialize localTest elements to their original (blank) values
    Move blankTest to localTest
End_Procedure

The same can be done with struct properties:

Property tTest pMyTest

Procedure ReinitializepMyTest
    tTest blankTest
    Set pMyTest to blankTest
End_Procedure

Example 8: Initializing Array Variable Members

In the example below, Move is used to initialize members of the iValues array variable. You can also use FillArray to initialize array values.

Procedure Example
    Integer[10] iValues
    Integer i
    For i From 0 to 9
        Move (i + 1) to iValues[i]
    Loop
End_Procedure

Example 9: Initializing Dynamic Array Members

In the example below, Move is used to initialize members of the myFriends array variable, which is a dynamic array of tFriends structs.

Struct tFriends
    String sName
    String sPhoneNumber
End_Struct

Procedure Example
    tFriends[] myFriends
    Move "John" to myFriends[0].sName
    Move "305-238-0012" to myFriends[0].sPhoneNumber
    Move "Susie" to myFriends[1].sName
    Move "305-555-1212" to myFriends[1].sPhoneNumber
End_Procedure

Tip: AppendArray Alternative

You can append values to the end of a dynamic array using Move and passing [-1] as the array indexer as an alternative to AppendArray.

Procedure Example
    tFriends[] myFriends
    tFriends friend
    Move "John" to friend.sName
    Move "305-238-0012" to friend.sPhoneNumber
    Move friend to myFriends[-1]
    Move "Susie" to friend.sName
    Move "305-555-1212" to friend.sPhoneNumber
    Move friend to myFriends[-1]
End_Procedure

Example 10: Copying Array Variable Members

The example below uses Move to copy the value of the array variable sValues1 to sValues2. Using Move to copy array variables uses a deep memberwise copy operation to copy each array variable member value from the first array variable to the second array variable. To copy a range of array elements to another array, rather than the whole array, use CopyArray.

Procedure Example
    String[5] sValues1 sValues2
    // some code to initialize sValues1
    Move sValues1 to sValues2
End_Procedure

You can use the Move command to move an object access name to a handle variable. The following example moves the object ID of object oMyObject to a handle variable:

Move oMyObject to hoMyObject

The command actually moves the result of an object access expression to the handle variable. At runtime, the following samples are identical.

Move oMyObject to hoMyObject
Move (oMyObject(Self)) to hoMyObject

This value of the object is determined at runtime and uses the standard DataFlex object access evaluation process. This non-expression move syntax is supported to make it easier to read and write programs.

If you will be using an object name many times in the same program, it is more efficient to move the object name to a handle variable and use the handle variable.

Procedure ShowAll
    Handle hoMyList
    String sValue
    Integer iCount i
    Move oMyList to hoMyList  // this access method is only evaluated once!
    Get Item_Count of hoMyList to iCount
    For i from 0 to (iCount - 1)
        Get Value of hoMyList i to sValue
        Send ProcessValue sValue
    Loop
End_Procedure