Basically I have a object class like this:

Type TSomething = Class
  Private
    Blah : Integer;
  Public
    Procedure DoSomething;
End;

Type TSecond = Class(TSomething)
  Public
    Procedure DoSomethingNew;
End;
Procedure TSecond.DoSomethingNew;
Begin
  ShowMessage(IntToStr(Blah));
End;

...and the code to call the classes is:

Try
  Something := TSomething.Create;
  Something.Blah := 5000;
Finally
  Something.Free;
End;

Try
  Second := TSecond.Create;
  Second.DoSomethingNew;
Finally
  Second.Free;
End;

Now even though I assign the value "5000" to the variable Blah in the main class, when I try to check the value of Blah in the second class it returns a null (0) value. Why is the variable wiped when I have assigned a value to it already.

How can I overcome this problem? I am new to object orientated programming so I'm not too sure why this isn't working, is it because I am freeing the memory? I was thinking of having one main class which has variables which all the sub-classes will use, but the main classes variables keep on getting wiped whenever I check them in sub-classes.

You are right. When you create a new instance of a class (with .Create), all variables (members) for that object (instance of a class) are initialized. You can give them whatever default value you want, if you use a constructor. You may want to read this.

commented: You deserve it buddy! +1

Thanks! The constructor was the solution, how do I mark this as solved? Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.