Forum > Beginners

Test Variant on Null/nil gives error

(1/2) > >>

SymbolicFrank:
This gives an EVariantError: Invalid variant type cast.


--- 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";}};} ---function Test(v: Variant): Boolean;begin  Result := False;  if v = nil then Exit;  Result := True;end; procedure TForm1.Button1Click(Sender: TObject);var  i: Integer;begin  i := 0;  Test(i);end;        
I try to write a function that encapsulates things 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";}};} ---MyData : TJSONData;ThisTable: TDataSet; if not MyData.Items[i].FindPath('description').IsNull then  ThisTable.FieldByName('descr').Value := MyData.Items[i].FindPath('description').AsString;
So I have to test the value.

avk:
It seems to be 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";}};} ---MyData, Found: TJSONData;ThisTable: TDataSet; Found := MyData.Items[i].FindPath('description');if Found <> nil then  ThisTable.FieldByName('descr').Value := Found.AsString; 

SymbolicFrank:
I wrote this function for it:


--- 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";}};} ---function TForm1.AddValue(ThisTable: TDataSet; const FieldName: string;  ThisValue: Variant): Boolean;begin  Result := False;   if ThisValue = nil then Exit;  if ThisValue.IsNull then Exit;   if not Assigned(ThisTable) then  begin    Logging.Lines.Add('ERROR: Cannot add field to unassigned table!');    Exit;  end;   if ThisTable.FindField(FieldName) = nil then  begin    Logging.Lines.Add('ERROR: Field ' + Name + ' does not exist!');    Exit;  end;   ThisTable.FieldByName(FieldName).Value := ThisValue;   Result := True;end; 
But that doesn't work. Strangely enough, it tries to convert a WideString to a Double for both tests. While I test it with a variant that contains an Integer.

SymbolicFrank:
I cannot read the value anyway before testing it on NULL. So it won't work anyway.

But it's a lot of long lines this way.

wp:

--- Quote from: SymbolicFrank on October 26, 2021, 02:46:28 pm ---This gives an EVariantError: Invalid variant type cast.


--- 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";}};} ---function Test(v: Variant): Boolean;begin  Result := False;  if v = nil then Exit;  Result := True;end;        
--- End quote ---

Just to fix this:  You cannot check a variant against nil, use the function VarIsNull(v) instead (in unit variants):

--- 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 Test(v: Variant): Boolean;begin  Result := False;  if VarIsNull(v) then exit;  Result := True;end;

Navigation

[0] Message Index

[#] Next page

Go to full version