extending picture and creating a function

I am following various examples that seem to define a whole function in the code space, but I can’t find anywhere specifically just to do that.

It seems I have to use the inspector to add parameters and properties, return types etc, but trying to emulate that is not getting me anywhere.

eg, I have this code:

Function ProportionalScale(extends Pic as Picture, Width as integer, Height as Integer) As Picture
  // Calculate scale factor
  dim factor as Double = min( Height / Pic.Height, Width / Pic.Width )
  
  // Calculate new size
  dim w as integer = Pic.Width * factor
  dim h as integer = Pic.Height * factor
  
  // create new picture
  dim NewPic as new Picture( w, h, 32 )
  
  // draw picture in the new size
  NewPic.Graphics.DrawPicture( Pic, 0, 0, w, h, 0, 0, Pic.Width, Pic.Height )
  
  // return scaled image
  Return NewPic
  
End Function

I have tried several ways but I am never able to access ProportionalScale as part of the picture object

A quick add: I have read that it needs to be a “global method” but I am not finding anything in the documentation about creating one.

It probably should be better highlighted but:

Methods declared in this way are sometimes called “class extension methods” even though they are not actually part of a class. You can use Extends only for methods in a module. Extends cannot be used to override another method. Extension methods are not virtual, since they are not part of a class.

Drag the function to ExtraFunctions and you should be good to go.

Jim

Have you read that:

https://blog.xojo.com/2020/06/17/quick-tip-centering-a-picture-on-any-graphic-context/

?

If not read it (centering an image instead of resizing image, but that display how to do what you want to do, IMHO).

An extension method must be a global method, which is a method in a Module with its scope set to global.

More information here:

[quote=497090:@Paul Lefebvre]An extension method must be a global method, which is a method in a Module with its scope set to global.

More information here:

What I’m finding most difficult with the documentation is exampled exactly by the third link you provided: https://documentation.xojo.com/api/language/extends.html

In that whole page it keeps providing example functions, but I haven’t found anywhere that I can inject a function like that. The IDE is very inflexible like that, it appears everything (as far as I have seen) needs to be entered using inspectors and attributes and sometimes hidden little settings. It would be really helpful if there was a document in the getting started area that describes what you see in examples and how that translates to the IDE, because as a newcomer, that is not entirely clear. In fact, it’s not clear at all.