chen wrote:
> It work!
>
> but there are new error message "Resource clsZHSFile not
> found."<------------------------------I don't know why?
As I said earlier, your clsZHSFile class derives from TForm but has no DFM.
That is what the error is complaining about. If you really want the class
to derive from TForm without a DFM, you need to use the TForm.CreateNew()
constructor instead, which bypasses DFM streaming.
> In the clsZHSFile class, change my code to those:
>
> {code}clsZHSFile = class {code}
> {code} constructor Create();{code}
> {code}constructor clsZHSFile.Create();
That is fine. That will get rid of the resource error, and the TForm dependancy.
> clsZHSFile is a class for saving ZHS format.
Then there is no reason to derive it from TForm.
> But I don't know what the different between QT5.4 and FMX.
Everything. There are completely different frameworks, nothing in common
other than they are both designed to be cross-platform.
--
Remy Lebeau (TeamB)
Vote for best answer.
Score: 0
# Vote: 0
Date Posted: 20-Jan-2015, at 8:52 PM EST
From: Remy Lebeau (TeamB)
Re: How to access the public member in function? [Edit] [Edi
It work!
but there are new error message "Resource clsZHSFile not found."<------------------------------I don't know why?
Thanks for your reminder. I check my code about "constructor ,TComponent,...".
In the clsZHSFile class, change my code to those:
{code}clsZHSFile = class {code}
{code} constructor Create();{code}
{code}constructor clsZHSFile.Create();
begin
inherited;
// self.myMemo := '';
end; {code}
{code} MyClass := clsZHSFile.Create(); {code}
It work! ^___^ Thanks~
> That being said, why are you deriving clsZHSFile from TForm in the first
> place, if you are not going to Show() it to the user? It does not appear
> to have a DFM attached to it, either.
clsZHSFile is a class for saving ZHS format.
I have the other class like this. ex. clsAAAFile,clsBBBFile.....
Hence, I want to deriving clsZHSFile from the first place for managing my code easily.
Edited by: chen wei-lun on Jan 20, 2015 7:50 PM
Edited by: chen wei-lun on Jan 20, 2015 8:51 PM
Vote for best answer.
Score: 0
# Vote: 0
Date Posted: 20-Jan-2015, at 8:52 PM EST
From: chen wei-lun
Re: How to access the public member in function? [Edit]
chen write:
> I want to create a simple class .
>
> But it is hard for me in delphi~
The only mistake I see in this code is that you are not overriding the existing
TForm constructor. In the clsZHSFile class, change your constructor to this
instead:
{code}
constructor Create(AOwner: TComponent); override;
{code}
And make sure the implementation calls the inherited constructor:
{code}
constructor clsZHSFile.Create(AOwner: TComponent);
begin
inherited;
// self.myMemo := '';
end;
{code}
Then call this constructor like this in your other Form:
{code}
procedure TForm4.btnLoadFileClick(Sender: TObject);
var
...
MyClass: clsZHSFile;
...
begin
...
MyClass := clsZHSFile.Create(nil);
...
end;
{code}
That being said, why are you deriving clsZHSFile from TForm in the first
place, if you are not going to Show() it to the user? It does not appear
to have a DFM attached to it, either.
--
Remy Lebeau (TeamB)
Vote for best answer.
Score: 0
# Vote: 0
Date Posted: 20-Jan-2015, at 12:05 PM EST
From: Remy Lebeau (TeamB)
Re: How to access the public member in function? [Edit]
chen wrote:
> should I add the constructor in my code?
It is not a matter of ADDING a constructor. Are you CALLING a constructor
before USING the component? Again, please show the code that is actually
USING the component.
> and it looks like the below code :
>
> constructor clsZHSFile.Create;
> begin
> self.myMemo := '';
> end;
>
> But it still has an error~~~
Aside from a lack of call to the inherited constructor, there is nothing
in that code snippet that would cause an AV, unless myMemo is not being initialized
to nil correctly before the assignment is performed.
--
Remy Lebeau (TeamB)
Vote for best answer.
Score: 0
# Vote: 0
Date Posted: 19-Jan-2015, at 1:04 PM EST
From: Remy Lebeau (TeamB)
Re: How to access the public member in function? [Edit]
yep, you got me.( I know why now)
should I add the constructor in my code?
I add the code "Constructor Create();" in my code ,
and it looks like the below code :
constructor clsZHSFile.Create;
begin
self.myMemo := '';
end;
But it still has an error~~~
the error message:
First chance exception at $0000000000A56818. Exception class $C0000005 with message 'c0000005 ACCESS_VIOLATION'. Process MySew.exe (2972)
> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}
> chen wrote:
>
> > I got a error message "First chance exception at $000000000040FF2C.
> > Exception class $C0000005 with message 'c0000005 ACCESS_VIOLATION'.
>
> That suggests that you likely did not create an object instance of clsZHSFile
> before trying to access its members. You did not show the code that is actually
> using clsZHSFile.ReadFile(), but you probably did something like this:
>
> {code}
> var
> f: clsZHSFile;
> begin
> f.ReadFile(filename);
> ...
> {code}
>
> When you need to do thing instead:
>
> {code}
> var
> f: clsZHSFile;
> begin
> f := clsZHSFile.Create; // <-- add this!
> f.ReadFile(filename);
> ...
> {code}
>
> BTW, you need to add some try/finally blocks to your ReadFile() method to
> avoid memory leaks if an error occurs on a non-mobile platform:
>
> {code}
> function clsZHSFile.ReadFile(FileName: String): integer;
> var
> Stream: TFileStream;
> reader: TBinaryReader;
> begin
> Stream := TFileStream.Create(FileName, fmOpenRead);
> try
> reader := TBinaryReader.Create(Stream, TEncoding.Default, false);
> try
> Result := ReadFile_V1_1(reader);
> finally
> reader.Free;
> end;
> finally
> Stream.Free;
> end;
> end;
> {code}
>
> --
> Remy Lebeau (TeamB)
Edited by: chen wei-lun on Jan 18, 2015 8:01 PM
Vote for best answer.
Score: 0
# Vote: 0
Date Posted: 18-Jan-2015, at 8:01 PM EST
From: chen wei-lun
Re: How to access the public member in function? [Edit]
chen wrote:
> I got a error message "First chance exception at $000000000040FF2C.
> Exception class $C0000005 with message 'c0000005 ACCESS_VIOLATION'.
That suggests that you likely did not create an object instance of clsZHSFile
before trying to access its members. You did not show the code that is actually
using clsZHSFile.ReadFile(), but you probably did something like this:
{code}
var
f: clsZHSFile;
begin
f.ReadFile(filename);
...
{code}
When you need to do thing instead:
{code}
var
f: clsZHSFile;
begin
f := clsZHSFile.Create; // <-- add this!
f.ReadFile(filename);
...
{code}
BTW, you need to add some try/finally blocks to your ReadFile() method to
avoid memory leaks if an error occurs on a non-mobile platform:
{code}
function clsZHSFile.ReadFile(FileName: String): integer;
var
Stream: TFileStream;
reader: TBinaryReader;
begin
Stream := TFileStream.Create(FileName, fmOpenRead);
try
reader := TBinaryReader.Create(Stream, TEncoding.Default, false);
try
Result := ReadFile_V1_1(reader);
finally
reader.Free;
end;
finally
Stream.Free;
end;
end;
{code}
--
Remy Lebeau (TeamB)