Creating my first 'extends'

I’m using a plugin that creates a new number type (Decimal Plugin), and I wanted to make it easy to round to X decimal and get the answer as Text.

I inserted a Module, then a Method called RoundTo with this code:

Function RoundTo(extends d As Decimal, i As Integer) As Text DecSetScale(i) // it sets the number of decimals to round to Dim s As String = d.Str // the new number type can set it to String Return s.ToText End Function

And it works. I can do the following:

Dim d1 As Decimal = 1234.123456789 Label1.Text = d1.RoundTo(4) // 1234.1235

Then I was thinking of having a default value for the number of decimals if d1.RoundTo is used (without a value).

After some tests I created another method with same name (RoundTo) but without the i As Integer. It works. I can use Decimal.RoundTo and the default number of decimals or Decimal.RoundTo(i) with i As Integer = number of decimals to round to.

So the question is: is that the correct way to do it? Have 2 methods with same name, one without a parameter and the other with the one needed?

The best way to handle what you are describing is to define a default value for the parameter in the method definition.

So you would define RoundTo as:

Function RoundTo(extends d As Decimal, i As Integer = 1) As Text DecSetScale(i) // it sets the number of decimals to round to Dim s As String = d.Str // the new number type can set it to String Return s.ToText End Function

If you call the method without passing a parameter it will use the default value in the method definition.
No need for two methods, so it’s easier to maintain.

Yes. You could also give the i As Integer parameter a default value and have everything in one method:

Function RoundTo(extends d As Decimal, i As Integer = 4) As Text

Thank you Jared and Andrew.

Exactly that is what I wanted to know. No need for 2 methods.

I just tested and it works great. I created a constant kRound and used that instead and of course it works too.

Alberto, its nice to see someone enjoying learning Xojo, its a refreshing change :slight_smile:

Thank you Julian.

Learning Xojo is hard and fun for me. Finding ways to become a “lazy programmer” is exciting.

Now for my second extends.

I see there is a NthField for String but I can’t find one for Text. So I created my second extends called Xword:

Function Xword(extends t As Text, d As Text = " ", i As Integer = 1) As Text Dim tA() As Text tA = t.Split(d) If i > 0 And i <= UBound(tA) + 1 Then Return tA(i-1) Return "" End Function

I did some tests and everything works as I expected. I hope my expectations are correct :slight_smile:

Dim txtTest As Text = "This is only a Test, testing." MsgBox txtTest.Xword // This MsgBox txtTest.Xword(",") // This is only a Test MsgBox txtTest.Xword(" ", 5) // Test,

I read NthField doc. And it say:

Syntax result=NthField(source, separator, fieldNumber) OR result=stringVariable.NthField(separator, fieldNumber)

With the extends I have:
textVariable.Xword(separator, fieldNumber)

but can’t use Xword as Xword(source, separator, fieldNumber)

I created a new method called Xword but without extends (same code):

Function Xword(t As Text, d As Text = " ", i As integer = 1) As Text

and now I can have result = Xword(source, separator, fieldNumber)

Anyway, I think I will only use Text.Xword

Just make two methods

Function Xword(t As Text, d As Text = " ", i As integer = 1) As Text 
Function Xword(Extends t As Text, d As Text = " ", i As integer = 1) As Text 

As Shao says - the method signature then identifies which you are calling.

Sorry, I think I wasn’t clear.

After re-reading my post it looks like I changed Xword with extends to the other method without extends, when in fact I added a method with same name, it looks like this:

Even my last words “Anyway, I think I will only use Text.Xword”, seems to say that I only need one, not both, and I wasn’t able to make both work. Sorry for that.

Thank you Shao and Tim.