Forum > Beginners

How to initialize the array

(1/6) > >>

Doe:
Hello, I am a beginner and I was trying to get a value from the string and put it into the array. An error message "Warning: Variable "code" of a managed type does not seem to be initialized". How do I solve it?

program pra2;
uses SysUtils;
var
  code:TStringArray;
  j,i:integer;
  text:string;

begin
  text:='rafrgqaf';
  code[0]:=text[1];
  writeln(code[0]);
  readln();
  end.           

lucamar:
It's just a warning, in case you forgot to initialize the array (or other managed type, like String), and it can usually be either ignored or shut down by initializing it, for example with:

--- 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";}};} ---code := Default(TStringArray);
Note, though, that in your case you did really forget to initialize the array, and that in a way that might lead you to an error/exception. Instead, do 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";}};} ---program pra2;uses SysUtils;var  code:TStringArray;  j,i:integer;  text:string; begin  text:='rafrgqaf';  { Initialize the array to contain 1 element }  SetLength(code, 1);  { you'll still get the warning: just ignore it,    deactivate it or use Default() to pre-initialize }  code[0]:=text[1];  writeln(code[0]);  readln();end.

Doe:
Got it, thanks a lot!

furious programming:
You can also use a special array constructor:


--- 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";}};} ---Code := TStringArray.Create('r', 'a', 'f');
for both literals and variables:


--- 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";}};} ---var  Code: TStringArray;  Foo: String = 'abc';begin  Code := TStringArray.Create(Foo[1], Foo[2], Foo[3]);   Write(Code[0], Code[1], Code[2]); // prints "abc"end;

lucamar:

--- Quote from: furious programming on December 28, 2020, 02:35:32 pm ---You can also use a special array constructor [...]
--- End quote ---

Is that already available in 3.2.0? Man, I didn't know! I thought it was still Trunk-only :-[

Navigation

[0] Message Index

[#] Next page

Go to full version