Skip to main content

D. What is the Scope of an Object in Object Oriented Programming and TradeStation's OOEL

Posted in

Section 1. What do We Mean by Scope?

One must not forget, during this entire discussion on Scope within an Object Oriented Program (OOP) we are not talking about just the Scope of Variables, but also Classes and its Methods. Thus, to make it clear, scope in OOP deals with:

1. A Class
2. A Classes Variable and
2. A Classes Methods

The following subject is not extremely complicated, but the hard part of explaining this in OOEL terms is problematic because TradeStation does not provide the code behind the Object Oriented Easy Language classes they provide us. Because of the limitations of not being able to see the code behind the classes, I will do my best in creating hypothetical examples in the following sections to fully explain the concept of Scope in the vast majority of Object Oriented Languages (I use the terms "vast majority" because there are always OO languages that have exceptions).

But before we examine the scope of an Object or an Object's Variables and Methods, we must first define what we mean by "Scope".

So what is Scope?

Scope is often referred to as Visibility. This is important because if you understand the concept of Visibility in OO programming, you will more than likely understand the material in this blog.

Before we dive in to the different scopes in an Object Oriented Language, lets simplify things and look at variable scopes in a standard Easy Language program. This we help in understanding the main concept of Scope.

We can define scope more easily using concepts that everyone is familiar with.
Since anyone who has programmed with Easy Language, has of course used variables. If you specify a variable at the top of a program, meaning not in a method, what is it's scope? Well its scope is universal within that program. You can use it, and change it, anywhere.

For Example, lets say we have a program called DisplayAverageVolume. At the top of that program we declare two global variables:

Vars:
vVolumeType(0),
vAverage(0);

Because these two variables are declared at the top of the program, and most importantly outside any method, they have "global" scope within the given program. Which again, means a programmer can use and change the variables anywhere in the program.

Now, if you create a method and declare a variable within that method, what is its scope? Well you can only specify and use it within that method. This means the variables have
"local" scope, not "global".

However, you can extend the scope of a local variable by passing it to another method using the method's parameters. By doing this, now you have extended the scope of the local variable from not just the method in which its declared, but by passing the local variable to another method. However, this is the only method you can use to extend the scope of its local variables. By Easy Language forcing you to extend the scope of a local variable through a parameter, notice it makes it clear to those reading your program, where that local variable is being used, and maybe more importantly, not used. This is unlike global variables, where the reader has to search for global variables throughout the program to see where they are not only used, but changed. This is an important concept we will be covering in further sections and blogs. But for now, lets just focus on the
concept of Scope.

For Example, lets say we create a method "Main" with two local variables: l_Stringx and
l_Counter.

method void Main()
vars:
string l_Stringx,
int l_Counter;
begin
……
end;

Now lets say we create another method named processVolume, with one parameter that is type "int".

The signature of the method may look like the following:

// The parameter is "int p_Counter"
method void processVolume(int p_Counter)
begin
.....

end;

Now, if you had the following statement below, inside processVolume, or the method Main, you would not get a compiler error because aVolume has a global scope.


// This would not create an error because remember, we declared
// the variable aVolume as global. Thus it can be referred
// to anywhere.
aVolume = Volume;

Now, if inside the processVolume method, you placed the following lines of code:

// This would create an compiler error. Why? Because we
// declared the variable local to the method "MAIN". Thus, it
// can only be directly referred to inside the method Main,
// and only Main.

if l_Counter = 1 then
begin…

What about if you want use the variable l_Counter inside of processVolume? You can pass the variable l_Counter in to the method processVolume as a parameter.
For example, inside of the method Main you would have the following line of code:


// This calls the method processVolume and passes in l_Counter
// as a parameter that can be used in processVolume
processVolume(l_Counter);

Then, inside of the method processVolume(int p_Counter), you can now use the local variable declared inside the method Main, l_Counter, through the parameter p_Counter. Thus you could have the following line of code that would not create a compiler error. And notice we have now extended the "scope" of the local variable l_Counter, by passing it to processVolume as a parameter.

// The parameter p_Counter refers to the local variable
// l_Counter declared local inside the method main

if p_Counter = 1 then

begin…

In an Nutshell, this is the definition of scope. So far we have dealt with scope by examining where you can use a variable within a program in standard Easy Language code. Although the concept's of scope we have discussed apply in the Object Oriented Easy Language world, it extends to classes, their methods, and its variables. They too have certain scopes as we shall see in the next few sections. But the important thing to keep in mind, are the different type's of scopes become much more complex in nature.

See some powerful OOEL Indicators and Strategies "OOEL Indicators and Strategies".

___________________________

Coming soon…..

Section 2. The Different Types of Scope In Most Object Oriented Languages

Section 3. The Scope of a Class in OOP

Section 4. The Scope of a Class Method in OOP

Section 5. The Scope of a Class Variable in OOP

Section 6. Introduction of Get and Set Methods

Section 7. Conclusion

Here is the link to the Main TradeStation OOEL Blog.