Home > SAP software/management Tips > SAP tips and best practices > Working with Field Symbols
SAP Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

SAP TIPS AND BEST PRACTICES

Working with Field Symbols


Peter Walters, SAP Platinum Instructor
01.10.2002
Rating: -3.83- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


An important, but frequently misunderstood aspect of ABAP, is 
the "Field Symbol". But you'll find they aren't mysterious. 
In fact, they may remind you of some features in popular 
general-purpose programming languages.

Field symbols allow you to:
**	Assign an alias to a data object(for example, a shortened 
        name for data objects structured through several hierarchies
        - <fs>-f instead of rec1-rec2-rec3-f)
**	Set the offset and length for a string variably at runtime
**	Set a pointer to a data object that you determine at runtime (dynamic ASSIGN)
**	Adopt or change the type of a field dynamically at runtime 
**	Access components of a structure
**	(from Release 4.5A) Point to lines of an internal table 
        (process internal tables without a separate work area)

Field symbols in ABAP are similar to pointers in other programming 
languages. However, pointers (as used in PASCAL or C) differ from ABAP
field symbols in their reference syntax.

The statement ASSIGN f to <fs> assigns the field f to field
symbol <fs>. The field symbol <fs> then "points" to the
contents of field f at runtime. This means that all changes to the 
contents of f are visible in <fs> and vice versa. You declare
the field symbol <fs> using the statement FIELD-SYMBOLS: <fs>.


Reference syntax
Programming languages such as PASCAL and C use a dereferencing symbol
to indicate the difference between a reference and the object to which
it refers; so PASCAL would use p^ for a pointer instead of p, C would 
use *p instead of p. ABAP does not have any such dereferencing symbol.

**	In PASCAL or C, if you assign a pointer p1 to a pointer p2, 
you force p1 to point to the object to which p2 refers (reference semantics).

**	In ABAP, if you assign a field symbol <fs1> to a field 
symbol <fs2>, <fs1> takes the value of the data object to
which <fs2> refers (value semantics).

**	Field symbols in ABAP are always dereferenced, that is, 
they always access the referenced data object. If you want to 
change the reference yourself in ABAP, you can use the ASSIGN statement
to assign field symbol <fs1> to field symbol <fs2>.


Using field symbols
You declare field symbols using the FIELD-SYMBOLS statement. 
They may be declared either with or without a specific type.
At runtime you assign a field to the field symbol using the ASSIGN 
statement. All of the operations on the field symbol act on the field
assigned to it.
When you assign a field to an untyped field symbol, the field symbol 
adopts the type of the field. If, on the other hand, you want to assign 
a field to a typed field symbol, the type of the field and that of the 
field symbol must be compatible.
A field symbol can point to any data object and from Release 4.5A, 
they can also point to lines of internal tables.

The brackets (<>) are part of the syntax.

Use the expression <fs> IS ASSIGNED to find out whether the field
symbol <fs> is assigned to a field.
The statement UNASSIGN <fs> sets the field symbol <fs> so
that it points to nothing. The logical expression <fs> 
IS ASSIGNED is then false. The corresponding negative expression 
is IF NOT <fs> IS ASSIGNED.

An unassigned field symbol <fs> behaves as a constant with 
type C(1) and initial value SPACE.
MOVE <fs> 
TO dest	Transfers the initial value SPACE to the variable dest
MOVE 'A' to <fs>	
Not possible, since <fs> is a constant 
(runtime error).

To lift a type restriction, use the CASTING addition in the
ASSIGN statement. The data object is then interpreted as though
it had the data type of the field symbol. You can also do this
with untyped field symbols using the CASTING TYPE <type> addition.

The danger with pointers is that they may point to invalid areas. 
This danger is not so acute in ABAP, because the language does not 
use address arithmetic (for example, in other languages, pointer p 
might point to address 1024. After the statement p = p + 10, it would
point to the address 1034). However, the danger does still exist, and
memory protection violations lead to runtime errors.

A pointer in ABAP may not point beyond a segment boundary. ABAP does
not have one large address space, but rather a set of segments. 
Each of the following has its own segment:
*	All global data
*	All local data
*	Each table work area (TABLES)
*	Each COMMON PART

You should only let field symbols move within an elementary field or
structure where ABAP allows you to assign both within the global data
and beyond a field boundary.


Code

You can find more information about Field Symbols by attending BC402:ABAP Programming Techiques,
a regularly scheduled class at 'SAP Educational Services' 10 North American Education Centers.
SAP Educational services offers a wide variety of courses on the ABAP programming language.
Check out our website at http://www.sap.com/usa/education for course outlines and schedules.

Rate this Tip
To rate tips, you must be a member of SearchSAP.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
SAP tips and best practices
Minimizing business disruptions during an SAP upgrade or implementation
Retrieving content from an SAP workflow attachment
Updating URLs in SAP SRM
How to change default settings in SAP SRM
What are the benefits of implementing SAP SRM?
Increase column width in a transaction screen
Updating an RT table entry in SAP HR Payroll -- without the ADDWTE option
Is this the quickest way to find a BADI?
Adding custom fields for retail product comparisons in SAP BW
Improving performance with ABAP Objects in SAP Workflow

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



NetWeaver SAP White Papers
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
SearchSAP.com is a search service provided by TechTarget and is completely
independent of and not affiliated with SAP AG.
  TechTarget - The IT Media ROI Experts