Recent

Author Topic: How to check if the KeyValue of a DBLookupComboBox is Nil?  (Read 4590 times)

panoss

  • Full Member
  • ***
  • Posts: 162
  • You only live twice
How to check if the KeyValue of a DBLookupComboBox is Nil?
« on: August 05, 2018, 09:25:21 am »
How can I check if my combobox 's KeyValue is nil? (null)
The combobox is a DBLookupComboBox.
It 's KeyValue is Variant.
Windows 10 64bit, Lazarus Version 2.2.0    FPC 3.2.2.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to check if the KeyValue of a DBLookupComboBox is Nil?
« Reply #1 on: August 05, 2018, 10:41:29 am »
How can I check if my combobox 's KeyValue is nil? (null)
The combobox is a DBLookupComboBox.
It 's KeyValue is Variant.
Code: Pascal  [Select][+][-]
  1.   if DBLookupComboBox1.keyvalue = null then
  2.     showmessage('Nothing was selected')
  3.   else
  4.     showMessage('You selected the item with Code :'+string(DBLookupComboBox1.keyvalue));
  5.  
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

panoss

  • Full Member
  • ***
  • Posts: 162
  • You only live twice
Re: How to check if the KeyValue of a DBLookupComboBox is Nil?
« Reply #2 on: August 05, 2018, 10:52:30 am »
Thanks it works, my mistake was that I was using 'nil':
Code: Pascal  [Select][+][-]
  1. if DBLookupComboBox1.keyvalue = nil then
  2.  

I thought that nil in Pascal is the null used in other languages.
Windows 10 64bit, Lazarus Version 2.2.0    FPC 3.2.2.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to check if the KeyValue of a DBLookupComboBox is Nil?
« Reply #3 on: August 05, 2018, 11:42:16 am »
Thanks it works, my mistake was that I was using 'nil':
Code: Pascal  [Select][+][-]
  1. if DBLookupComboBox1.keyvalue = nil then
  2.  

I thought that nil in Pascal is the null used in other languages.
Null is a variant constant. Since variant is a nullable record in memory the nil check is against an internal field. But in general you are correct null and nil are synonyms.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11444
  • FPC developer.
Re: How to check if the KeyValue of a DBLookupComboBox is Nil?
« Reply #4 on: August 05, 2018, 03:42:17 pm »
I thought that nil in Pascal is the null used in other languages.

It is. But you don't want the language pointer NIL, but SQL NULL here.

C NULL and SQL NULL are also two different things.

ASerge

  • Hero Member
  • *****
  • Posts: 2240
Re: How to check if the KeyValue of a DBLookupComboBox is Nil?
« Reply #5 on: August 06, 2018, 09:00:53 pm »
Code: Pascal  [Select][+][-]
  1.   if DBLookupComboBox1.keyvalue = null then
VarIsNull is more optimal and clearer.
This code
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. {$APPTYPE CONSOLE}
  3.  
  4. uses Variants;
  5.  
  6. var
  7.   V: Variant; // Unassigned;
  8. begin
  9.   if V = null then
  10.     ;
  11.   if VarIsNull(V) then
  12.     ;
  13. end.
in assembler look like:
Code: ASM  [Select][+][-]
  1. # [9] if V = null then
  2.         leaq    -32(%rbp),%rax
  3.         movq    %rax,%rcx
  4.         call    VARIANTS_$$_NULL$$VARIANT
  5.         leaq    -32(%rbp),%rdx
  6.         leaq    U_$P$PROGRAM_$$_V(%rip),%rcx
  7.         call    SYSTEM_$$_equal$VARIANT$VARIANT$$BOOLEAN
  8.         testb   %al,%al
  9. .Ll7:
  10. # [11] if VarIsNull(V) then
  11.         cmpw    $1,U_$P$PROGRAM_$$_V(%rip)
  12.         seteb   %al
  13.         testb   %al,%al

 

TinyPortal © 2005-2018