Forum > General

Quickly set unit variables to their default value

(1/2) > >>

dadycool:
Hi all

I have a unit(a)

This unit(a) contain lots of variables, and it is referenced in my other units as a placeholder.

Question is how I can quickly reset variables in unit(a) to it's default value?

Thanks a lot!

winni:
Hi!

But if you want to reset all your values to zero you can use a record:


--- 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 myrec = record             a,b,c : integer;              F1,F2 : single;              ...             end; // record ...beginFillchar (myrec, sizeof(myrec),0)end;  Winni


Kays:

--- Quote from: dadycool on January 12, 2022, 06:54:15 pm ---[…] Question is how I can quickly reset variables in unit(a) to it's default value? […]
--- End quote ---
In Pascal, variables don’t have a “default value”. The programming language specification does not say a Boolean variable has e.g. true as a “default”. I think, Jamie’s approach of writing and calling a procedure setting your variables is probably the way you wanna go. (NB: A resourceString does have a default/fallback value, but you probably aren’t asking for such.)


--- Quote from: winni on January 12, 2022, 10:59:42 pm ---[…]
--- 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";}};} ---[…]Fillchar (myrec, sizeof(myrec),0)[…]
--- End quote ---
Please use default. I’m not even sure if it will properly treat a record containing for instance an ANSIString (i.e. decrement reference count), but it’s definitely better than a blunt “erase everything with #0!”.

speter:
Combining what Jamie & Kays wrote, you can try something like:


--- 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";}};} ---unit decU;// global variable declarations {$mode ObjFPC}{$H+} interface uses  Classes, SysUtils; const  sz=100;type  tmyrec = record    some : integer;    stuff : double;    here : string;  end;  tmyarray = array [1..sz] of tmyrec;  tdynamic = array of tmyrec;var  anint : integer = 0;  adouble : double  = 0.0;  another : integer;   anarray : tmyarray;  adynamic : tdynamic;const  azero : tmyrec = (some:0; stuff:0.0; here:''); procedure InitDec; implementation procedure InitDec;var  a : byte;begin  // don't need to initialise "anint" or "adouble"  another := 0;   anarray := default(tmyarray);   // this compiles, but I'm not sure what it does...  //adynamic := default(tdynamic);   setlength(adynamic,sz);  for a := 0 to sz-1 do    adynamic[a] := azero;end; begin  InitDec;end.
This allows you to call InitDec multiple times (if appropriate) or simply let the unit initialisation do its job. :)

cheers
S.

dseligo:

--- Quote from: speter on January 13, 2022, 12:41:48 am ---  // don't need to initialise "anint" or "adouble"
--- End quote ---

--- Quote from: speter on January 13, 2022, 12:41:48 am ---This allows you to call InitDec multiple times (if appropriate) or simply let the unit initialisation do its job. :)
--- End quote ---

If he won't initialize anint and adouble in code then there is no initialization when InitDec is called multiple times.
And if there is no need for multiple initialization, you don't have to initialize variables to 0: global variables are initialized by FPC.

Navigation

[0] Message Index

[#] Next page

Go to full version