Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Can a class see what it's instantiated as?
Message
De
09/10/2015 16:26:11
Dragan Nedeljkovich (En ligne)
Now officially retired
Zrenjanin, Serbia
 
 
À
09/10/2015 09:27:19
Information générale
Forum:
Visual FoxPro
Catégorie:
Classes - VCX
Versions des environnements
Visual FoxPro:
VFP 9 SP2
OS:
Windows XP SP2
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Desktop
Divers
Thread ID:
01625748
Message ID:
01625792
Vues:
52
>>>I have a moment of brain shortage, right now I can't see the forest for all the trees. Is it possible from within an object to know what is the name it's instantiated as? Given this code
>>>
>>>
>>>loCountries = NewObject('SqlTable','SQL.vcx')
>>>
>>>In this case I want the class SqlTable to understand that it's instantiated as loCountries. I am building a rather complicated generic class to work with SQL tables. My idea is that if I call the object for instance loCountries, my class can be smart enough to know that I want it to connect to the SQL table called Countries.
>>
>>You can use AINSTANCE, but not in Init, you should call dedicated method.
>>after the Init of the class.
>>
>>CLEAR
>>test = CREATEOBJECT([TestMe])
>>test.CheckInstance()
>>
>>DEFINE CLASS TestMe AS Custom
>>
>>
>>
>>  PROCEDURE Init
>>****        this.CheckInstance()
>>  ENDPROC 
>>  
>>  PROCEDURE CheckInstance
>>      AINSTANCE(aVars,[TestMe])
>>      DISPLAY MEMORY  LIKE aVars
>>  ENDPROC 
>>  
>>  
>>ENDDEFINE
>>
>
>No luck, the array is empty.

Probably doesn't see its own variable. You need to run ainstance() from outside the object.

Still, what you want won't work the way you want it, because while your object is instantiated the assignment still doesn't take place. Only after its .init() has returned .t. a reference to it is returned and then assigned to a variable.

You may try some kind of factory approach... say you want your table objects to be properties of your master object - then you can
this.addTable("countries")

procedure addTable(tcAlias)
loObj= NewObject('SqlTable','SQL.vcx',"", tcAlias)
this.addproperty("o"+tcAlias, loObj)
and then your master object would have an object property .oCountries; likewise, you can add these objects to a collection, array or assign them to variables, but some of these ways would require macros.

back to same old

the first online autobiography, unfinished by design
What, me reckless? I'm full of recks!
Balkans, eh? Count them.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform