Free Pascal - Create an array of previously declared elements/objects - pascal

Using Lazarus - Free Pascal under Win7 64Bit, Laz Version 2.0.2 , fpc 3.0.4 , i would like to know if it is possible to create an array of previously declared checkboxes TCheckBox or TEdit, or whatever element i would like.
Using something like this does not compile:
var
myarray: array[1..2] of TCheckBox = (CheckBox1, CheckBox2); //CheckBox1 has been declared earlier as TCheckBox
While this one compiles ok
var
myarray: array[1..2] of TCheckBox;
begin
myarray[1]:= CheckBox1;
myarray[2]:= CheckBox2;
If i had 50 CheckBoxes, i would like to avoid assigning all of them to myarray one by one under begin.
Is it possible myarray values to be given during variable declaration, in var section?

In the declaration section you can not use variables, only constants. However you can use dynamic array constructor:
type
TCheckBoxArray = array of TCheckBox;
var
myarray: TCheckBoxArray;
begin
myarray := TCheckBoxArray.Create(CheckBox1, CheckBox2);

Related

p5.js - When to declare variables using var vs this.varName

I'm still figuring out how to use p5.js. In regular java you have to declare each variable using its data type, ex. int foo = 0.
In p5, I know you can just use var foo but you can also declare variables using this.foo. If someone could clarify when is the proper time to use var and when i can use this, that would be very helpful.
For example, if I want to declare a variable inside a method, should i use var foo = thing or could I declare it using this.foo = thing? What should I use when declaring global variables or when referring to objects passed into methods?
Thanks!
First of all, p5 is not a language, it is a Javascript library, you are coding in Javascript, not p5.
Coming to your question, if you want to use some function as a data type, similar to a class in java, and want all the "instances" of that to have their own different variables, you use this. If they are just variables you use in someway but don't need to be specific for each instance, or if the function is not a constructor function and is not to be used as a data type, you will just use var then.
Again, there is no class stuff in javascript, you will have to write what is called a constructor function in order to "simulate" a java class, but be aware that a constructor function should not return anything. Here is an example of car class in java:
class car {
int speed = ___;
String model = ___;
static int numOfWheels = ___;
}
This is what it will look like in javascript (a constructor function):
function car() {
this.speed = ____;
this.model = ____;
var numOfWheels = ___;
}
If you declare a variable without this, it can be roughly compared to a static variable in a java class in the sense that it will be constant among all the instances.
So basically, at least in most cases, you will use this.varName usually inside constructor functions, i.e., functions that you will use to construct objects.
What should I use when declaring global variables or when referring to objects passed into methods?
Global variables will almost always be var something = something. When referring to objects passed into functions, just use the dot notation to refer to its properties like passedObject.someProperty
I would recommend you to learn Javascript before jumping into p5 directly, here are some resources that I found useful when I started learning Javascript-
w3 School
JavaScript Info Website
TheNewBoston

Why use extra procedure to initialize values instead of doing it in constructor?

So i've been going trough types at my new work and they pretty much all look like this
create or replace TYPE BODY T_Some_Type AS
CONSTRUCTOR FUNCTION T_Some_Type
RETURN SELF AS RESULT AS
BEGIN
INTIALIZE();
RETURN;
END T_Some_Type;
MEMBER PROCEDURE INTIALIZE AS
BEGIN
var1 := 0;
var2 := 0;
var3 := 0;
END INTIALIZE;
END;
Being skilled in OOP but new to pl/sql, i keep wondering why use extra procedure to initialize variables, when it can be done directly in constructor making objects interface simpler and lighter on memory.
This is how i would normally do it :
create or replace TYPE BODY T_Some_Type AS
CONSTRUCTOR FUNCTION T_Some_Type
RETURN SELF AS RESULT AS
BEGIN
var1 := 0;
var2 := 0;
var3 := 0;
RETURN;
END T_Some_Type;
END;
Is there any advantage or this is recommended for some reason?
Please advise.
Please bear in mind that the object features in Oracle have evolved slowly since version 8.0, and yet are still pretty limited in some respects. This is because Oracle is a relational database with a structured and procedural programming paradigm: object-orientation is not a good fit.
So. In a language like Java we can override a method in a sub-type but execute the code in the parent's implementation with a super() call.
Before 11g Oracle had no similar mechanism for extending member functions. If we wanted to override a super-type's method we had to duplicate the code in the sub-type, which was pretty naff. There was a workaround which is not much less naff: extract the common code into a separate method in the super-type, and call that method in the super- type and its dependents.
In 11g Oracle introduced "Generalized Invocation". This allows us to call super-type code on a sub-type. Find out more. There is one major limitation with Generalized Invocation, and that is that we cannot use it with Constructor methods. Therefore, in its sub-types' constructors too our only choice is to put that code in a method like the initialize() in your question.

IS there reflection in Oracle Forms 6 or later?

IS there reflection in Oracle Forms 6 or later?
Is it possible to enumarate labels or other elements?
Forms is an old and venerable programming language, and it doesn't support full-on reflection, Java style. However, it does have a complement of GET and SET functions which enable us to interrogate and manipulate a Form's metadata.
So we can step through the items of a block and get their labels using GET_ITEM_PROPERTY like this (example adapted from the documentation):
DECLARE
cur_itm VARCHAR2(80);
cur_block VARCHAR2(80) := :System.Cursor_Block;
cur_label VARCHAR2(120);
BEGIN
cur_itm := Get_Block_Property( cur_block, FIRST_ITEM );
WHILE ( cur_itm IS NOT NULL ) LOOP
cur_itm := cur_block||’.’||cur_itm;
cur_label := Get_Item_Property( cur_itm, LABEL);
-- do whatever you want with the label here
cur_itm := Get_Item_Property( cur_itm, NEXTITEM );
END LOOP;
END;
You could change the LABEL of the current item using SET_ITEM_PROPERTY.
Note: LABEL is a property which only applies to certain items (buttons, checkboxes, etc) so you might what to include a test for the item type and perhaps grab the PROMPT_TEXT instead, if that's appropriate.
There are loads of ways we can change the appearance and behaviour of a Form on the fly. The Form Builder Reference covers all the built-ins, so there's no point in recapitulating it here. Find out more.

How do I reinitialise a UDT in VB6?

I've got a loop, which is reading in a stack of XML files, for each one, it validates the data that was in the XML and loads it into some UDTs and then does some work on the data.
Then it gets back to the beginning of the loop and the UDTs still have data in from the previous XML. If that tag is defined in the new one, it overwrites, but if that tag isn't defined, then that element in the UDT is left alone.
But I can't reset the UDT by the technique I'd use for a variable (Let X = 0) unless I go through every single element of the UDT and reset the value. And doing it object-style (Set X as New UDT) doesn't work.
How do I do it?
Dim a new variable as the UDT and set the old one equal to the new variable.
For instance:
Dim XEmpty as UDT
X = XEmpty
Will reinitialise a variable X that is a UDT of type UDT.
You can use an empty utility function that simply returns the UDT
public function newTFoo() as TFoo
'//
end function
dim t as TFoo
t.x = 1234 ...
t = newTFoo()
'// t is reset

How do dictionary lookups work in IronRuby?

I have this line of IronPython code:
Traits['Strength'].Score + Traits['Dexterity'].Score
Traits is defined as such:
Dim m_Traits As New Dictionary(Of String, Trait)
scope.SetVariable("Traits", m_Traits)
I would like to translate the IronPython code into IronRuby, but I'm having trouble finding the correct syntax.
In Ruby (and IronRuby), variables must begin with a lowercase letter. Therefore, just change the Traits variable to traits to make your code works:
var engine = IronRuby.Ruby.CreateEngine();
var scope = engine.CreateScope();
scope.SetVariable("traits", traits);
dynamic result = engine.Execute("traits['Strength'].Score + traits['Dexterity'].Score", scope);
(this code works, I checked).
By the way, creating a variable that starts with a capital letter makes it a constant (that's how Ruby works) and adding a constant to the scope is done in a slightly different way.

Resources