Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Save Object
Message
De
13/10/2008 11:52:48
Jay Johengen
Altamahaw-Ossipee, Caroline du Nord, États-Unis
 
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Titre:
Versions des environnements
Visual FoxPro:
VFP 9 SP2
OS:
Windows XP SP2
Network:
Windows 2008 Server
Database:
MS SQL Server
Divers
Thread ID:
01354561
Message ID:
01354630
Vues:
17
>>>>I have an object that I use to populate a table using SCATTER/GATHER NAME, but there is not a one-to-one field for each object property. I want to be able to create a cursor from all the properties in the object and populate it for debugging. I want to be able to see all properties for the object and all rows as it is populated during a loop. If I add a property, I want that to automatically get added as a field in the cursor.
>>>
>>>I've similar tool for arrays Array Browser file #9948. It should be good starting point.
>>
>>Is there an easy way to convert an object into a array and then keep adding rows to the array as each loop object is created?
>
>AMEMBERS(,,0) will create one dimensional array of an object properties.

My code is much more involved than the example below, but I just wanted to cross-post this information as your solution using Obj2Cursor for my other thread worked for this as well.
CLOSE DATABASES 
CLEAR ALL

USE C:\fmsrun\PROMPT\WorkList_Detail.dbf

SCAN

	SCATTER NAME oWorkListDetail
	=Obj2Cursor(oWorkListDetail,'csrTemp')
	IF !USED('csrWorkListDetail')
		SELECT * FROM csrTemp WHERE .F. INTO CURSOR csrWorkListDetail READWRITE
	ENDIF 
	SELECT csrWorkListDetail
	APPEND FROM DBF('csrTemp')
	SELECT WorkList_Detail

ENDSCAN

SELECT csrWorkListDetail
BROWSE LAST

CANCEL



PROCEDURE obj2cursor

LPARAMETERS toObject, tcCursor, tcOpt
LOCAL lcCursor, lcOpt, lvPropVal, lcPropName, lnI, laStru[1], lnRows, lcPropType

lcCursor = IIF( Empty( tcCursor) Or VarType(tcCursor) <> "C", SYS(2015), tcCursor)
lcOpt = UPPER(IIF( Empty( tcOpt) Or VarType(tcOpt) <> "C", "", tcOpt))

*!*	lcCmd = "CREATE CURSOR " + lcCursor + "("
lcCmd = "CREATE CURSOR " + lcCursor 

lnCount = AMEMBERS(laProps, toObject, 0)
DIMENSION laStru(lnCount, 16)
laStru = ""

lnRows = 0

FOR lnI=1 TO lnCount
	lcPropName = LOWER(laProps[lnI])
	lvPropVal = EVALUATE( "toObject." + lcPropName) 
	lcPropType = TYPE("toObject." + lcPropName)
	
	lnDp = 0
	DO CASE
	CASE lcPropType = "C"
		lnSize = LEN(lvPropVal)
		IF lnSize = 0
			lnSize = 1
		ENDIF 
		IF lnSize > 254
			lcPropType = "M"
			lnSize = 0
		ENDIF	
	CASE lcPropType = "L"
		lnSize = 0
	CASE lcPropType = "D"
		lnSize = 0
	CASE lcPropType = "T"
		lnSize = 0
	CASE lcPropType = "N"
		lcVal = ALLTRIM(PADL(lvPropVal,20))
		lnSize = LEN(lcVal)
		IF "." $ lcVal
			lnDp = lnSize - RAT(".", lcVal)
		ELSE	
			lcPropType = "I" 
			lnSize = 0
		ENDIF
	OTHERWISE 
		* Unsupported data types 
		LOOP
	ENDCASE		

	lnRows = lnRows + 1
	
	laStru[lnRows, 1] = lcPropName
	laStru[lnRows, 2] = lcPropType
	laStru[lnRows, 3] = lnSize
	laStru[lnRows, 4] = lnDp
	laStru[lnRows, 5] = .T.
	laStru[lnRows, 6] = .F.

	
ENDFOR

IF lnRows > 0
	DIMENSION laStru(lnRows, 16)
	* Make a cursor with fields defined by laStru
	CREATE CURSOR (lcCursor) FROM ARRAY laStru
	INSERT INTO (lcCursor) FROM NAME toObject
	llOk = .T.
ELSE
	llOk = .F.
ENDIF	

RETURN llOk
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform