Extension Methods

This forum is meant for questions and discussions about the X# language and tools
Post Reply
Karl-Heinz
Posts: 774
Joined: Wed May 17, 2017 8:50 am
Location: Germany

Extension Methods

Post 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
User avatar
wriedmann
Posts: 3668
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Extension Methods

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
PaulB

Extension Methods

Post 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
User avatar
wriedmann
Posts: 3668
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Extension Methods

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply