Re: String access error in class



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?
.



Relevant Pages

  • Re: Create new object with the same subsubclass
    ... classtype of TObject. ... you need to use a virtual constructor and a class ... this is resolved to a class reference in a variable of TComponentClass ... Create is called on that class reference, and the compiler knows ...
    (comp.lang.pascal.delphi.misc)
  • Re: ownerdrawing (on a components canvas): comitting any sins here?
    ... On the outside, when calling them, they ... >> can look like functions (when called on a class reference) or like ... But there is no var parameter in the constructor ... (There's also the hidden parameter that ends up in DX, IIRC, the flag ...
    (alt.comp.lang.borland-delphi)
  • Re: Instantiate class reference...
    ... but the constructor for TncJob is never ... Is the constructor TncObject.Create virtual? ... Of course this means that the constructors of all TncObject descendants ... an example of creation of instances through class reference). ...
    (borland.public.delphi.language.objectpascal)
  • Re: ownerdrawing (on a components canvas): comitting any sins here?
    ... On the outside, when calling them, they ... >>> can look like functions (when called on a class reference) or like ... >>> creating something to be used as Result and calling the constructor ... >(There's also the hidden parameter that ends up in DX, IIRC, the flag ...
    (alt.comp.lang.borland-delphi)
  • Re: ownerdrawing (on a components canvas): comitting any sins here?
    ... On the outside, when calling them, they ... >can look like functions (when called on a class reference) or like ... >You might get away with calling Free in a constructor. ... That has well-defined behaviour but it does not return nil. ...
    (alt.comp.lang.borland-delphi)