Value Types and Reference Types
In Visual Basic, data types are implemented based on their classification. The Visual Basic data types can be classified according to whether a variable of a particular type stores its own data or a pointer to the data. If it stores its own data it is a value type; if it holds a pointer to data elsewhere in memory it is a reference type.
A data type is a value type if it holds the data within its own memory allocation. Value types include the following:
All numeric data types
, , and
All structures, even if their members are reference types
Enumerations, since their underlying type is always , , , , , , , or
Every structure is a value type, even if it contains reference type members. For this reason, value types such as and are implemented by .NET Framework structures.
You can declare a value type by using the reserved keyword, for example, . You can also use the keyword to initialize a value type. This is especially useful if the type has a constructor that takes parameters. An example of this is the Decimal(Int32, Int32, Int32, Boolean, Byte) constructor, which builds a new value from the supplied parts.
A reference type contains a pointer to another memory location that holds the data. Reference types include the following:
All arrays, even if their elements are value types
Class types, such as Form
A class is a reference type. For this reason, reference types such as and are supported by .NET Framework classes. Note that every array is a reference type, even if its members are value types.
Since every reference type represents an underlying .NET Framework class, you must use the New Operator keyword when you initialize it. The following statement initializes an array.
Elements That Are Not Types
The following programming elements do not qualify as types, because you cannot specify any of them as a data type for a declared element:
Properties and procedures
Variables, constants, and fields
Working with the Object Data Type
You can assign either a reference type or a value type to a variable of the data type. An variable always holds a pointer to the data, never the data itself. However, if you assign a value type to an variable, it behaves as if it holds its own data. For more information, see Object Data Type.
You can find out whether an variable is acting as a reference type or a value type by passing it to the IsReference method in the Information class of the Microsoft.VisualBasic namespace. Information.IsReference returns if the content of the variable represents a reference type.
Nullable Value Types
Type Conversions in Visual Basic
Efficient Use of Data Types
Object Data Type
Author: Anirban Bhattacharjee
Submitted: 16 Sept 2008
Concept of Run Time Type Creation
Type object creation
Type object can be created referring to the corresponding description class in 3 steps.
Declaration of structure and table type referring to the corresponding description classes.
ref_rowtypeTYPE REF TO cl_abap_structdescr--->Structure declaration
ref_tabletype TYPE REF TO cl_abap_tabledescr--->Internal Table declaration
Dynamic instantiation. First we create a line type dynamically referring to the given data table name by the syntax,
ref_rowtype?= cl_abap_typedescr=>describe_by_name( p_tab ).
After creation of the row type the same is widening casted toref_rowtype.
To create an internal table dynamically at runtime we use CREATE method of description class CL_ABAP_TABLEDESCR. CREATE method contains parameter P_LINE _TYPE to determine line type of the internal table with respect to ref_rowtypedefined above. Finally the internal table instantiated to ref_tabletype.
ref_tabletype= cl_abap_tabledescr=>create( p_line_type = ref_rowtype ).
Creation of object.
The CREATE DATAallows us create object during run time andHANDLEaddition is used as the reference is also created dynamically during runtime.CREATE DATA ref_itabTYPE HANDLE ref_tabletype.
CREATE DATA ref_waTYPE HANDLE ref_rowtype.
The value of is assigned to the compatible field symbol by ASSIGN statement
ASSIGN ref_itab->* TO <fsym_itab>.
ASSIGN ref_wa->*TO <fsym_warea>.
Data Population into the dynamically created internal table.
INTO TABLE <fsym_itab>.
Display of data.
The data is displayed in two steps.
Initially we take data of each row of the internal table into work area one-by-one with the help of LOOP... ENDLOOP statement.
LOOP AT <fsym_itab> INTO <fsym_warea>.
Inside this LOOP.... ENDLOOP, we take one DO...ENDDO. loop where the field values of each of the work area take previously is assigned to the field symbol<fsym_field>.
ASSIGN COMPONENT sy-index OF STRUCTURE <fsym_warea> TO <fsym_field>.
After the value of the last field of each work area is assigned to<fsym_field>,if we again executetheASSIGN COMPONENT, the sy-subrcvalue will become non-zero and we need to break the loop byEXITstatement.
IF sy-subrc NE 0.
In the selection screen , enter the name of any SAP Data table. Here I have taken SCARR.
On hit F8, we can get the contain of SCARR.