unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TSendMessage = function (hwnd : Longword; MSG : Longword;
      wParam : Longint; lParam : longint ) : longint; stdcall;
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    Instance : Longword;
    MySendMessage : TSendMessage;
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
const
   StartPos : Integer = 0;
   EndPos : Integer = -1;
begin
   Edit1.SetFocus;
   StartPos := Not StartPos;
   EndPos := Not EndPos;
   MySendMessage(Edit1.Handle, EM_SETSEL, StartPos, EndPos);end;procedure TForm1.FormCreate(Sender: TObject);
begin
  Instance := LoadLibrary('user32.dll');
  if( Instance <> 0 ) then
    MySendMessage := GetProcAddress( Instance, 'SendMessageA');end;procedure TForm1.FormDestroy(Sender: TObject);
begin
  FreeLibrary(Instance);
end;end.报错:
[Error] Unit1.pas(40): Left side cannot be assigned to
[Error] Unit1.pas(41): Left side cannot be assigned to
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'
即:TForm1.Button1Click方法中的StartPos := Not StartPos; EndPos := Not EndPos;
两句
为什么啊?

解决方案 »

  1.   

    //Delphi5下默认为{$J+},Delphi6下默认为{$J-},改一下就可以了~~
    procedure TForm1.Button1Click(Sender: TObject);
    {$J+}
    const
       StartPos : Integer = 0;
       EndPos : Integer = -1;
    {$J-}
      

  2.   

    Type Switch
    Syntax {$J+} or {$J-}
    {$WRITEABLECONST ON} or {$WRITEABLECONST OFF}
    Default {$J-}
    {$WRITEABLECONST OFF}
    Scope Local
    The $J directive controls whether typed constants can be modified or not. In the {$J+} state, typed constants can be modified, and are in essence initialized variables. In the {
    $J-} state, typed constants are truly constant, and any attempt to modify a typed constant causes the compiler to report an error.
    Writeable consts refers to the use of a typed const as a variable modifiable at runtime. For example:const   foo: Integer = 12;
    begin
        foo := 14;end.With $WRITEABLECONST OFF, this code produces a compile error on the assignment to the foo variable in the begin..end block.  To fix it, change the const declaration to a var declaration.In previous versions of Delphi and Borland Pascal, typed constants were always writeable, corresponding to the {$J+} state. Old source code that uses writeable typed constants must be compiled in the {$J+} state, but for new applications it is recommended that you use initialized variables and compile your code in the {$J-} state.
      

  3.   

    还有一个问题麻烦大家:StartPos, EndPos 都被定义为const,怎么还能被改变呢?