Re: String access error in class
- From: E Sterrett <bgreenf@xxxxxxxxxxxxx>
- Date: Fri, 28 Mar 2008 00:24:13 +0000
Probably calling a constructor on an object variable somewhere, instead of
on a class reference and storing the returned object in the variable.
Look for code that says 'Config.Create' instead of
'Config:=TConfig.Create'. If Config still happens to be nil, that would
cause these exact symptoms.
You don't show the code that creates the object. It seems somewhat
likely that you got it right in the console application, then messed
up the second time.
You are absolutely right, thanks.
I was using config.create instead of tconfig.create. I overlooked it because I wouldn't have expected it to get as far as it did before throwing an error.
type
TConfig = Class(TObject)
public
FileName:string;
ComputerName:String;
As a rule, fields should be private, not public.
If you need outside access, make properties. (If you don't, make
properties anyway, but make them protected. Then never touch the
fields again.)
Is there a reason for doing this? I do want everything to be readable and writable.
In this case I don't see the difference between a public variable and a property.
Try calling the inherited create first maybe?
That's a good habit but I don't think it makes a difference in this
case. (I still do think it _matters_. You should always call an
inherited constructor.)
Constructors can be called in either of two ways: on a class reference
or on an instance. The first case is handled by creating a new, blank
instance first through compiler magic, then calling the constructor
on the new instance as per the second case.
TObject.Create does nothing, so you don't lose any code by not
calling it. But that's just coincidence. Forgetting to call the
inherited constructor is dangerous and it is far better to always
call it as a matter of principle.
Gotchya.. Do I have to call it by name like: TObject.Create
Or is there a generic parent object that can be used?
.
- Follow-Ups:
- Re: String access error in class
- From: Maarten Wiltink
- Re: String access error in class
- References:
- String access error in class
- From: E Sterrett
- Re: String access error in class
- From: Jamie
- Re: String access error in class
- From: Maarten Wiltink
- String access error in class
- Prev by Date: Re: String access error in class
- Next by Date: Re: New to Delphi - Connecting to database using just code
- Previous by thread: Re: String access error in class
- Next by thread: Re: String access error in class
- Index(es):
Relevant Pages
|