Recent

Author Topic: Inheritance  (Read 1877 times)

Zvoni

  • Hero Member
  • *****
  • Posts: 3135
Inheritance
« on: April 04, 2018, 07:37:23 pm »
Hi Folks,

New to Pascal, but 18 years Background with Visual Basic, so, as the subject implies: Not used to Inheritance.

Let's say, i have a Class CMySQLDB
Code: Pascal  [Select][+][-]
  1. Type
  2.              CMySQLDB = Class
  3.  
  4.          Private
  5.              strDBName:String;
  6.              
  7.          Protected
  8.              Procedure SetDBName(Const argDBName:String);
  9.              
  10.          Public
  11.              Property DBName:String Read strDBName Write SetDBName;
  12.  

That one works, all Properties are filled and what not.

As a second, let's say, i have another class, which i want to inherit from my first class (in a different unit, but uses-clause is there)
Code: Pascal  [Select][+][-]
  1. Type
  2.              CMySQLDBConfig = Class(CMySQLDB)        
  3.  

Why is the Watch-Window showing empty "DBName" for my Second class? Do i have to assign my first class to the second to inherit the values?
Constructor?
And before you ask: No, the Variable holding the first class has not gone out of scope (neither the second variable).

EDIT: To clarify: I want to inherit the values of an already created (and filled) instance of a class
« Last Edit: April 04, 2018, 07:54:20 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11795
  • Debugger - SynEdit - and more
    • wiki
Re: Inheritance
« Reply #1 on: April 04, 2018, 07:58:50 pm »
Why is the Watch-Window showing empty "DBName" for my Second class?
You have hit a limitation of the debugger
http://wiki.lazarus.freepascal.org/GDB_Debugger_Tips#Properties

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11795
  • Debugger - SynEdit - and more
    • wiki
Re: Inheritance
« Reply #2 on: April 04, 2018, 08:22:55 pm »
Why is the Watch-Window showing empty "DBName" for my Second class? Do i have to assign my first class to the second to inherit the values?
Constructor?
And before you ask: No, the Variable holding the first class has not gone out of scope (neither the second variable).

EDIT: To clarify: I want to inherit the values of an already created (and filled) instance of a class

I see, so actually the debugger shows correct.

Yes you have to "assign" (copy each of) the values to your new class.

-------------
You need to differentiate between:
- the class (defines the structure, but does not hold data itself)
- the instance (holds the data)

Each instance has its own data. An instance does not "inherit" from another instance.

An inherited class only shares the structure.

-----------
Also note:
Code: Pascal  [Select][+][-]
  1. instance1 := instance2

This does NOT copy the instance.
It gives you 2 variables, both referring to the same instance. (there is a hidden pointer, and you only copy the pointer)

So you need to copy each field on its own.

----------
However, if you want changes in your CMySQLDB  instance, to also ALWAYS immediately be reflected in your instance of CMySQLDBConfig, then you may want to use "delegation"

That means the 2nd instance has a variable holding a reference to the 1st instance (copy of pointer).
And in the 2nd instance you fetch the data from the first instance when needed.
« Last Edit: April 04, 2018, 08:30:17 pm by Martin_fr »

Zvoni

  • Hero Member
  • *****
  • Posts: 3135
Re: Inheritance
« Reply #3 on: April 04, 2018, 09:43:39 pm »

----------
However, if you want changes in your CMySQLDB  instance, to also ALWAYS immediately be reflected in your instance of CMySQLDBConfig, then you may want to use "delegation"

That means the 2nd instance has a variable holding a reference to the 1st instance (copy of pointer).
And in the 2nd instance you fetch the data from the first instance when needed.

Ahhh. Ok, that's something i'm used to.
And, probably, i've been kind of expecting it, since i know of the problem with copying complete instances of a class.

Thx to everybody for clearing that up
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018