RSS
 

Friday, 10 April, 2009

10 Apr

I finished the SQL editor program for the sports project. I made a stupid mistake in the program, which was preventing the program from reading all the opened SQL file. I also updated the underlying DBMS to the latest version I have. And while I was doing all this, I was also cleaning up old files. That was a mistake, since I accidentally deleted the sport program About dialog. I recreated it and the new one is even better than the old. Have you ever noticed how often that kind of thing happens?


Programming Tip:

Yesterday’s tip was written in Delphi. I will continue with them and with the language for today’s tip.

Delphi does not have any functions that will help you determine what ‘type’ a character is, e.g., is it alphabetic, numeric, alphanumeric or none of those? Let us take as an example determining whether a character is alphabetic. That includes the characters ‘A’ through ‘Z’, ‘a’ through ‘z’, the underline character and any foreign characters in the ASCII range from 128 through 254. A new programmer might do that this way:

  Result := ((Value >= 'A') and (Value <= 'Z')) or
    ((Value >= 'a') and (Value <= 'z')) or
    (Value = '_') or
    ((Value >= #128) and (Value <= #254));

That is in perfect accord with yesterday’s tip, but there is a much simpler and more elegant way to do it:

  Result := ( Value in ['A'..'Z','a'..'z','_',#128..#254] );

Using ranges can make your code much more legible. Here is a Delphi unit with three of the functions I mentioned:


  char_funcs

  unit Unit1;

  interface

  function IsAlpha( Value: Char ) : Boolean;
  function IsNum( Value: Char ) : Boolean;
  function IsAlphaNum( Value: Char ) : Boolean;

  implementation

  function IsAlpha( Value: Char ) : Boolean;
  begin
    Result := ( Value in ['A'..'Z','a'..'z','_',#128..#254] );
  end;

  function IsNum( Value: Char ) : Boolean;
  begin
    Result := ( Value in ['0'..'9', '-', '.' );
  end;

  function IsAlphaNum( Value: Char ) : Boolean;
  begin
    Result := ( IsAlpha( Value ) OR IsNum( Value ) );
  end;

  end
.


Silly sign of the day:


Free/Open Source Software

RIAA/MPAA/IFPI/BSA/SIIA/FCC

Stupid Patent Tricks, Copyright and Other IP Nonsense

Local and Other News

 
Comments Off

Posted in Uncategorized

 

Comments are closed.