Recent

Author Topic: Check against null value  (Read 305 times)

nikel

  • Full Member
  • ***
  • Posts: 121
Check against null value
« on: September 18, 2020, 10:58:17 am »
I want the check objects for null value. Here's what I tried:

Code: Pascal  [Select][+][-]
  1. if (IDispatch(ListOfFolder[RandomFileNum]) = nil) then
  2. begin
  3.    ShowException(Exception.Create('File Error: Please check if file is accessible'));
  4.    Terminate;
  5. end;

This gives me index error doesn't show exception. How can I check for null value?
« Last Edit: September 18, 2020, 11:08:05 am by nikel »

PascalDragon

  • Hero Member
  • *****
  • Posts: 2268
  • Compiler Developer
Re: Check against null value
« Reply #1 on: September 18, 2020, 02:26:56 pm »
You need to check whether RandomFileNum is inside the range of your ListOfFolder before accessing it.

Also you should not use Exception.Create to pass it along to ShowException, because the exception object will not be freed in that case. Better use MessageDlg or ShowMessage with only the string or use raise Exception.Create('...'); instead of ShowException(...) (you don't need the Terminate then either).

nikel

  • Full Member
  • ***
  • Posts: 121
Re: Check against null value
« Reply #2 on: September 18, 2020, 08:06:59 pm »
Thanks for the hint.

 

TinyPortal © 2005-2018