overloading methods questions

when overloading a method, what items make that a unique method that the compiler can determine which one to use? parameters? return value? I remember at some point @Joe Ranieri or @Norman Palardy told me but I cant find the posting and my memory is well not there.

function bob( extends s() as string, item as string )
function bob( extends s() as string, items() as string )

why cant I do something like stringArray.bob( newString ) and it figure out which one to use?

coding late on a friday is never a good thing. should be relaxing instead. but as most of you, I rather code Xojo than relax.

sb

parameters - type & order

Scott, I think all should be fine with your code. It doesn’t work? Seems like it does here.

I had

function bob( extends i as integer, number as integer)
function bob( extends i as integer, numbers() as integer)
function bob( extends i as double, number as double)
function bob( extends i as double, numbers() as double)

as when I tried to use anInteger.bob( number ) it told me it couldnt figure out which method to use. same with anDouble.bob( number )

strange…

pretty sure it is a PEBKAC issue.

the “signature” of each overload must be different…

function bob(extends i as integer, number as integer)
function bob(extends i as integer,number as double)

those are different as one has an Integer as the 2nd paramenter, and the other is double

but number() as double and number as double are considered the same for this…

Scott, that works for me too. Sure you don’t have a typo somewhere?

Opps, I realized I misread. Ignore my previous post, but look at this screen shot. This works fine also. I’m not having any issues with Xojo resolving which method to call. All checks and runs fine. Sure there isn’t a typo in your code somewhere?

try with a literal

I didn’t think anything worked with a literal? i.e.

Print Str("Bob".Len)

doesn’t work either.

like i.Bob(123)

I did

  dim i as Integer = 10
  dim d as Double = 10.5
  dim iArray() as Integer = Array(10, 20)
  dim dArray() as Double = Array(10.5, 20.5)
  
  i.Bob(10)
  i.Bob(iArray)
  d.Bob(10.5)
  d.Bob(dArray)

That all works.

What happens if you feed it an Int64?

Type mismatch error. Expected Int64, but got Int32.

However, adding overloaded Int64 methods works fine.

Scott, any chance you were using ParamArray?

That’ll do it. If you use:

function bob(extends i as integer, number as integer)
function bob(extends i as integer, numbers() as integer)
function bob(extends i as integer, paramarray numbers() as integer)

Then Xojo will not know to call the first or 3rd method for

i.Bob(1)

If that was the problem, get rid of the first version of the method.

@Kem Tekinay I was not using a ParamArray (never used one before). I only had the 4 instances of the method “bob” (listed above).

I am about to take another freaking computer certification exam so I havent had time to look at this since yesterday afternoon. I will look at it again tomorrow (Sun) once I recover from tonight. Thanks everyone so far that has helped.

I will take some pictures and post them.

see the image attached


link

I have the image inline (I think) and the URL (incase I cant do images correctly)

the build doesnt barf/throw errors… but when I go to use it, it says it cant figure out which one.

Edit (Paul): Fixed inline image link.

What is the return value of .push(integer(), integer) ?

none of the 4 push methods have a return value. the modify the extends variable…

Scott, then that’s the issue. You are using it as a call to a method, which is expecting some return value from the .Push method.

Assert.AreEqual(expected, actual)

So what you really need is:

myAry.Push(1)

Assert.AreEqual(expected, myAry)