访问违规Delphi程序

时间:2011-08-21 21:33:07

标签: delphi oop access-violation

请帮我处理以下代码。单击“登录”按钮后,我从delphi中获得了Access违规....由!!!

指示

登录单元

unit Log;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls,cls_LogIn,Jpeg;

type
  TForm2 = class(TForm)
    lbllogIn: TLabel;
    pnlSplitter: TPanel;
    edtUserName: TEdit;
    edtPassword: TEdit;
    lblUserName: TLabel;
    lblPassword: TLabel;
    btnLogIn: TButton;
    btnRegister: TButton;
    lblRegister: TLabel;
    edtRegName: TEdit;
    edtRegSurname: TEdit;
    edtRegPassword: TEdit;
    edtCountry: TEdit;
    edtHomelanguage: TEdit;
    btnRegisterF: TButton;
    btnCancel: TButton;
    lblRegName: TLabel;
    lblRegSurname: TLabel;
    lblRegPassword: TLabel;
    lblCountry: TLabel;
    lblHomelanguage: TLabel;
    imgLinks: TImage;
    imgRegs: TImage;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnLogInClick(Sender: TObject);
  private

  public

    { Public declarations }
  end;

var
  Form2: TForm2;
  Login : Tlogin;
implementation

uses Main;

{$R *.dfm}

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Application.Terminate;

end;

procedure TForm2.btnLogInClick(Sender: TObject);   **!!!!!!!!**
var

bUser,bPassword : Boolean;
sUser,sPassword,sMessage : string;
begin
  form1.qrymain.close  ;
Form1.Qrymain.sql.text := 'select * from Gebruikers';
form1.qryMain.open;

sUser := edtUserName.Text;
sPassword := edtpassword.text;
bUser :=  form1.qrymain.locate('Username',sUser,[]);
bPassword := form1.qrymain.locate('Password',sPassword,[]);
Login.create;
login.create(sUser,sPassword,bUser,bPassword);
sMessage:= Login.toetslog;
ShowMessage(sMessage);

end;

班级单位:

unit cls_LogIn;

interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, StdCtrls, Buttons, math, ExtCtrls;
type
Tlogin = class(Tobject)

private

Fepos :string ;
fwagwoord :string ;
Fbepos : Boolean;
fbwagwoord : Boolean;
public
constructor create; overload;
constructor Create(epos,wagwoord:String;bepos, bwagwoord: boolean); overload; 

function toetslog: string;


end;
implementation

{ Tlogin }



constructor Tlogin.Create(epos, wagwoord: String;bepos, bwagwoord: boolean);
begin
fepos := epos ;
fwagwoord := wagwoord ;
fbepos := bepos;
fbwagwoord := bwagwoord;
end;


constructor Tlogin.create;
begin
 fepos:= ' ';
 fwagwoord := ' ';
 fbepos := false;
 fbwagwoord := true;
end;

function Tlogin.toetslog;
begin

if (fbepos = True) and (fbwagwoord = True)
then
begin
result := 'Welcome '+' '+fepos
 end
else
result := 'SORRY VERKEERD HEHE'+' '+fwagwoord ;
end;


end.
end.

1 个答案:

答案 0 :(得分:6)

跳过login.create并将下一行更正为:

login := TLogin.Create(sUser,sPassword,bUser,bPassword);

登录准备就绪时,应该释放登录对象。

像这样:

login := TLogin.Create(sUser,sPassword,bUser,bPassword);
try
  sMessage := login.toetslog;
  ShowMessage(sMessage);
finally
  login.free;
end;