Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Create cursor from object
Message
De
06/01/2012 11:37:32
 
 
À
06/01/2012 11:13:16
Information générale
Forum:
Visual FoxPro
Catégorie:
Programmation Orientée Object
Versions des environnements
Visual FoxPro:
VFP 9 SP2
Database:
Visual FoxPro
Divers
Thread ID:
01532019
Message ID:
01532322
Vues:
46
Thanks for sharing this.

It ALMOST works... I've found 2 problems with my testing:

1. String length of Character data can only be 254 max, so I added this code:
CASE m.lcType == "C"
	lnLen = Min(LEN(m.lvValue), 254)
	lcFields = m.lcFields + "(" + LTRIM(STR(lnLen)) + ")"
I suppose the real solution would be to use a Memo type in this case so that no data would be lost off the end of the string.


2. it does not handle decimals properly, if the decimal values equate to 0.

For instance, a value of 1.00 is resulting in a field of N(1), whereas it should be N(4,2). This is causing an error: "Field width or number of decimal place is invalid)"













>>Let's say I have created a simple object from Empty, and that I have added sevaral properties (and values) to it. The properties could be Integers, floats, dates, strings, logicals, etc.
>>
>>Now I want to create a cursor with fields named the same as the properties, and have 1 row in the cursor where the row has the values from the object properties.
>>
>>Basically, I want a function like this:
>>
>>CreateCursorFromObject(oMyObject, cCursor)
>
>
>loObj = CREATEOBJECT("Empty")
>ADDPROPERTY(m.loObj, "id", 1)
>ADDPROPERTY(m.loObj, "name", "test")
>ADDPROPERTY(m.loObj, "bdate", DATE(1980, 1, 1))
>
>IF CreateCursorFromObject(m.loObj, 'test')
>	SELECT test
>	BROWSE
>ENDIF
>
>*!* A very simple cursor from object function
>*!* not contain field width / error handler
>FUNCTION CreateCursorFromObject(toObject, tcCursor)
>	LOCAL ARRAY laFields[1]
>	LOCAL i, lcFields, lcField, lvValue, lcType, lcTemp, lnPos, lnLen, lnDec
>	lcFields = ""
>	FOR i = 1 TO AMEMBERS(laFields, m.toObject)
>		lcField = m.laFields[m.i]
>		lvValue = EVALUATE("m.toObject." + m.lcField)
>		lcType = VARTYPE(m.lvValue)
>		lcFields = m.lcFields + "," + m.lcField + " " + m.lcType
>		DO CASE
>		CASE m.lcType == "C"
>			lcFields = m.lcFields + "(" + LTRIM(STR(LEN(m.lvValue))) + ")"
>		CASE m.lcType $ "NY"
>			lcTemp = TRANSFORM(m.lvValue)
>			lnLen = LEN(m.lcTemp)
>			lnPos = AT(".", m.lcTemp)
>			IF m.lnPos > 0
>				lnDec = m.lnLen - m.lnPos - 1
>				lnLen = m.lnLen - m.lnDec - 1
>				lcFields = m.lcFields + "(" + LTRIM(STR(m.lnLen)) + "," + LTRIM(STR(m.lnDec)) + ")"
>			ELSE
>				lcFields = m.lcFields + "(" + LTRIM(STR(m.lnLen)) + ")"
>			ENDIF
>		CASE m.lcType $ "DT"
>		ENDCASE
>	ENDFOR
>	IF !EMPTY(m.lcFields)
>		m.lcFields = TRIM(SUBSTR(m.lcFields, 2))
>		CREATE CURSOR (m.tcCursor) ( &lcFields. )
>		APPEND BLANK
>		GATHER NAME m.toObject
>	ENDIF
>	RETURN USED(m.tcCursor)
>ENDFUNC
>
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform