Forum > General

Finding null value of variable types

(1/2) > >>

nikel:
Hello, I'm trying to find null values of variable types in order to terminate them. I was searching and finally this seemed worked:


--- 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";}};} ---Num:=not -1; // Outputs 0WriteLn(Num.ToString) // Should give error
On ActionScript I used to use something like:


--- Code: ---Num=NaN; // Not a number
Mc=MovieClip;
--- End code ---

Finally I stuck at this. I can't learn null value of TStringList (also tried StringList:=TStringList;)

--- 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";}};} ---StringList:=TStringList.Create;StringList.LoadFromFile('Yeni Metin Belgesi.txt', TEncoding.UTF8);WriteLn(Default(TStringList).ToString); // Gives error External:SIGSEGV ReadLn;
And this sets an integer to 0 but doesn't terminate:

--- 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";}};} ---Num:=Default(Integer);
Where can I learn null value of variable types?

lucamar:
Few types have a "null" value in Pascal;  besides pointers as such basically only those that are internally a pointer, such as LongStrings, arrays, records, (old-style) objects, classes ... In all those cases comparing/setting to Nil is enough, or (for strings) to ''.

Furthermore, floating point vars (Single, Doubles, etc.) can be set/compared to NaN.

For all other types (Integer, Char, ...) you can set them to some "default" value which you could consider null, but they don't have one per se since any value they may have does, in fact, represent something.

Note also that there some caveats if you want to "terminate" variables of the first kind: arrays must have its length set to 0 (with SetLength()), records and objects must be Disposed (or its memory freed somehow), classes Freed, etc.

So in your examples, Num (which I suppose is an integer) can't be "terminated", while StringList (a class object) is terminated with StringList.Free (or FreeAndNil(StringList) for a testable state)

MarkMLl:
I echo what @Lucamar says, with the extension that an empty string is = '' (use of which is, I'm told, more efficient than checking for Length() = 0), an assigned-but-empty stringlist has Count = 0, and an unassigned object is nil... that's specifically why there's the Assigned() function.

As related issues, sometimes you need to consider the maximum and minimum possible values of (numeric) types, and in principle you might need to consider the identity of a type+operator combination.

The poor support for reserved "this variable contains nothing" values isn't an FPC problem, it's implicit in the PC architecture and in fact in most architectures with the exception of the few that have had out-of-word tags.

MarkMLl

egsuh:
I think there are no null values in pascal, in any type of variables. Well, PChar might --- but not sure --- I haven't used it.
With your example  num := not -1,

-1 is $FFFFFFFF.  and "not -1" does bit operation so that the result is $00000000, which is zero, not null.

nikel:
Thanks for the replies. So I'm going to use initial value instead of null while initializing and terminating most of them.

Navigation

[0] Message Index

[#] Next page

Go to full version