How to extend any class

Post

Posted
Rating:
#1 (In Topic #674)
Avatar
Regular
tincho is in the usergroup ‘Regular’
Hi,
After trying several methods to extend some classes like 'String' or 'File' I found a simple way to do it.
I'm going to do the example adding the function 'Words' to the class 'String'.
The first step is to create a class called 'String' in the current project, that is String.class, then inside this file:

Code (gambas)

  1. ' Gambas class file
  2.  
  3.   Dim aString As New String[]
  4.  
  5.   s = String.Trim(s)
  6.  
  7.   If InStr(s, " ") > 0 Then
  8.     aString = Split(s, " ")
  9.     int = aString.Count
  10.   Else
  11.     If Len(s) > 0 Then
  12.       int = 1
  13.     Endif
  14.  
Regards
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
I do not think it is a good idea to name classes with the same name as already existing internal classes.
Might as well name the class StringF.class or something, not much difference between typing String.Words or StringF.Words and will avoid conflicts.

PS. here's your function shortened to 2 lines. ;)  :roll:

Code (gambas)

  1.   Return Split(s, " ").Count
  2.  
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
tincho is in the usergroup ‘Regular’

BruceSteers said

I do not think it is a good idea to name classes with the same name as already existing internal classes.
I was thinking the same thing, but so far there are no conflicts, I guess because I don't overwrite existing methods.
It's an experiment to tune some functions that I would like to add to the real String, maybe someday.
PS. here's your function shortened to 2 lines. ;)  :roll:
Great, very good code and more efficient. Thanks
<IMG src="https://imgur.com/yndQmyi.png"> </IMG>
Regards
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’

tincho said

BruceSteers said

I do not think it is a good idea to name classes with the same name as already existing internal classes.
I was thinking the same thing, but so far there are no conflicts, I guess because I don't overwrite existing methods.
It's an experiment to tune some functions that I would like to add to the real String, maybe someday.
PS. here's your function shortened to 2 lines. ;)  :roll:
Great, very good code and more efficient. Thanks
<IMG src="https://imgur.com/yndQmyi.png"> </IMG>
Regards

oops i forgot to Trim() may not be as fast now.

Code (gambas)

  1. Dim s As String = Trim(st)
  2.   Return Split(s, " ").Count
  3.  

I guess if you have no problems then go for it
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
tincho is in the usergroup ‘Regular’
oops i forgot to Trim() may not be as fast now.
Still faster that the original function: 13.617 vs 7.294
I guess if you have no problems then go for it
Hehe, that's true, although problems usually come without you calling them. Problem solving is what brought us to where we are, (I mean humanity), but it can be interpolated for programming as well. :D
Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’

tincho said

oops i forgot to Trim() may not be as fast now.
Still faster that the original function: 13.617 vs 7.294
I guess if you have no problems then go for it
Hehe, that's true, although problems usually come without you calling them. Problem solving is what brought us to where we are, (I mean humanity), but it can be interpolated for programming as well. :D

Yep lol , I think humanity needs much more bugfix than antivirus to be honest.

If you compile gambas from source (using git to pull) you can create your own branch and then add your code to String.class in gb.util

Hmm actually it looks like gb.util just does the same as you do by making a String.class so i guess your way will be sound unless you use gb.util
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Regular
tincho is in the usergroup ‘Regular’

BruceSteers said

Hmm actually it looks like gb.util just does the same as you do by making a String.class so i guess your way will be sound unless you use gb.util
Its correct, indeed my two targets are File.class & String.Class both inside gb.util
But i test in a project (tradukisto) at the same time gb.util and my classes File.class & File.class everything works perfectly.
Online now: No Back to the top
1 guest and 0 members have just viewed this.