Extension Methods

I have a project with a module defining extension methods, an example of which is:

Function MaxValue(Extends val as Double) As Double
  return 1.79769e+308
End Function

I want to use it like:

  Dim d as Double
  
  d = Double.MaxValue

The code completion finds the extension method and lets me insert it but if I analyse the project I get a bug “This item does not exist” on the line

 d = Double.MaxValue

whereas if I use it like

  Dim d as Double
  
  d = d.MaxValue

It seems ok.

Seems strange to have to use an instance as I really want a static/shared class extension method in this case to make the syntax look correct. Is there another way to define my extension method so that it still looks like an extension of Double but works?

Extension methods work on the instance of a class, not the class itself. I don’t know of a way to do what you are looking to do. I guess you could define your method as global and rename it “DoubleMaxValue” without the “Extends”:

dim d as double = DoubleMaxValue

Thanks Kem - that code completion got me convinced I was on the right track for a moment!
I wanted to have Double.MaxValue, Double.MinValue, Double.NaN, etc added to the class as if they were part of the original design so I could both assign and test with the same extension methods along the lines of:

d = Double.MinValue
...
if d = Double.MaxValue then...

d = Double.MaxValue
...
if d = Double.NaN then..

etc.

Maybe it is an unwanted feature in code completion to show them against the class?

Anyway I will rethink and move on.

[quote=13690:@Carl Clarke]
I wanted to have Double.MaxValue, Double.MinValue, Double.NaN, etc added to the class[/quote]

Since “Doubles” are not classes you can’t define what would amount to class methods on them via a class extension.
There are a handful of intrinsic types - single double integer color string ( and a few derivatives like Uint8 Int8 etc) that are not classes.

You CAN still create extension methods BUT you require an “instance” as mentioned.
Extension methods require an instance.