On 02/21/2016 11:37 AM, tha...@thaddy.com wrote:
>
>   1.
>       procedure TFrame00.ComboBoxChanged(Sender: TObject);
>   2.
>       Var
>   3.
>         x : TComboBox;
>   4.
>       begin
>   5.
>         If (Sender Is TComboBox) Then
>   6.
>         begin
>   7.
>         x := (Sender As TComboBox);
>   8.
>        
>   9.
>           case x.Name of
>  10.
>           'ComboBox01':if x.ItemIndex = -1 then x.ItemIndex :=
>       PrjIndex else
>  11.
>             begin
>  12.
>             end;
>  13.
>           'ComboBox02':if x.ItemIndex = -1 then x.ItemIndex :=
>       HubIndex else
>  14.
>             begin
>  15.
>             end;
>  16.
>           'ComboBox03':if x.ItemIndex = -1 then x.ItemIndex :=
>       RimIndex else
>  17.
>             begin
>  18.
>             end;
>  19.
>           'ComboBox04':if x.ItemIndex = -1 then x.ItemIndex :=
>       SpkIndex else
>  20.
>             begin
>  21.
>             end;
>  22.
>           end;
>  23.
>         End;
>  24.
>       end;  
>  25.
>        
>  26.
>
>  Now how much nicer it would be if you could write someting along the
> lines of:
>
>   1.
>       procedure TFrame00.ComboBoxChanged(Sender: TObject);
>   2.
>       begin
>   3.
>         If Sender Is TComboBox Then
>   4.
>         case TCombobox(sender) of
>   5.
>           Combobox1:;
>   6.
>           ComboBox2:;
>   7.
>           ComboBox3:;
>   8.
>         end;
>   9.
>       end; 
>
> In other words, use the instance pointer as an ordinal value.


The reason this does not work is that Combobox1, Combobox2, ... are not
constants. Replacing the strings in your first example with
Combobox1.Name, Combobox2.Name will also fail to compile.

Take this example:
=== example begin ===
Program Example;

{$mode ObjFPC}

Var
    C, D: TObject;
Begin   
    Case ptruint(C) of
        0: ;
        1: ;
    End;
   
    Case ptruint(C) of
        0: ;
        1: ;
        ptruint(D): ;
    End;
End.
=== example end ===

The first case will compile, the second will not, failing with a
"Constant Expression expected". So, if you would somehow know the
instance pointers at compile time, you might get away with it ;-)

-- 

Ewald

_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply via email to