Variable'lar ve ScopeVariables and Scope

This page is part of an incremental translation project. Body text not yet translated to Turkish falls back to English even in TR mode.

ScopeScope

Basitçe, bir variable tanımının scope'u, o tanımın geçerli olduğu yazılım parçası veya aralığıdır.Simply, scope of a variable definition is the part or range of software that definition is valid.

TROIA'da üç seviyede scope vardır: global, member, local.In TROIA there are three levels of scope: global, member, local.

Global ScopeGlobal Scope

Bir variable global variable olarak tanımlanırsa, yazılımın her yerinden erişilebilir olur. TROIA'da en geniş scope global'dir ve transaction (application) ile sınırlıdır. Başka bir deyişle, bir global variable tanımlarsanız, tüm işlemler aynı transaction içinde çalıştığı sürece herhangi bir dialog veya class method/event'inden ona erişebilirsiniz.If a variable defined as global variable, it is accessible from any part of the software. In TROIA, largest scope is global and it is bounded by transaction (application). In other words, if you define a global variable, you can access it from any dialog or class method/event providing that all items run in a transaction (application).

Member ScopeMember Scope

Bir variable member variable olarak tanımlanırsa, tanımlandığı class'ın tüm method'larında erişilebilir olur, yani member variable için en geniş sınır class'tır.If a variable defined as a member variable, it is accessible in all methods of the class that is defined in, so class is the largest boundary for a member variable.

Member variable'lara class dışından sadece isimleriyle erişmek mümkün değildir, çünkü her class instance'ı için ayrı ayrı tanımlanırlar. Bir class instance'ının member variable'ına erişmek için @ operatörü kullanılır. (Diğer dillerdeki . operatörüne benzer: INSTANCE1@XPOS, INSTANCE1 adlı class instance'ının XPOS member'ı anlamına gelir.)It is not possible to access member variables using only their name outside of the class, because they are defined for each instance of class. To access a member variable of a class instance @ operator is used. (It is similar to . operator in other languages, INSTANCE1@XPOS : XPOS member of a class instance named INSTANCE1)

In a class method, a member variable overrides variables that is
    defined for a larger scope (global).

Örneğin XPOS adında integer type bir member variable'ınız ve aynı isimde bir global variable'ınız varsa, XPOS'a erişildiğinde tüm method'lar member olanı kullanır.For example if you have an integer type member variable named XPOS and a global variable with same name, all methods use member when XPOS is accessed to do anything.

Local ScopeLocal Scope

Bir variable local variable olarak tanımlanırsa, yalnızca tanımlandığı method/event içinde erişilebilir olur, yani local variable için en geniş sınır method/event'tir.If a variable defined as a local variable, it is accessable only in the method/event that it is defined in, so method/event is the largest boundary for a local variable.

TROIA'da bir variable'ı block gibi daha dar bir scope için tanımlamak mümkün değildir, bu yüzden bir local variable tanımlarsanız method/event içinde her yerden erişebilirsiniz.In TROIA, it is not possible to define a variable for a narrower scope like a block, so if you define a local variable you can access it anywhere in method/event.

A local variable overrides other variables
    which are defined for a larger scope (member, global).

Local variable'lar, diğer dillerde olduğu gibi method/event'in her instance'ı için ayrı ayrı oluşturulur (recursive çağrıları düşünün).Local variables are created for each instance of the method/event like any other language (think on recursive calls).

A static local variable is a variable that is defined for once
    and shared by all instances of method.

Static local variables are not supported in TROIA.

Function parametreleri local symbol'lerdir; programcı bunlara yalnızca method içinde parameter ismiyle erişebilir.Function parameters are local symbols that, programmer can access them only inside the method using parameter name.

Variable TanımlamaDefining Variables

TROIA'da birden fazla variable tanımlama yöntemi vardır. İlk ve en yaygın yöntem, LOCAL, GLOBAL, MEMBER ve OBJECT gibi data definition komutlarını kullanmaktır.In TROIA, there are multiple variable definition methods. The first and most common method is using data definition commands such as LOCAL, GLOBAL, MEMBER and OBJECT.

Diğer yaygın yöntem ise dialog üzerindeki control'leri kullanmaktır. Örneğin, textfield içeren bir dialog açıldığında, sistem otomatik olarak textfield ile aynı isimde bir global string symbol tanımlar. Bunlara "control symbol" denir. Tüm control symbol'ler global'dir. Her control type/subtype'ının otomatik tanımlanacak karşılık gelen bir variable type'ı vardır. Control'ler, type'lar ve subtype'lar ilerleyen bölümlerde detaylıca ele alınacaktır.Another common method is using controls on dialogs. For example, when a dialog containing textfield is opened, system automatically defines a global string symbol which has same name with textfield. They are called as “control symbols”. All control symbols are global. Each control type/subtype has a corresponding variable type to define automatically. Controls, types and subtypes will be discussed in next sections detailly.

Ayrıca, hedef variable'lar mevcut değilse sabit type'ta global variable tanımlayabilen (sınırlı sayıda) özel komutlar da vardır (örn: SELECT, TABLE), ancak okunabilirlik açısından bu yöntem önerilmez.Additionally, there are (limited) special commands which are able to define fixed type global variables if target variables do not exist. (ex:SELECT, TABLE), however this method is not recommended because of readibility.

Variable Tanımlama KomutlarıVariable Definition Commands

GLOBAL KomutuGLOBAL Command

GLOBAL komutu, verilen data type ile bir global variable tanımlar. Tek bir GLOBAL komutuyla, data type'ları farklı olsa bile birden fazla variable tanımlamak mümkündür.GLOBAL command defines a global variable with given data type. Defining multiple variables with a single GLOBAL command is allowed, even if data types are different.

/* define a single variable */
GLOBAL:
        STRING STRINGVAR1;

/* define multiple variables */
GLOBAL:
        STRING STRINGVAR1,
        STRING STRINGVAR2,
        TABLE TABLEVAR1,
        INTEGER INTVAR,
        MYCLASS MYCLASSREC;

MEMBER KomutuMEMBER Command

MEMBER komutu, class'ın tüm method'larından erişilebilen bir member variable tanımlar. Yalnızca constructor dahil class method'larında kullanılabilir; dialog, report vb. içinde MEMBER komutu kullanmak programming hatasıdır. Tek bir MEMBER komutuyla, data type'ları farklı olsa bile birden fazla member variable tanımlamak mümkündür.MEMBER command defines a member variable which is accessible from all methods of the class. It can be used in only class methods including constructor and others, so using MEMBER command in dialogs, reports (etc.) is a programming error. It is possible to define multiple members variables in a single MEMBER command even if data types are different.

/* define a single member */
MEMBER:
        STRING STUDENTNAME;

/* define multiple members */
MEMBER:
        STRING FIRSTNAME,
        STRING LASTNAME,
        TABLE ITEMLIST,
        INTEGER XPOSITION
        MYCLASS MYCLASSREC;

STRING, INTEGER, TABLE veya TROIA class'ları gibi her türlü data type member variable olarak tanımlanabilir.All kind of data types such as STRING, INTEGER, TABLE or TROIA classes can be defined as member variable.

TROIA Class'ları ve constructor'ları (_CONSTRUCTOR & _VARIABLES) ilerleyen bölümlerde detaylıca ele alınacaktır.TROIA Classes and its constructors (_CONSTRUCTOR & _VARIABLES) will be discussed detailly in next sections.

LOCAL KomutuLOCAL Command

LOCAL Komutu, verilen data type ile bir local variable tanımlar. Diğer data definition komutlarına benzer şekilde, tek bir LOCAL komutuyla birden fazla variable tanımlamak mümkündür.LOCAL Command defines a local variable with given data type. Similar to other data definition commands it is possible to define multiple variables with a single LOCAL command.

/* define a single local variable */
LOCAL:
        STRING STRINGVAR1;

/* define multiple local variables */
LOCAL:
        STRING STRINGVAR1,
        STRING STRINGVAR2,
        TABLE TABLEVAR1,
        INTEGER INTVAR,
        MYCLASS MYCLASSREC;

OBJECT KomutuOBJECT Command

OBJECT komutu, en eski ve en çok kullanılan variable tanımlama komutudur. Bir variable OBJECT komutuyla tanımlandığında, scope'u variable'ın data type'ına ve OBJECT komutunun hangi method içinde kullanıldığına bağlıdır.OBJECT command is the oldest and most used variable definition command. When a variable is defined with OBJECT command, it’s scope depends on data type of the variable and which method that OBJECT command is used in.

Scope'u belirleyen ana parametre data type'tır. Table'lar ve class instance'ları her zaman global'dir. Ancak simple type (STRING, DECIMAL, LONG, INTEGER,…) variable'ların scope'u tanımlandıkları method'a bağlıdır. Simple type variable'lar; tanım bir dialog/report method'unda veya event'inde yapılırsa global, method bir class constructor'ıysa (_CONSTRUCTOR & _VARIABLES) member, sıradan bir class method'uysa local olarak tanımlanır.The main parameter is data type to decide scope. Tables and class instances are always global. But scope of simple type (STRING, DECIMAL, LONG, INTEGER,…) variables depends on the method that they defined in. Simple typed variables are defined as global if definition is made on a dialog/report method or event, if method is a class constructor ( _CONSTRUCTOR & _VARIABLES) scope is member, but if method is a regular class method simple variables are defined as local.

İşte OBJECT komutunun, data type ve method type'ına bağlı olarak scope'u nasıl belirlediğini gösteren basit bir tablo.Here is a simple table that shows how OBJECT command decides scope, depending on data type and method type.

Dialog/Report Events&MethodsClass Constructor&VariablesClass Methods
TableGlobalGlobalGlobal
Class InstanceGlobalGlobalGlobal
Simple TypesGlobalMemberLocal

Tek bir OBJECT komutunda birden fazla variable tanımı yapmak da desteklenir.It is also supported multiple variable definitions on a single OBJECT command.

/* suppose that this is a dialog method, think on its scope */
OBJECT:
        STRING STRINGVAR1;

/* suppose that this is a class method, think on their scope */
OBJECT:
        STRING STRINGVAR1,
        STRING STRINGVAR2,
        TABLE TABLEVAR1,
        INTEGER INTVAR,
        MYCLASS MYCLASSREC;

İlk bakışta, OBJECT komutuyla tanımlanan bir variable'ın scope'una karar vermek biraz zordur. Bu yüzden, okunabilirliği artırmak için OBJECT yerine GLOBAL, LOCAL ve MEMBER kullanılması önerilir. (Ne yazık ki, mevcut TROIA uygulamalarında en çok kullanılan data definition komutunun OBJECT olduğunu bilmelisiniz.)At first glance, it is a little bit hard to decide scope of a variable that is defined by OBJECT command. As a result of this fact, using GLOBAL, LOCAL and MEMBER instead of OBJECT is recommended to increase readibility. (Unfortunately, you must know that OBJECT is the most used data definition command on existing TROIA applications.)

System Variable'larSystem Variables

System variable'lar, bu değerleri TROIA seviyesinde kullanmak üzere sistem, user session veya bazı özel eylemler hakkında bilgi saklayan global ve önceden tanımlanmış variable'lardır. System variable'ların çoğu read-only'dir ve data type'ları değişkenin amacına bağlıdır.System variables are global and predefined variables that stores information about system, user session or some specific actions to use these values on TROIA level. Most of system variables are read-only and their data types depends on variable’s purpose.

Aşağıda bazı system variable örnekleri listelenmiştir, daha fazlası için TROIA Help'e bakınız.Some examples of system variables are listed below, for more please view TROIA Help.

SYS_CURRENTDATE       : Returns long value of now.
SYS_CLIENT            : Client value that is used while login.
SYS_LANGU             : Language value that is used while login.
SYS_USER              : Username of current user.
SYS_VERSION           : TROIA platform server version.
SYS_AFFECTEDROWCOUNT  : Number of affected rows after db update/insert/delete.
SYS_CURRENTDIALOG     : Name of current dialog.
CONFIRM               : Selected value after a confirm or option message.
SQL                   : Latest SQL Query that is sent to database.

Bir system variable ile aynı isimde variable tanımlamak yasaktır. Çoğu SYS ön ekiyle başlar, ancak SQL, CONFIRM gibi istisnalar da vardır.It is not allowed to define variables which have same name with a system variable. Most of them starts with SYS prefix, although there are exceptions such as SQL, CONFIRM etc.

Defining variables that start with 'SYS' prefix
    is not a good programming practice.

Variable Tanımlama Hakkında Bazı GerçeklerSome Facts About Defining Variables

Undefined Variable KullanımıUsing Undefined Variables

TROIA'nın yapısı (dialog'lar arası veri aktarımı) nedeniyle undefined variable kullanmak compile hatasına yol açmaz. Bir variable tanımlanmadan önce kullanılırsa, adıyla aynı değere sahip bir string variable gibi kendi ismini value olarak döndürür. Undefined variable'lar string variable'lara benzese de, aslında STRING olmadıklarını bilmeliyiz.Using undefined variables do not cause compiling errors because of TROIA’s structure (data transfer between dialogs). If a variable is used before it is defined, it returns its name as value like a string variable that has same value with its name. Although undefined variables are similar to string variables, we must know that they are not STRING.

LOCAL:
        STRING MYVAR;

        MYVAR = MYUNDEFINEDVAR;

        /* MYVAR's value: MYUNDEFINEDVAR */

Aynı Variable'ı Birden Fazla Kez TanımlamaDefining Same Variable More Than Once

Aynı variable'ı birden fazla kez tanımlamanın ilk yolu, aynı variable'ı tanımlayan farklı definition komutları yazmaktır. Bu durumda ikinci komut tanımı yok sayar. İşte örnek:The first way of defining same variable more than once is writing different definition commands which define same variable. In this case second command ignores the definition. Here is the sample:

OBJECT:
        STRING RESULT,
        STRING MYVAR;

MYVAR = 'Hello World';

OBJECT:
        STRING MYVAR;

RESULT = MYVAR;

/*  RESULT's value: Hello World
        Second OBJECT command ignored the MYVAR definition. */

İkinci yöntem ise aynı definition komutunu birden fazla kez çalıştırmaktır. Bir loop içinde ya da birden fazla kez tetiklenen bir event'te. Bu durumda, variable'ı tanımlayan definition komutu onu initialize eder (default değerini ayarlar; default değerleri görmek için data types bölümüne bakınız).Second method is running same definition command multiple times. In a loop or an event which is triggered multiple times. In this case, the definition command which defines the variable initializes it (sets its default value, to see the default values please see data types section).

start loop block that runs twice:
        OBJECT:
                STRING MYVAR;

        RESULT = MYVAR;

        MYVAR = 'Hello World';
end loop

OBJECT:
        STRING MYVAR;

RESULT = MYVAR;

/* in first and second iteration RESULT's value is empty string.
   after last assignment RESULT's value: Hello World                    */

Looping ve assignment'lar ilerleyen bölümlerde detaylıca ele alınacaktır, bu bölümde yalnızca aynı variable'ı birden fazla kez tanımlamaya odaklanın.Looping and assignments will be discussed detailly in next sections, in this section please focus on defining same variable in multiple times.

Üçüncü yöntem, zaten control symbol olarak tanımlanmış bir variable için data definition komutu çalıştırmaktır. Bu durum interpreter tarafından yok sayılır.Third method is running a data definition command for a variable that is already defined as control symbol. This case is ignored by the interpreter.

Bir Variable'ın Data Type'ını ÖğrenmekTo Learn Data Type of a Variable

Bazı durumlarda programcıların bir variable'ın data type'ına ihtiyacı olabilir. TROIA'da, GETVARTYPE() system function'ı (predefined function) verilen variable'ın type'ını string olarak döndürür.In some cases, programmers may need data type of a variable. In TROIA, GETVARTYPE() system function(predefined function) returns type of given variable as string.

Undefined variable'lar string'lere benzese de, GETVARTYPE undefined variable'lar için 'UNKNOWN TYPE' döndürür. İşte GETVARTYPE() fonksiyonunun bir örneği.For undefined variables GETVARTYPE returns ‘UNKNOWN TYPE’ even if undefined variables are similar to strings. Here is an example of GETVARTYPE() function.

OBJECT:
        STRING STRINGVAR,
        INTEGER INTVAR,
        STRING RESULT;

RESULT = GETVARTYPE(STRINGVAR); /* RESULT : 'STRING' */
RESULT = GETVARTYPE(INTVAR);    /* RESULT : 'INTEGER' */
RESULT = GETVARTYPE(NOVAR);     /* RESULT : 'UNKNOWN TYPE' */

İsimlendirme & KurallarNaming & Conventions