In order for this site to work correctly we need to store a small file (called a cookie) on your computer. Most every site in the world does this, however since the 25th of May 2011, by law we have to get your permission first. Please abandon the forum if you disagree.

Para que este foro funcione correctamente es necesario guardar un pequeño fichero (llamado cookie) en su ordenador. La mayoría de los sitios de Internet lo hacen, no obstante desde el 25 de Marzo de 2011 y por ley, necesitamos de su permiso con antelación. Abandone este foro si no está conforme.

Property y sobrecarga de operadores.

Foro público de Xailer en español
Responder
rafa
Mensajes: 119
Registrado: Vie Ene 16, 2009 9:59 am

Property y sobrecarga de operadores.

Mensaje por rafa »

Estoy realizando una clase para Xailer desde Delphi, asi voy aprendiendo
los 2 al mismo tiempo ;-) , y estoy intentado traspasar, quizás no todo
pero si en parte, el funcionamiento de la clase TStringList, que creo
que es muy interesante para manejar listas de cadenas para Xailer.
En tema es que estoy con poco mosca con esto;
Delphi syntax:
property Strings[Index: Integer]: string; default;
C++ syntax:
__property AnsiString Strings[int Index] = {read=Get, write=Put};
Lógicamente, he intentado hacer;
// Use Strings to read or modify the string at a particular position
PROPERTY aStrings READ METHOD GetString WRITE METHOD SetString
METHOD GetString( nIndex ) CLASS TStringList
Local cString := ''
if ::GetCount() >= nIndex
cString := ::faStrings[ nIndex ]
endif
RETURN cString
METHOD SetString(nIndex, cString) CLASS TStringList
if ::GetCount() >= nIndex
::faStrings[ nIndex ] := cString
endif
RETURN NIL
Pero esto no es correcto, por que me obliga a tener que pasarlo como una
funcion, cuando lo que quiero hacer es sobrecargar el []
Pero , claro, el problema es que deberí­a ser sobrecargado el [].
METHOD aStrings() OPERATOR [] // SobreCarga de operador
Pero esto trae consigo que no funciona en Xailer, me casca en tiempo de
ejecucion , diciendo que no existe el method aStrings.
La idea básicamente es;
? objeto:aStrings[1] ---> Salte al method GetString( )
objeto:aStrings[1] := "PEPE" ---> Salte al method SetString()
Es más me gustarí­a que fuese una PROPERTY con sobrecarga de operadores ,
no se si eso es posible , ya por rizar el rizo.
¿ Alguna idea de como implementar esto ?
Saludos
Rafa Carmona
Avatar de Usuario
jfgimenez
Site Admin
Mensajes: 5707
Registrado: Lun Abr 06, 2015 8:48 pm
Contactar:

Property y sobrecarga de operadores.

Mensaje por jfgimenez »

Rafa,
no lo he probado, pero podrías intentar algo así:
METHOD aString( nIndex, uValue ) OPERATOR "[]"
...
METHOD aString( nIndex, uValue )
RETURN IIF( PCount() == 1, ::GetString( nIndex ), ::SetString( nIndex,
uValue ) )
y desarrollar los métodos GetString() y SetString() como quieras. Eso sí, el
array lo tienes que guardar en otra DATA, p.ej., siguiendo la estructura de
Xailer, FaString.
--
Un saludo,
José F. Giménez
http://www.xailer.com
http://www.xailer.info
José F. Giménez
[Equipo de Xailer / Xailer team]
http://www.xailer.com
http://www.xailer.info
rafa
Mensajes: 119
Registrado: Vie Ene 16, 2009 9:59 am

Property y sobrecarga de operadores.

Mensaje por rafa »

Jose F. Gimenez escribió:
> Rafa,
>
> no lo he probado, pero podrí­as intentar algo así­:
>
> METHOD aString( nIndex, uValue ) OPERATOR "[]"
Umm... METHOD aString( nIndex, uValue ) OPERATOR []
Lo miraré, pero seria interesante que las PROPERTY pudieran ser
sobrecargadas por los operadores. ;-)
Saludos
Rafa Carmona
rafa
Mensajes: 119
Registrado: Vie Ene 16, 2009 9:59 am

Property y sobrecarga de operadores.

Mensaje por rafa »

La sobrecarga de operadores no funciona en Xailer de esta manera.
Quizás hay otra, pero ;
METHOD aStrings( nIndex, uValue ) OPERATOR "[]"
o esto;
METHOD aStrings( nIndex, uValue ) OPERATOR []
Un simple
o := TStringList():Create()
? o:aStrings[1]
Devuelve este error;
Error BASE/1004 Message not found: TSTRINGLIST:ASTRINGS Argumentos: ()
Quizás existe alguna forma más simple, pero mi idea es intentar ser
lo más proximo a la clase de Delphi.
Dejo aqui la clase que estoy intentando portar a Xailer desde Delphi;
*
* Proyecto: tarificador
* Fichero: tstringlist.prg
* Descripción: Port desde Delphi.
* Autor:Rafa Carmona
* Fecha: 13/01/2009
*/
#include "Xailer.ch"
CLASS TStringList
PUBLIC:
// Controls whether strings are located, sorted, and identified as
duplicates in a case-sensitive or case-insensitive manner.
PROPERTY lCaseSensitive INIT .f. READ INLINE ::FlCaseSensitive WRITE
METHOD SetCaseSensitive AS LOGICAL
// Indicates the number of strings in the list.
PROPERTY nCount INIT 0 READ METHOD GetCount AS INTEGER

// TODO;
//Specifies whether duplicate strings can be added to sorted lists.
//enum TDuplicates { dupIgnore, dupAccept, dupError };
PROPERTY enumDuplicates
//TODO;
PROPERTY Objects

// Specifies whether the strings in the list should be automatically
sorted.
PROPERTY lSorted INIT .f. READ INLINE ::FlSorted WRITE METHOD
SetSorted AS LOGICAL
// Use Strings to read or modify the string at a particular position
METHOD aStrings() OPERATOR "[]" // SobreCarga de operador
METHOD Create() CONSTRUCTOR
PROTECTED:
DATA faStrings
METHOD SetCaseSensitive( lCase )
METHOD GetCount()
METHOD SetSorted( lSorted )
METHOD GetString()
METHOD SetString()
ENDCLASS
METHOD Create() CLASS TStringList
::faStrings := {"1","2"} // De prueba a piñon.
RETURN Self
METHOD SetCaseSensitive( lCase ) CLASS TStringList
RETURN ( ::flCaseSensitive := lCase )
METHOD GetCount() CLASS TStringList
RETURN Len( ::faStrings )
METHOD SetSorted( lSorted ) CLASS TStringList
RETURN ( ::fSorted := lSorted )
METHOD aStrings( nIndex, cString ) CLASS TStringList
Local cRes := NIL
if nIndex > 0
if pcount() > 1 //Hemos recibido más de un parámentro
::SetString( nIndex, cString )
else
cRes := ::GetString( nIndex )
endif
endif
RETURN cRes
METHOD GetString( nIndex ) CLASS TStringList
Local cString := ''
if ::GetCount() >= nIndex
cString := ::faStrings[ nIndex ]
endif
RETURN cString
METHOD SetString(nIndex, cString) CLASS TStringList
if ::GetCount() >= nIndex
::faStrings[ nIndex ] := cString
endif
RETURN NIL
Responder