Page 1 of 3

Objects added two times: problem o misunderstanding?

Posted: Wed Jun 08, 2011 9:28 pm
by l3whmg
Hi friends.
I'm trying to find solution for some problems and for this reason I create a little fork of HMG4 with copy and paste.
I think I've found a problem or perhaps I can't understand the source code. Can you help me?

Problem: when we add the current object to its parent (to use syntax FormName:Control), I think we add two times the same object with different names.

Example: button.

Code from button.prg

Code: Select all

METHOD New( oParent ) CLASS BUTTON
   Super:New( oParent )  ====>0
   ::oQTObject := QPushButton( ::oParent:oQTObject )
   ::oFont := ::oQTObject:Font()
   ::oParent:AddData( ::cName , Self ) ====>1
Code from control.prg

Code: Select all

METHOD New( oParent ) CLASS CONTROL
   IF !::lGridCellFlag
      ::nObjectCounter++
      ::cName := '_HMG_OBJECT_' + AllTrim( Str( Int( ::nObjectCounter ) ) )   ====>0
====>0 Here, we assign the name _HMG_OBJECT_xxxxxxx
====>1 Here, we add the object named _HMG_OBJECT_xxxxxxx to its parent (ie now we can use FormName:_HMG_OBJECT_xxxxxxx)

Code from basic.prg

Code: Select all

METHOD Name( cValue ) CLASS BASIC
   IF Pcount() == 0
      RETURN ::cName
   ELSEIF Pcount() == 1
      IF ::cName != cValue
         ::cName := cValue
         ::oParent:AddData( ::cName, Self ) ====> 2
====>2 Here, we assign AND CHANGE the name. We add the object named "button1" to its parent (ie now we can use FormName:button1)

source code WITHOUT use of :Name method

Code: Select all

WITH OBJECT oWin := Window():New()
  WITH OBJECT Button():New()
  END WITH
END WITH
we have added only ONE time an object (named _HMG_OBJECT_xxxxxxx) to its parent

source code WITH use of :Name method

Code: Select all

WITH OBJECT oWin := Window():New()
  WITH OBJECT Button():New()
   :Name := "Button1"
  END WITH
END WITH
we have added, the same object, TWO times: one time named _HMG_OBJECT_xxxxxxx and another time named button1

My analysis is correct or is a misunderstanding? If it's correct, IMHO we have the risk of unnecessarily fill the memory.

Best regards and many thanks in advance for any help.

Re: Objects added two times: problem o misunderstanding?

Posted: Thu Jun 09, 2011 4:37 am
by Rathinagiri
Let me check that Luigi. Thanks for the finding.

Re: Objects added two times: problem o misunderstanding?

Posted: Thu Jun 09, 2011 9:41 am
by concentra
Hi Luigi.
I already noted tat some time ago.
I didn´t played much attention on it because I didn´t know if internally the automatic name was used or if this could be coded that way, in order to be possible to have multiple names to the same object.
I think it´s easy to change the Name method to eliminate the current name before add the new one.

Re: Objects added two times: problem o misunderstanding?

Posted: Thu Jun 09, 2011 9:57 am
by Rathinagiri
In this regard, I want to stress these points for HMG backward compatibility and also for better programming.

1. Once a window/control is defined the name of the window/control object should be a public variable which could be available to all the functions until the window/control is released.

2. Accidentally there should not be any overwritten windows/controls. ie., before creating a window/control, we must check whether there is already a window/control defined in this name.

Re: Objects added two times: problem o misunderstanding?

Posted: Thu Jun 09, 2011 12:05 pm
by l3whmg
Hi guys.
I want remember that, when I have presented my POV, I solved this problem with a rule: the name always in the New() method! In this scenario, work fine OOP and old style.
In my code I had

Code: Select all

METHOD HMGButton:New( cName, oParent )
...
// set-up name
::cName := IIF( VALTYPE( cName ) == "C", cName, "_HMG_OBJECT_" + hb_nots( ::nObjectCounter++ ) )
....
And we can loose this syntax ":Name := <name>"

For PUBLIC problem we solve inside hmg.ch:

Code: Select all

WITH OBJECT name := HMGButton:New( name....
What do you think?

Best regards

Re: Objects added two times: problem o misunderstanding?

Posted: Tue Jun 14, 2011 3:33 pm
by concentra
Hi.
Luigi, your idea of passing the name in the New() method isn´t bad, I agree.
Anyway, I added a method in basic.prg to remove the old name of a control from the parents object stack whenever it is renamed.

Re: Objects added two times: problem o misunderstanding?

Posted: Tue Jun 14, 2011 3:36 pm
by concentra
Hi Rathinagiri.
rathinagiri wrote:2. Accidentally there should not be any overwritten windows/controls. ie., before creating a window/control, we must check whether there is already a window/control defined in this name.
This can be intentional and useful in some situations, specially to change dynamically the interface at runtime.

Re: Objects added two times: problem o misunderstanding?

Posted: Tue Jun 14, 2011 3:45 pm
by Rathinagiri
Yes. I do understand about this. At that time, we can release the previous object and redefine the same again. Isn't it?

Re: Objects added two times: problem o misunderstanding?

Posted: Tue Jun 14, 2011 4:04 pm
by concentra
Is there any difference ? When the variable containing an object is assigned another object the fist one is automatically released, or not ?

Re: Objects added two times: problem o misunderstanding?

Posted: Tue Jun 14, 2011 5:51 pm
by Rathinagiri
That is what I am also saying. It will automatically be released. But, in HMG 3 it is purposefully avoided and if you want to create a new control/window already defined, first you have to release the same explicitly. Otherwise, a run time error would be shown.