-5

Helo, I have the following stuff: selectedPanel should get the clicked panel object, and if the form is clicked, selectedPanel should be "null", nada, empty, etc :)

var
     selectedBlock: Tpanel; <== fixed typo

...

procedure TForm1.stubPanelMouseDown(Sender: TObject...
begin

    ...

    Panel:= Sender as TPanel;

    if (*selectedBlock is not null*) then
    begin
            // ie, store the current panel
            selectedBlock:= Panel;
    end
    else
    begin
            // empty the selection
            *selectedBlock:= null*;
    end;

So the question is: how I set that variable to "null"? Doing selectedBlock:= Unassigned throws me an error.

Thanks

EDIT: this still throws an error: access violation

if (selectedBlock=nil) then <= fixed and works
  begin
    selectedBlock:= Panel;
  end
  else
  begin
     selectedBlock:= nil;
 end;
Aram Alvarez
  • 536
  • 6
  • 21

1 Answers1

5

Pointers are set to "null" using the nil constant:

selectedBlock := nil;

Null is a special value that only applies to Variant and OleVariant.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477
  • It is still throwing me an access violation error when I do as the coded I added to the original post... – Aram Alvarez Jun 07 '14 at 00:15
  • 1
    It appears that you have two variables: `selectedBlock` and `selectedPanel`, and you're accidentally using one when you mean the other. – Mason Wheeler Jun 07 '14 at 00:33
  • shooo! sorry, I mistyped the code here...it is ok in the project and still nada :( – Aram Alvarez Jun 07 '14 at 01:10
  • 1
    If you don't show the real code you have, it is not possible to see if an error is due to a typo or due to something else. Setting `selectedBlock` to `nil` is correct. If you gat an AV, then you are perhaps accessing the `selectedBlock` after it was set to `nil`. First check if it is `Assigned(selectedBlock)` before accessing it. – Rudy Velthuis Jun 07 '14 at 12:25
  • FWIW, I would advise you to read the Delphi Language Guide (or Object Pascal Language Guide, depending on your version of Delphi) before you attempt to do anything non-trivial with the language. The Guide is part of the supplied help files (see Contents). – Rudy Velthuis Jun 07 '14 at 12:27