Forum > Beginners
Is there an easy way to initialize an array of objects in free pascal? [SOLVED]
YiannisKam:
In C++ I can initialize an array of objects like this:
--- Code: C [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Person persons[3] = { Person("John", 1.77, 80), Person("Maria", 1.65, 52), Person("Jim", 1.85, 95) };Is there a similar way that can be done in free pascal?
jamie:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Type TPersons = Record Name:String; Num1,Num2:Single; end;Const Persons:array[0..1]of TPersons=((Name:'Crap';Num1:1.7;Num2:80),(Name:'Crapper';Num1:1.9;Num2:85));
Thats one way.
you can also do this within the VAR section too but you need a type;
YiannisKam:
Person is a class and it doesn't work. Here is the code I'm trying to make it work
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program ArrayOfObjects; {$mode objfpc}{$H+} type { TPerson } TPerson = class public name: string; height: real; weight: integer; constructor Create(p_name: string; p_height: real; p_weight: integer); procedure printPerson; private end; { TPerson } constructor TPerson.Create(p_name: string; p_height: real; p_weight: integer);begin self.name := p_name; self.height := p_height; self.weight := p_weight;end; procedure TPerson.printPerson;begin writeln('Name: ', self.name); writeln('Height: ', self.height:0:2); writeln('Weight: ', self.weight); writeln;end; {this doesn't workconst persons: array[0..2] of TPerson = ((name:'John'; height:1.77; weight: 90), (name:'Maria'; height:1.65; weight: 52), (name:'Jim'; height:1.85; weight: 95));}var i: Integer; begin for i := Low(persons) to High(persons) do begin persons[i].printPerson; end; readln;end.
I know I can do:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- persons[0] := TPerson.Create('John', 1.77, 90); persons[1] := TPerson.Create('Maria', 1.65, 52); persons[2] := TPerson.Create('Jim', 1.85, 95); but out of curiosity, I'm looking something more compact if already exists
Bogen85:
--- Quote from: YiannisKam on January 30, 2023, 02:30:23 am ---Person is a class and it doesn't work.
I know I can do:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- persons[0] := TPerson.Create('John', 1.77, 90); persons[1] := TPerson.Create('Maria', 1.65, 52); persons[2] := TPerson.Create('Jim', 1.85, 95); but out of curiosity, I'm looking something more compact if already exists
--- End quote ---
More compact, possibly. You can't assign a class instance to a const outside a parameter list.
You can assign a assign class instances to a const var parameter though. But you will have to free them though, as class instances are allocated off the heap, not the stack.
The following works:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program ArrayOfObjects; {$mode objfpc}{$H+} type { TPerson } TPerson = class public name: string; height: real; weight: integer; constructor Create(p_name: string; p_height: real; p_weight: integer); procedure printPerson; private end; { TPerson } constructor TPerson.Create(p_name: string; p_height: real; p_weight: integer);begin self.name := p_name; self.height := p_height; self.weight := p_weight;end; procedure TPerson.printPerson;begin writeln('Name: ', self.name); writeln('Height: ', self.height:0:2); writeln('Weight: ', self.weight); writeln;end; procedure main(const persons: array of TPerson); var i: Integer; begin for i := Low(persons) to High(persons) do begin persons[i].printPerson; end; for i := Low(persons) to High(persons) do begin persons[i].free; end; readln; end; begin main([TPerson.Create('John', 1.77, 90), TPerson.Create('Maria', 1.65, 52), TPerson.Create('Jim', 1.85, 95)]);end.
YiannisKam:
Thanks Bogen85, I didn't know that kind of syntax but to be honest it looks more counterintuitive to me. I think I'll go with this
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- persons[0] := TPerson.Create('John', 1.77, 90); persons[1] := TPerson.Create('Maria', 1.65, 52); persons[2] := TPerson.Create('Jim', 1.85, 95); although it's cumbersome and ugly.
Navigation
[0] Message Index
[#] Next page