Problem with constructor in Delphi 2011 [Edit]

Anybody helps me plz. 
{code}
type
  PQuestion = class
  public
    q: String;
    a, b, c, d: String;
    ans: byte;
  public
    constructor Create(q1, a1, b1, c1, d1: String; ans1: byte);
  end;
{code}

{code}
constructor PQuestion.Create(q1, a1, b1, c1, d1: String; ans1: byte);
begin
  q := q1;
  a := a1;
  b := b1;
  c := c1;
  d := d1;
  ans := ans1;
end;
{code}

But when I call it from another unit, I got error message: +Acess violation at address....write at....+
{code}
obj.Create('q','a','b','c','d',1);//Example
{code}
What's going on ?

Edited by: dangngoc hoangthanh on Sep 17, 2010 2:15 AM
0
dangngoc
9/17/2010 9:15:40 AM
embarcadero.delphi.win32 2183 articles. 0 followers. Follow

6 Replies
596 Views

Similar Articles

[PageSpeed] 30

dangngoc hoangthanh wrote:

> Anybody helps me plz. 
> {code}
> type
>   PQuestion = class
>   public
>     q: String;
>     a, b, c, d: String;
>     ans: byte;
>   public
>     constructor Create(q1, a1, b1, c1, d1: String; ans1: byte);
>   end;
> {code}
> ...
> But when I call it from another unit, I got error message: +Acess violation at
> address....write at....+ {code}
> obj.Create('q','a','b','c','d',1);//Example
> {code}
> What's going on ?

Shouldn't this be "PQuestion.Create..."?

BTW, it is Delphi XE.
-- 
Uwe Schuster
http://www.bitcommander.de/blog
0
Uwe
9/17/2010 10:05:38 AM
Your constructor should be:

{code}
constructor PQuestion.Create(q1, a1, b1, c1, d1: String; ans1: byte);
begin
  inherited Create;
  q := q1;
  a := a1;
  b := b1;
  c := c1;
  d := d1;
  ans := ans1;
end;
{code}
0
Lajos
9/17/2010 12:44:39 PM
Thaks. Can I ask more question. Is Self object in Delphi like this pointer in C++ ?

> {quote:title=Uwe Schuster wrote:}{quote}
> dangngoc hoangthanh wrote:
> 
> > Anybody helps me plz. 
> > {code}
> > type
> >   PQuestion = class
> >   public
> >     q: String;
> >     a, b, c, d: String;
> >     ans: byte;
> >   public
> >     constructor Create(q1, a1, b1, c1, d1: String; ans1: byte);
> >   end;
> > {code}
> > ...
> > But when I call it from another unit, I got error message: +Acess violation at
> > address....write at....+ {code}
> > obj.Create('q','a','b','c','d',1);//Example
> > {code}
> > What's going on ?
> 
> Shouldn't this be "PQuestion.Create..."?
> 
> BTW, it is Delphi XE.
> -- 
> Uwe Schuster
> http://www.bitcommander.de/blog
0
dangngoc
9/17/2010 1:11:55 PM
dangngoc hoangthanh wrote:

> Thaks. Can I ask more question. Is Self object in Delphi like this
> pointer in C++ ?

Yes. You usually only need to refer to it explicitely to resolve a
naming conflict, or pass the current instance to some other routine as
parameter.

> > > But when I call it from another unit, I got error message: +Acess
> > > violation at address....write at....+ {code}
> > > obj.Create('q','a','b','c','d',1);//Example

Typing beginner syntax error, you call the constructor on a variable,
not on a type.

  obj := PQuestion.Create(...);

Calling a constructor on an instance is actually valid, it acts like a
normal method in this case. But it does not create a new object.




-- 
Peter Below (TeamB)  
Don't be a vampire (http://slash7.com/pages/vampires), 
use the newsgroup archives :
http://codenewsfast.com
http://groups.google.com
0
Peter
9/17/2010 6:45:53 PM
<dangngoc hoangthanh> wrote in message news:287004@forums.embarcadero.com...

> But when I call it from another unit, I got error message: +Acess 
> violation at address....write at....+

That is because you are calling it wrong.

> obj.Create('q','a','b','c','d',1);//Example

That needs to be this instead:

{code:delphi}
obj := PQuestion.Create('q','a','b','c','d',1);
{code}

-- 
Remy Lebeau (TeamB)
0
Remy
9/17/2010 6:49:04 PM
Uwe Schuster wrote:

> dangngoc hoangthanh wrote:
> 
> > Anybody helps me plz. 
> > {code}
> > type
> >   PQuestion = class
> >   public
> >     q: String;
> >     a, b, c, d: String;
> >     ans: byte;
> >   public
> >     constructor Create(q1, a1, b1, c1, d1: String; ans1: byte);
> >   end;
> > {code}
> > ...
> > But when I call it from another unit, I got error message: +Acess
> > violation at address....write at....+ {code}
> > obj.Create('q','a','b','c','d',1);//Example
> > {code}
> > What's going on ?
> 
> Shouldn't this be "PQuestion.Create..."?

As Uwe points out, you need to instantiate from the type, and not the
variable.

{code}
var
  obj : PQuestion;
begin
  obj := PQuestion.Create(...
{code}

Personally, I prefer to name all class types the standard Delphi way,
and never use arguments in the constructor.  That simplifies any form
of serialization and polymorphism.  I also always use properties, so
that I can inject setters and getters if I need to.  Another variation
to avoid having to define the type in two places (variable declaration
and instantiation) - is to use a factory class function - so you can
use the variable's class function to instantiate the variable.

{code}
TQuestion = class
  private
    FQ:String;
    ...
  public
    constructor Create; virtual;
    class function Factory(const aQ, aA, aB, aC, aD: String; const
aAns: byte):TQuestion; overload; virtual;
    property Q:String read FQ write FQ;
    ...

class function TQuestion.Factory(const aQ, aA, aB, aC, aD: String;
const aAns: byte):TQuestion;
begin
  Result := TQuestion.Create;
  Result.Q := aQ;
  Result.A := aA;
  ...

var
  obj : TQuestion;
begin
  obj := obj.Factory('q', 'a', 'b', 'c', 'd', 1);
{code}


Lars F.
-- 
http://Lars.Fosdal.com - There are no stupid questions
http://delphi.fosdal.com - Delphi Programming
0
Lars
9/18/2010 1:22:51 PM
Reply:

Similar Artilces:

Delphi 2006 to Delphi XE Crystal BPL problem [Edit]
I am in the process of migrating our application from Delphi 2006 to Delphi XE and, for ease of use, I have both 2006 and XE installed on my development computer. I access Crystal Reports XI RDC in our application and so I rebuilt the BPL in XE in order to add the new package to XE. However, when I try to add the package to XE I get the error message: “Registration procedure, Craxddrt_tbl.Register in the package c:\Documents and Settings\All Uers\Documents\RADS Studio\8.0\BPL\Crystal.bpl raised exception class EFilerError: Component TDatabase can’t be registered by package crystal.bpl be...

SEPA components for Delphi with Source Code (Delphi 5
Hi all, in the european union change next year the Bankingformat to the SEPA Format. All peoples and companies must change the bankingssoftware and the costumer data form acountnummers in the new IBAN and BIC numbers. See: http://www.arma-it.de/shop/artikelueber.php?wgruppeid=211&wgruppe_offen=211 Functions: - generate SEPA XML'S - Calc IBAN - BIC Database (DE,AT and CH) Questions: vertrieb@arma-it.de PS: Bankinssoftware for Develpoers (Germany only) http://www.arma-it.de/shop/artikelueber.php?wgruppeid=212&wgruppe_offen=212 El 26/10/13 21:38, A...

Delphi XE2 / x64 to launch on 24 August 2011 @ Delphi Day 2011 Buenos Aires
Delphi XE2 / x64 to be launched on 24 August 2011 at Delphi Day 2011 Buenos Aires/Argentina http://twitter.com/#!/devstonez/status/96192928803848192 > {quote:title=Ciprian Popa wrote:}{quote} > Delphi XE2 / x64 to be launched on 24 August 2011 at Delphi Day 2011 Buenos Aires/Argentina > http://twitter.com/#!/devstonez/status/96192928803848192 Interesting. I don't want to read too much into the emphasis on x64, but it seems cross platform won't be in XE2. > {quote:title=Kyle Miller wrote:}{quote} > > {quote:title=Ciprian Popa wrote:}{quote} > > Delp...

Convert Delphi 7 to Delphi 2009 [Edit]
Hi! Please, I have a code done in Delphi 7... Now I want to convert Delphi 7 to Delphi 2009 but I'm having problems when compile the code. Error message: +[DCC Fatal Error] Far.dpr(9): F2063 Could not compile used unit 'System.pas'+ Please can somebody help me? Source Download (349KB): http://rapidshare.com/files/256191328/FileManager.rar.html Mirror Source Download: http://www.megaupload.com/?d=12GYLPT0 Password: delphi Thank u so much! (sorry for my bad english, it is not my native language) Edited by: loquax loquax on Jul 15, 2009 12:24 PM Sorry, bu...

delphi Win32 using delphi .NET dll
Hi, I'm trying to use a delphi.NET dll in delphi.WIN32. I am currently using CodeGear Delphi 2007 with version2(base version) of .NET I can get the dll to import into the WIN32 application the only problem is when i include things such as: "using Classes,DateUtils, SysUtils" in the .NET dll the win32 application will instantly hang when any of the dll functions are called. Any help would be great thanks. Also I have tried this example and it also crashes for me? http://cc.embarcadero.com/Item/22688 -Braden I also found this.. "The problem is that, wehn you instal...

Delphi.NET loading Delphi.Win32 Driver
Hi, What I'm trying to do is marshal an array of cardinal (or integer) back into managed memory from a win32 dll. I know how to pass managed memory into a win32 dll {code} var aa : array of Integer; Buffer : IntPtr; begin SetLength(aa,2); aa[0] := 1; aa[1] := 80; if not Supports(ExtractFilePath(Application.ExeName)+'Win32_Library\SDK_Driver.Win32.io', TypeOf(IMyFunctions), MyFunctions) then Exit; //loads the driver into memory. MyFunctions contains the method names found in the SDK_Driver. Buffer := Marshal.AllocHGlobal(2 * {Marshal.SystemDefaultC...

New to Delphi
Hi I am new to Delphi & want to know good resources for learning to use it. Is there a list of good Delphi learning resources (books/online tutorials) somewhere? What recommendations can anyone make for someone new to Delphi (& only a hobby programmer at the moment)? A couple of weeks ago I started with DelphiXE4 trial & have Googled for a few Delphi resources but I haven't found anything very good yet for me as a beginner. I'm just struggling along trying to learn as I go! (I'm presently struggling with DragandDrop...see [http://stackoverflow.com/questions/17232...

Converting Delphi for Win32 to Delphi .Net(Prism)
Hi, I am currently migrating a project from Delphi for Win32 to Delphi.net. Part of my code currently goes into a directory and pulls out a random file from this directory and loads the contents of the file for me. This code doesn't seem to work in Delphi.Net. It uses PString and a number of functions in SysUtils that don't seem to be present in Delphi.net's SysUtils file. If anyone can help me please, it would be greatly appreciated! Many thanks, Jonathan Mackey Jonathan Mackey a écrit : > I am currently migrating a project from Delphi for Win32 to &...

Migrate from Delphi 2007 for Win32 to Delphi XE
we use Delphi 2007 for Win32 to support legacy (32Bit) OWL-based pascal applications (yes i know it was a mistake not to switch to VCL 15 years ago). could our applications still be opened and compiled with Delphi XE? The existing projects are all plain Pascal-Code, coming back from the times of Turbo Pascal for Windows and later on Borland Pascal. Are there any improvements we could profit from (i.e IDE, Debugger)? Thanks Andrej > {quote:title=Andrej Dimic wrote:}{quote} > could our applications still be opened and compiled with Delphi XE? I'm not sure, but I guess ...

Problem with Delphi 6 to Delphi 7 upgrade
I have just purchased Delphi XE because I have an immediate need of D7 which came for free with XE. I downloaded and installed D7 and I think I applied the Update 1 that I had also downloaded. After much messing around with old components, I have finally attempted to compile my D6 project with D7. I get the following error:- [Fatal Error] apm.dpr(225): Unit ExtDlgs was compiled with a different version of ExtCtrls.TPanel I have checked the library paths all point to D7 versions of everything and now am at a loss to know what to do next. Also checked the sequence to make sure that al...

Problem using InstallAware 7 CodeGear Special Edition with Delphi 2010 [Edit]
Hi Everyone, I am trying to create an installation disk for my delphi 2010 application using the installAware that kind of comes with Delphi 2010. When I try to built it, it keeps giving me the error message:- No files matching pattern "C:\Windows\system32\\*120.bpl" and when I look into my windows\system32 folder, there the files with extension *.bpl end with *140.bpl and not *120.bpl like it was expected. Can anyone tell me if I am doing something wrong or how I can get around this. Thanks in advance. Edited by: Tat Hon Chu on Dec 3, 2009 12:55 AM > {quo...

Update Delphi 2005 to Delphi 2010, which do i need? [Edit]
Hello, we have one Delphi 2005 Professional here and want to upgrade to Delphi 2010, but we are a bit confused. Our distributor in Germany, ingram-micro offers several upgrade products: UPG DELPHI 2010 PRO - NAMED USER UPG DELPHI 2010 PRO - NAMED USER ELS UPG DELPHI 2010 PRO - NAMED USER FLEX RAD STUDIO 2010 PRO - UPG UPG RAD STUDIO 2010 PRO - NAMED ELC UPG RAD STUDIO 2010 PRO - NAMED FLEX UPG DELPHI 2010 PRO - CONCURRENT ELS UPG DELPHI 2010 PRO - CONCURRENT FLEX What do they mean exactly and which one do i need? At the moment delphi is installed on two PCs and we want to use ...

Delphi
Hi, I'm trying to make an application that connects to the specyfic device and read some data... thay are in strings... Device producer give Delphi library that works but instead of number (string with number '1234.67') I get kind of chinesse letters.. if I do AnsiString := String there is '??????d' string instead od chinnesse :-) with other string I get without last letter i.e.: 'Unknow'. http://i.imgur.com/jP47wgC.png The library was made for Delphi 7 and is open in XE5... In future I'd like to use it in FMX also... Sebastian wrote: > I&#...

Win32 program: Delphi 7 vs Delphi XE5
How is a D7 Win32 program compared to a Delphi XE5 one in terms of stability and performance? Is Delphi XE5 good enough for a big ERP project with several DLL's and hundreds of units and forms? Thanks in advance Am 26.12.2013 15:02, schrieb lior ilan: > How is a D7 Win32 program compared to a Delphi XE5 one in terms of stability > and performance? > Is Delphi XE5 good enough for a big ERP project with several DLL's and > hundreds of units and forms? > Thanks in advance > Hello, XE5 has increased functionality. Stability seems to be ok for most ...

Web resources about - Problem with constructor in Delphi 2011 [Edit] - embarcadero.delphi.win32

Von Neumann universal constructor - Wikipedia, the free encyclopedia
Three generations of machine are shown: the second has nearly finished constructing the third. The lines running to the right are the tapes of ...

Bridge Constructor Playground on the App Store on iTunes
Get Bridge Constructor Playground on the App Store. See screenshots and ratings, and read customer reviews.

Lewis Hamilton wins inaugural Russian Formula One Grand Prix as Mercedes take constructors' title
... Russian Formula One Grand Prix and extended his championship lead on Sunday in a Mercedes one-two that clinched the team's first constructors' ...

Observation wheel constructor vows to fight court writ
The construction company being sued for $6 million by the owner of the troubled Melbourne Star Observation Wheel has vowed to vigorously contest ...

Bill Shorten denies Winslow Constructors union payment deal disadvantaged workers after royal commission ...
Opposition Leader Bill denies lucrative deal he oversaw while union leader disadvantaged workers.

Chinese constructor resumes work after security assurance
Chinese constructor resumes work after security assurance People's Daily Online MOYALE, Kenya, Dec. 16 A Chinese roads construction firm, Wu ...

Colleagues Remember ‘Master Crossword Constructor’ Merl Reagle
Colleagues Remember ‘Master Crossword Constructor’ Merl Reagle

Apps of the Week: Hurricane, Bridge Constructor Medieval, Origami Live Wallpaper and more!
It's the last edition of Apps of the Week for the month of May, and we've pulled together a great list of apps from the folks here at Android ...

A Chance To Win Bridge Constructor For iPhone And iPad
... has teamed up with Headup Games , our latest AppAdvice Daily podcast sponsor, in order to offer you a chance to win a copy of Bridge Constructor ...

Price Drop: Monster Constructor
Monster Constructor 1.0 Device: iOS Universal Category: Games Price: Free, Version: 1.0 ( iTunes ) Description: Monster Constructor lets you ...

Resources last updated: 1/1/2016 6:17:16 PM