Extension Methods

More
1 week 2 days ago #1 by Karl-Heinz
Extension Methods was created 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 :-)

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

Please Log in or Create an account to join the conversation.

More
1 week 2 days ago #2 by wriedmann
Replied by wriedmann on topic Extension Methods
Hi Karl-Heinz,

yes, it works like a charm. I can give you a bit more code - written before the X# runtime was available:
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:
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

Please Log in or Create an account to join the conversation.

More
1 week 9 hours ago #3 by PaulB
Replied by PaulB on topic Extension Methods
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

Please Log in or Create an account to join the conversation.

More
1 week 9 hours ago #4 by wriedmann
Replied by wriedmann on topic Extension Methods
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

Please Log in or Create an account to join the conversation.