How to create (dinamically) array/list of objects in PPL?
Array of objects?
That is a very good question. I haven't implemented it yet but I am working on it right now. Hope to have it working for the next release. I'll keep you up-to-date.
Ok, so far I was able to make it work with linked-list. Here is how it works. Of course this is only going to work with the next release of PPL. I will be working on arrays tomorrow.
- Code:
#class employe
Public(Name$, Age$, Experience$, Salary$);
public forward nproc Create;
public forward proc Destroy;
#endclass
nproc employe.Create
if (Count(Args$) >= 1)
Name$ = Args$[0];
end;
if (Count(Args$) >= 2)
Age$ = Args$[1];
end;
if (Count(Args$) >= 3)
Experience$ = Args$[2];
end;
end;
proc employe.Destroy
ShowMessage(Name$ + " object is destroyed!");
end;
proc main
list(l$);
#object employe e$("1", 35, 15);
Static(e$);
Add(l$, e$);
#object employe e$("2", 35, 15);
Static(e$);
Add(l$, e$);
employe(l$);
First(l$);
ShowMessage(l.name$);
Next(l$);
ShowMessage(l.name$);
First(l$);
freeobject(l$);
Next(l$);
freeobject(l$);
end;
I was going to ask the same question
Okay, for example, I am going to create list of 10 employes:
Will it work? (It seems to me not:) if wont, how to do this (in next release)?
- Code:
list(l$);
for (i$,0,9,1)
#object employe e$("1", Random(100), Random(100));
//maybe here something like NewObject or CreateObject as for COM objects
Static(e$);
Add(l$, e$);
end;
Will it work? (It seems to me not:) if wont, how to do this (in next release)?
Hi,
This should work no problem, you don't need the Static() line anymore.
This should work no problem, you don't need the Static() line anymore.
Here we go here is the new section from the help file about advanced object-oriented programming:
Hope this will make everybody happy!
Advanced Object-Oriented Programming
PPL allow for more advanced object-oriented programming. There will be times where you will need to create multiple objects and reference to them later by their pointer instead. This topic will cover object pointers and list of objects and arrays of objects too.
Object pointers:
Object pointers are obtained the same way any other variable pointers can be obtained. Assigning an object pointer to a variable is quite easy. The variable will not be converted to an object automatically. However assigning a pointer to another variable to then use as an object requires an extra step.
#object myclass o$
This will create a new object with the class "myclass".
p$ = &o;$;
p$ will now contain the pointer (address location) of object o$.
Let's say class myclass as variable "name$" publically declared.
ShowMessage(p.name$);
Is not valid because, p$ is just an ordinary value at this point. However by making the p$ variable an object with the NewObject () function, PPL will be able to access the "name$" variable.
NewObject(p$, "myclass", &o;$);
This will make p$ an object of class "myclass" and will make p$ point to o$. This could be achieve by doing the following:
p$ = o$;
However in a case where we need a list of objects or an array of objects, it will become very handy.
Arrays of Objects:
An array of objects is simply an array of integer values containing pointers to objects. Each array element points to an object but is not of a type object yet. We need to use the NewObject() function to prepare a new variable to receive the object content.
dim(l$, 10)
for (i$, 0, 9)
#object myclass o$;
o.value$ = i$;
l$[i$] = &o;$;
end;
for (i$, 0, 9)
NewObject(p$, "myclass", l$[i$]);
Writeln(p.value$);
FreeObject(p$);
end;
List of Objects:
Much like an array of objects, a list of objects involves linked-list and the information stored into each list element is really just an integer value (pointer) pointing to an object.
List(l$);
for (i$, 0, 9)
#object myclass o$;
o.value$ = i$;
Add(l$, &o;$);
end;
ForEach(l$)
NewObject(p$, "myclass", l$);
Writeln(p.value$);
FreeObject(p$);
end;
NB: It is very important to note that objects created with PPL won't be freed automatically just like normal variables. This is due to the fact that multiple objects can be created using the same variable. It is then imperative to free the objects from memory manually using the FreeObject() function.
Hope this will make everybody happy!
Here is an updated demo:
- Code:
#class myclass
Public(value$);
public forward nproc Create;
public forward proc Destroy;
#endclass
nproc myclass.Create
value$ = Args$[0];
end;
proc myclass.Destroy
ShowMessage(value$ + " is destroyed!");
end;
proc main
// Array of objects example
dim(l$, 10);
for (i$, 0, 9)
#object myclass o$(i$);
l$[i$] = &o;$;
end;
for (i$, 0, 9)
myclass(p$);
&p;$ = l$[i$];
ShowMessage(p.value$);
FreeObject(p$);
end;
// List of arrays example
List(l$);
for (i$, 0, 9)
#object myclass o$(0);
o.value$ = i$;
Add(l$, &o;$);
end;
ForEach(l$)
NewObject(p$, "myclass", l$);
ShowMessage(p.value$);
FreeObject(p$);
end;
end;