* * *

Recent Posts

Pages: [1] 2 3 ... 10
LCL / Re: Completion for a TEdit
« Last post by FRex on Today at 05:25:15 pm »
TComboBox was very similar to what I wanted but sometimes got in my way. In the end I replaced my TMemo by a TListBox to allow going through it with arrow keys and then handle few keys in OnKeyDown event to fill the original TEdit back. The TListBox is still filled when TEdit changes, just like TMemo was. If someone is really interested there is a link in my forum signature to the project (Botes) I'm doing this all for.
General / Re: Please advice
« Last post by taazz on Today at 05:02:16 pm »
In general it is bad to stop all exceptions like that because you are left with no information on why it failed. That is why the win32 api usually returns an error code instead of true/false. I'm assuming you have thought it over and decide that at this point plays no role in the flow of your application in which case the only thing I have to add is a way to allow you to distinguish between known and unknown errors. something along the lines of
Code: Pascal  [Select]
  1. function GetAllFields(dbFile: string): TStringArray;
  2. var
  3.   Database: TDbf;
  4.   i: Integer;
  5. begin
  6.   Database := TDbf.Create(nil);
  7.   try
  8.     with Database do
  9.       begin
  10.         FilePathFull := ExtractFileDir(dbFile);
  11.         TableName := ExtractFileName(dbFile);
  12.         Open;
  13.         i := FieldDefs.Count;
  14.         SetLength(Result, i);
  15.         for i := 0 to High(Result) do
  16.           Result[i] := FieldDefs[i].Name;
  17.         Close;
  18.       end;
  19.   except
  20.     On E:EDatabaseError do begin
  21.         //this is a database error search for the db specific problems
  22.     end;
  23.     On E:EFCreateError do begin
  24.     end;
  25.     On E:EFOpenError do begin
  26.     end;
  27.     On E:EStreamError do begin
  28.     end;
  29.     On E:Exception do begin
  30.       //log the exception somewhere for future reference.
  31.       SetLength(Result,
  32.     end;
  33.   end;
  34.   Database.Free;
  35. end;
Pay close attention to the order of the errors in the section and the error's order in the inheritance tree. The check runs from top to bottom and only executes the first check it finds, the rest will be ignored so if you put Exception on top the rest then the rest of the checks are dead code.
General / Re: Please advice
« Last post by Kays on Today at 04:55:22 pm »
Well, for starters I limit my try … except on E: exception do … finally … end blocks to where the exception may raise. I guess that's database.open here. So it should be the first statement in the try block. Also I gather all procedures,  which are supposed to be called in the end, in the finally block, even those, which don't make sense in the exception case (but don't harm, e.g. closing a file, which wasn't opened, because there was an exception). I don't know, whether that's particularly good, but it feels tidier.
Code: Pascal  [Select]
  1. function getAllFields(dbFile: string): TStringArray;
  2. var
  3.         database: TDBF;
  4.         i: integer;
  5. begin
  6.         database := TDBF.create(nil);
  7.         setLength(result, 0);
  8.         if not assigned(database) then
  9.         begin
  10.                 exit;
  11.         end;
  13.         database.filePathFull := extractFileDir(dbFile);
  14.         database.tableName := extractFileName(dbFile);
  16.         try
  17.         begin
  18.                 // may raise exception EMyHilariousException
  19.                 database.open;
  21.                 with database do
  22.                 begin
  23.                         setLength(result, fieldDefs.count);
  24.                         for i := 0 to length(result) - 1 do
  25.                         begin
  26.                                 // I haven't read the documentation, whether fieldDefs is zero-based
  27.                                 result[low(result) + i] := fieldDefs[low(fieldDefs) + i].name;
  28.                         end;
  29.                 end;
  30.         end;
  31.         except on E: EMyHilariousException do
  32.         begin
  33.         end;
  34.         finally
  35.         begin
  36.                 database.close;
  37.                 database.free;
  38.         end;
  39.         end;
  40. end;

Maybe a bad habit of mine I adapted from the assembler world is to assign a default return value at first.

I also tend to put everything into begin..end; blocks, because especially if you nest try..except-blocks it can become pretty hairy.
General / Re: Please advice
« Last post by rvk on Today at 04:44:03 pm »
Can you rely on Result being false at the beginning of the function?

Otherwise there should be a Result := false at the top.

Although... in case of an exception you wouldn't use the function-result anyway because the code would skip the result-check. It just didn't seem quite right to not set Result to false at the top but it probably doesn't matter.

General / Re: Free Pascal on the BBC micro:bit
« Last post by marcov on Today at 04:37:44 pm »
My guess is that the  -Wpnrf51822_xxaa  contains a layout and/or register file unit. However in the FPC sources I can only find nrf51 and nrf52.pp
General / Re: Please advice
« Last post by howardpc on Today at 04:34:03 pm »
I don't see a need to use exception syntax. My suggestion would be a boolean function, which if False indicates some error (possibly an exception, or some other error condition).
Code: Pascal  [Select]
  1. function GetAllDBFFields(const aDbfFilename: String; out FieldsArray: TStringArray): Boolean;
  2. var
  3.   dbf: TDbf;
  4.   i: Integer;
  5. begin
  6.   SetLength(FieldsArray, 0);
  7.   if not FileExists(aDbfFilename) then
  8.     Exit(False);
  9.   dbf:=TDbf.Create(Nil);
  10.   try
  11.     dbf.FilePathFull:=ExtractFileDir(aDbfFilename);
  12.     dbf.TableName:=ExtractFileName(aDbfFilename);
  13.     dbf.Open;
  14.     SetLength(FieldsArray, dbf.FieldDefs.Count);
  15.     for i:=0 to High(FieldsArray) do
  16.       FieldsArray[i]:=dbf.FieldDefs[i].Name;
  17.     Result:=True;
  18.   finally
  19.     dbf.Close;
  20.     dbf.Free;
  21.   end;
  22. end;
General / Free Pascal on the BBC micro:bit
« Last post by dubst3pp4 on Today at 04:12:32 pm »

I've just bought a BBC micro:bit and found small page about targeting the micro:bit in the wiki.
Has anyone experiences with it? Where does the gpio commands come from?

Best regards,
FPSpreadsheet / Re: Can I use formulas beetwen sheets?
« Last post by wp on Today at 04:06:31 pm »
No. Maybe in the future, I don't know...
FPSpreadsheet / Can I use formulas beetwen sheets?
« Last post by Tavr on Today at 03:51:03 pm »
Hello, can I use formulas like Sheet1!B1=Sheet2!B2+Sheet3!B3?
General / Re: can i make server?
« Last post by rvk on Today at 03:43:37 pm »
Code: Pascal  [Select]
  1.  repeat
  2.     S := ASocket.RecvString(120000);
  3.     WriteLn(S);
  4.     ASocket.SendString('Ok' + CRLF);
  5.   until ASocket.lasterror <> 0;

does that mean repeat that until client connects the server?
The connection is already established before calling HandleConnection (and this loop).
So RecvString(120000) waits (with a timeout) until a complete string (terminated by CRLF) is received.
This string is printed to the console and Ok+CRLF is send back.
This step is repeated until the client is disconnected.
In that case ASocket.lastError will be <> 0 (the lasterror will be the number for "connection lost" or something) and the loop ends.

Pages: [1] 2 3 ... 10


Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus