Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

oop - Access violation Delphi program

Please help me with the following code. I get a Access violation from delphi once I click the Log IN button.... Indicated by the !!!

LogIn Unit

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;

Class Unit:

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.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Skip login.create and correct next line to :

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

the login object should be freed when the login is ready as well.

Like this :

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...