Forum > Beginners

check assigned variable

(1/2) > >>

Kays:
Can I verify, that a variable (not of a pointer type) has been intentionally assigned a value?

I've got a function here, which accepts a variant record, and I'd like to verify that the variables in the variant part weren't forgotten.

balazsszekely:
Try something like 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";}};} ---uses variants; function MyFunction(V: Variant): Variant;var  VT: TVarType;begin  VT := VarType(V);  if VT = VarEmpty then  begin    ShowMessage('variant not assigned');    Exit;  end;end;  

taazz:

--- Quote from: Kays on October 03, 2017, 10:12:59 pm ---Can I verify, that a variable (not of a pointer type) has been intentionally assigned a value?

I've got a function here, which accepts a variant record, and I'd like to verify that the variables in the variant part weren't forgotten.

--- End quote ---
No. You can only check against an known state if there is no value/state for empty then you can't be certain that the fields of a record have values or never assigned any. This is usually address with nullable types, and there are a couple implemenation in pascal floating around.

Kays:

--- Quote from: GetMem on October 03, 2017, 10:28:08 pm ---Try something like this:
--- End quote ---
Sorry, I have to watch my language: I meant by “variant records”, records which have variant parts, not variants (the data type) per se.


--- Quote from: taazz on October 03, 2017, 11:37:11 pm ---[…]No. You can only check against an known state if there is no value/state for empty then you can't be certain that the fields of a record have values or never assigned any. […]
--- End quote ---
Hm, OK.

And, quote, “For a parameter of a simple type (i.e. not a structured type), a default value can be specified”.

molly:
You can use the default keyword to initialize your typed record with default values.

Another possibility is to always have some default values stored on each new record use. You could then check against these values to see if a field was initialized or not.

You could also opt for advanced records and add an init function/constructor and use that function to init all fields to some (other) default values.

Navigation

[0] Message Index

[#] Next page

Go to full version