PlanetSquires Forums

Support Forums => Other Software and Code => Topic started by: Christian Weilguny on September 16, 2010, 08:01:52 AM

Title: Not FF-related, but important for me
Post by: Christian Weilguny on September 16, 2010, 08:01:52 AM
Hi,

my problem this time is not a FF-problem, perhaps somebody can help me anyway:

When I define a class, I want to make all initialisation in the class method create.
First of all the data that is defining the object.

Now I have a class within is a handle defined that is basic data for another class.
In a method of the first class an instance of the second will be created.
I want to create the second class (object) and have the (existing) handle 'on Board'
Unfortunately there is no (normal) way to get this handle without defining a property, accessible for everyone, to set this data. But I want to make this handle readonly.

This time I've found a very silly 'solution':
Becouse from within a class global variables are not accessible, I make a global and a function outside the class, setting and retrieving this global. So I set the global before I create the object and in the create-method of this object I retrieve this variable. Now its necessary to clear this global immidiatly that no other process can access it.

May be someone has a smarter way to do this work.

Christian
Title: Re: Not FF-related, but important for me
Post by: Brian Chirgwin on September 16, 2010, 09:18:23 AM
Maybe the Factory method pattern  (http://en.wikipedia.org/wiki/Factory_method_pattern)or similar

Instead of the class create method, use a method to create the object and return the handle
Title: Re: Not FF-related, but important for me
Post by: Christian Weilguny on September 16, 2010, 09:50:25 AM
Hi Brian,

QuoteInstead of the class create method, use a method to create the object and return the handle
But in PB methods are always public except class methods. In VB there is the concept of the 'friend' methods. Friend methods are only accessible from the process in which the class is defined. Then I have in the same process the definition of the second class an all is fine.
In PB that's not possible. I have class methods, accessible only from within the class and I have interface-methods, accessible from everwhere, where the object is defined.
Now, I can't publish a property for a handle writeable. Evertime someone manipulates that handle from outside the object, except the creation of the object, the object will be corrupted.

Christian
Title: Re: Not FF-related, but important for me
Post by: Christian Weilguny on September 16, 2010, 05:52:36 PM
Sorry Brian,

I think, I've missunderstood your answer by first reading.

You think I shall create the first object from within the second. But it's contrary. The second shall be created within the first.
And the second shall have the handle from the first at the time of creation.

Christian
Title: Re: Not FF-related, but important for me
Post by: José Roca on September 16, 2010, 07:36:36 PM
Create a hidden interface in the second class and pass the handle to it. Being hidden, it will not be published.


#COMPILE EXE
#DIM ALL

CLASS CFoo1 GUID$("{CF69399A-0092-4898-8654-5A13AE406714}") AS COM

   INSTANCE phandle AS DWORD
   INSTANCE pIFoo2 AS IFoo2
   INSTANCE pIFoo3 AS IFoo3

   CLASS METHOD CREATE
      pIFoo2 = CLASS "CFoo2"
      pHandle = 12345
      pIFoo3 = pIFoo2
      pIFoo3.Foo3 pHandle
      pIFoo2.Foo2
   END METHOD

   INTERFACE IFoo1 GUID$("{7BC1F6EF-5E09-4609-ACA2-C85643E89185}")
      INHERIT IDispatch
      METHOD Foo1
      END METHOD
   END INTERFACE

END CLASS

CLASS CFoo2 GUID$("{96F7FDE5-D3BE-4929-A55A-09CA35F85A5A}") AS COM

   INSTANCE phandle AS DWORD

   INTERFACE IFoo2 GUID$("{3D5BF3B4-C398-4A6A-85B6-2D1ABEDFC6A4}")
      INHERIT IDispatch
      METHOD Foo2
         MSGBOX STR$(phandle)
      END METHOD
   END INTERFACE

   INTERFACE IFoo3 GUID$("{7BC1F6EF-5E09-4609-ACA2-C85643E89185}") AS HIDDEN
      INHERIT IUnknown
      METHOD Foo3 (BYVAL phndl AS DWORD)
         phandle = phndl
         ' // Now phandle is available to the methods of IFoo2
      END METHOD
   END INTERFACE

END CLASS

FUNCTION PBMAIN () AS LONG

   LOCAL pIFoo1 AS IFoo1
   pIFoo1 = CLASS "CFoo1"
   

END FUNCTION

Title: Re: Not FF-related, but important for me
Post by: Christian Weilguny on September 16, 2010, 08:16:54 PM
Thanks Jose,

HIDDEN Interface was the thing I was searching for.

Christian