Forum > General

What is the code for checking whether a variable is defined?

(1/3) > >>

vfclists:
I assume that it is a function which takes a string and checks if there is a variable for it?

These topics of whether is variable is Free, is Nil or is Assigned always catches me out in one way or the other. So lets start with whether it exists in the first place?

KodeZwerg:
Your topics getting more and more confused so when a variable not exists but you mention it somewhere than FPC will not compile your project = variable is free :D

srvaldez:
maybe like this

--- Code: ---{$ifndef some_constant}
const some_constant = 10;
{$endif}

--- End code ---

Josh:
hi

it must be me struggling with terminoligy or the question.

In pascal a variable has to be defined/declared before you use it, or it will not compile.
If you have declard a variable and not used it,you get a hint message variable not defined.
If you declare and assign a variable but do not use it you get hint message variable assigned but not used.

Always check the compiler hints,and fix as many as you can, quite often it will fix a problemyou havent thought of.

if you coming from basic, variable are defined on creation/use. most dialects would also set the default values.
ie

--- 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";}};} ---10 for i%=1 to10:print i%:next i%this defines i% as an integer on its use,in pascal you need to define it first

--- 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 tst;var i:integer;begin  for i:=1 to 10 do writeln(i);end.
basic to define a string you could use

--- 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";}};} ---let s$=""pascal you need to difine it and then set its value

--- 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 s:string;begin  s:='';
in later dialects  this can be done in its definition

--- 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 s:string='';
Its the job of the programmer to set the initial state of variables before you use them.
most of my old code,initializes the variables in the same order as they are defined,it makes for simpler debugging, i have been burnt many times assuming a default value is assigned on declaration. So always initialize before use.

I have probably not got the question though, so please give specifics



440bx:

--- Quote from: vfclists on February 24, 2024, 11:22:00 pm ---I assume that it is a function which takes a string and checks if there is a variable for it?

--- End quote ---
there is a compiler directive {$if declared(<variable_name>)} Pascal statements {$endif} but there is no FPC function that checks for the existence of a variable.

Navigation

[0] Message Index

[#] Next page

Go to full version