xsharp.eu • Extension Methods
Page 1 of 1

Extension Methods

Posted: Tue Aug 07, 2018 4:53 pm
by Karl-Heinz
Hi Wolfgang,

i read your answer in the italian forum - with a little help of Google ;-) - about Extensions. I´ve tested it with a Vulcan class and i´m impressed how seamless it works ! Sure, you can´t access class vars, but better that route than no way to go :-)

Code: Select all

BEGIN NAMESPACE ControlExtensions 

STATIC CLASS TabcontrolExtensions

STATIC METHOD GetAllControlsFromPage( SELF o AS Tabcontrol,  iIndex AS INT ) AS ARRAY 
LOCAL symPage AS SYMBOL 
                      
	IF ( symPage := o:__GetSymbolFromIndex( iIndex - 1 )) != NULL_SYMBOL
		RETURN o:GetAllControlsFromPage( symPage ) 
		
	ELSE
		RETURN {}
		
	ENDIF 		
		
STATIC METHOD GetAllControlsFromPage( SELF o AS Tabcontrol,  symPage AS SYMBOL ) AS ARRAY 
LOCAL oWin AS Window 

	IF ( oWin := (Window) o:GetTabPage( symPage ) ) != NULL_OBJECT
 		
		RETURN oWin:GetAllChildren()
		
	ELSE 
		
		RETURN {}	
		
	ENDIF	 	
		
END CLASS 

END NAMESPACE

regards
Karl-Heinz

Extension Methods

Posted: Tue Aug 07, 2018 5:09 pm
by wriedmann
Hi Karl-Heinz,

yes, it works like a charm. I can give you a bit more code - written before the X# runtime was available:

Code: Select all

static class StringExtensions

static method IsDigitsOnly( self cString as string ) as logic
	local lReturn			as logic
	
	lReturn				:= true
	foreach cChar as Char in cString
		if cChar < '0' .or. cChar > '9'
			lReturn				:= false
			exit
		endif
	next
	
	return lReturn

static method IsDecimalDigitsOnly( self cString as string ) as logic
	local lReturn			as logic
	
	lReturn				:= true
	foreach cChar as Char in cString
		if ( cChar < '0' .or. cChar > '9' ) .and. ! cChar == '.' .and. ! cChar == ','
			lReturn				:= false
			exit
		endif
	next
	
	return lReturn

end class
I have extended several base classes, not only string, but also object:

Code: Select all

class ObjectExtensions

static method PropertyNames( self oObject as object ) as string[]
	local aReturn		as string[]
	local nLen			as int
	local nI			as int       
	local oProperties	as System.Reflection.PropertyInfo[]
                          
	oProperties		:= oObject:GetType():GetProperties()	
	nLen			:= oProperties:Length
	aReturn			:= string[]{nLen}
	for nI := 1 upto nLen
		aReturn[nI]		:= oProperties[nI]:Name
	next
	oProperties		:= null_object
		
	return aReturn	
	
end class
It is a very powerful possibility, and for sure you will find some of these extension methods also in the XSharp Tools library.

Wolfgang

Extension Methods

Posted: Thu Aug 09, 2018 6:05 pm
by PaulB
Wolfgang,

Thanks so much for sharing this.

So the "SELF" parameter declaration can extend the class. Class variables follow the behavior as if a subclass?

Paul Bartlett

Extension Methods

Posted: Thu Aug 09, 2018 6:13 pm
by wriedmann
Hi Paul,
Class variables follow the behavior as if a subclass?
unfortunately not. Extension methods are added externally to the class so they have no access to the class internals.

Wolfgang