Checking for null date

Ruslan Zasukhin sunshine at public.kherson.ua
Sun Mar 29 14:19:07 CDT 2009


On 3/29/09 10:12 PM, "Steve Albin" <steve at steve-albin.com> wrote:

>>>>>   if VDateTime(cursor.Field(27)).Value <> nil then
>>>>>     Return  VDateTime(cursor.Field(27)).Value
>>>>>   else
>>>>>     Return nil
>>>>>   end
>>>>>  
>>>>> This returns a date field, but an invalid date which I would expect since
>>>>> I
>>>>> know the field is <NULL>.
>>>>> 
>>>>> I tried this:
>>>>> 
>>>>>   if  self.cursor.Field(27).GetString <> "" then
>>>>>     Return  VDateTime(cursor.Field(27)).Value
>>>>>   else
>>>>>     Return nil
>>>>>   end
>>>>>     
>>>>> This returns nil as expected.
>>>>> 
>>>>> I can easily change my code to check the string value, but it seems that
>>>>> the
>>>>> first way should work.  What am I doing wrong?
>>>> 
>>>> Much more easy
>>>> 
>>>>    if curs.Field(27).IsNull
>>> 
>>> That is easier.  Thanks!
>>> 
>>> But, the question was why doesn't VDateTime(cursor.Field(27)).Value return
>>> nil.  Is is becasue casting the date causes a date field to be created with
>>> unpredictable results?  This is probably an RB question more than a
>>> Valentina
>>> question...
>> 
>> No,
>> 
>> This is because WE did not promise you this !! :-)
>> 
>> Valentina PATTERN of work with NULL and value is:
>> 
>>  IF you know that you have NULLABLE field THEN
>>    
>>    * you MUST at first check
>>            fld.IsNull()        <<< this read ONE BIT from bitset file
>> 
>>    * then only if not NULL, you can touch
>>            fld.Value           <<<< reads from VALUE file,
>>                                this can be many bytes ...
>> 
>> 
> 
> I understand now.
> 
> In other words, you can only cast to VDateTime is you know that you have a
> valid VDateTime.  RB will allow casting to a null date, but the result is
> garbage.

Well, actually you can cast to DateTime and still check IsNull property,
Because VDateTime class is a child class of Vfield

    if VDateTime(cursor.Field(27)).IsNull

Aha, wait a moment :-)

There is no need write even such construction.
V4RB have in Vcursor class set of methods to get casted field immediately

    if cursor.DateTimeField(27).IsNull
    
----------------------
Another TIP

So you wrtie:

  if VDateTime(cursor.Field(27)).Value <> nil then
      Return  VDateTime(cursor.Field(27)).Value
  else
      Return nil
  end


I'd never write such code in any language,
Because it is known that TYPE CAST operation is not easy.
So why do it twice?

Better code is

    dim f27 as VDateTime

    f27 = Vcusor.DateTimeField(27)
    if not f27.IsNull then
      Return  f27.Value
    else
        ..


-- 
Best regards,

Ruslan Zasukhin
VP Engineering and New Technology
Paradigma Software, Inc

Valentina - Joining Worlds of Information
http://www.paradigmasoft.com

[I feel the need: the need for speed]




More information about the Valentina mailing list