1

i'd like to know if there is a way in which I can "shadow" a function, the following class is in an assembly reference of my current project, i cannot modify in any way the assembly's content.

Public Class  CoordinateSpace

Public Function FromPixelsY(ByVal y As Single) As Single
    Return (Me.m_originY + ((y * Me.ZoomY) / (Me.m_dpiY / 100.0F)))
End Function

Public Function ToPixelsY(ByVal y As Single) As Single
    Dim num2 As Single = ((y - Me.m_originY) / Me.ZoomY) * (Me.m_dpiY / 100.0F)
    Me.CheckOverflow(num2)
    Return num2
End Function  
End Class

In addition, within the assembly i have many calls in many classes like the following:

Public Class Printer
      public function testPx() as  boolean
       dim c as new CoordinateSpace
       return c.ToPixelsY(18) > 500
      end function
    End Class

For my project I need the above class to return what is in FromPixelsY when a call to ToPixelsY occurs.

Is there a way to do this? If I inherit the class and override or shadow these methods, when testPx calls function ToPixelsY, it would be actually calling CoordinateSpace method and not my new class' method.

This is in VB.net, but any .NET language for the solution is ok. Hope this was clear, thanks!

defmx
  • 13
  • 3
  • Inherit from the type and override the properties. – asawyer Aug 23 '13 at 21:05
  • `ToPixelsY` already returns a value. Do you mean you want to get rid of the 3 line implementation of `ToPixelsY` and essentially have it replaced with `Return Me.FromPixelsY(y)`? – Chris Sinclair Aug 23 '13 at 21:05
  • Yes @ChrisSinclair , that is exactly what i am trying to reach. Many classes in the assembly call this `ToPixelsY` function, but return type is not what i expect, instead, `FromPixelsY` return type is what i am looking for. – defmx Aug 23 '13 at 21:10
  • If you can't change the source code of the assembly, short of IL weaving, I don't think you can. Your best bet would be to wrap `CoordinateSpace` (and associated classes using it) with your own implementation facade. From the looks of it, I'm guessing this class hasn't been designed with extensibility in mind (that is, these methods aren't overridable), so I'm not sure of the implications of how to redesign your application usage to work around this. – Chris Sinclair Aug 23 '13 at 21:30
  • @ChrisSinclair - if the class is not sealed (`NotInheritable` in VB.NET), then you can just derive a new class from `CoordinateSpace` and then override (`Overrides` in VB.NET, `new` in C#) the method and have it call the base class' method. – Karl Anderson Aug 23 '13 at 21:36
  • @KarlAnderson: Ahh good stuff. I didn't realize VB lets you override methods not explicitly marked as `Overridable` (which is kind of weird to me; is that just the default state if not explicitly declared? Otherwise does it simply always emit `callvirt` IL calls when compiled against?) – Chris Sinclair Aug 23 '13 at 21:52
  • @ChrisSinclair - yes, in both C# and VB.NET the default for a type is that it can be inherited. – Karl Anderson Aug 23 '13 at 22:06
  • @KarlAnderson: _Inherit types_ yes, but in C# by default you can't _override_ a _method_. – Chris Sinclair Aug 23 '13 at 22:18
  • @ChrisSinclair - If you use the `new` keyword in the method, then yes you can, like this: `public new float ToPixelsY(float y)`. That would "shadow" the base class version of the method `public float ToPixelsY(float y)`. In VB.NET it is `Overrides` instead of `new`, as in C#. – Karl Anderson Aug 23 '13 at 22:23
  • @KarlAnderson: Ehhhhhhhhhh I'm not sure if you want to _hide_ the underlying method. Any APIs (including your own) which don't know about your inherited type (that is, don't have it explicitly typed against `MyDerivedType`) will _ignore_ your custom `new` methods. I'm not sure if this is a good idea (but this would depend on defmx's context for their application. Maybe it's ok, maybe it isn't!) – Chris Sinclair Aug 23 '13 at 22:26
  • @ChrisSinclair - oh I am not advocating this approach at all, just saying it is possible. Don't kill the messenger. LOL – Karl Anderson Aug 23 '13 at 22:27

1 Answers1

1
Public Class  MyCoorSpace 
 inherits CoordinateSpace



Public Overrides Function ToPixelsY(ByVal y As Single) As Single
    Return MyBase.FromPixelsY(y)
End Function  

End Class

That was inheritance

Now, decoration in case class is sealed (NotInheritable in VB.NET)

Public Class  MyCoordSpace // here of course would be nice to implement same interface as CoordinateSpace, if any 

    private  _cs as new CoordinateSpace()

Public Function ToPixelsY(ByVal y As Single) As Single
    Return _cs.FromPixelsY(y)
End Function  

End Class
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
T.S.
  • 18,195
  • 11
  • 58
  • 78
  • Ok that is a nice approach, however i need to guarantee that `MyCoordSpace` function `ToPixelsY` is called when any class in the assembly calls the base class `CoordinateSpace`; do you think it is possible? – defmx Aug 23 '13 at 22:18
  • That is not possible, of course. You can't control what is being called within referenced assembly. But you can control what called from your assembly. You need to abstract implementation of your call. Your clients must call an object via base or interface, and you must supply the object you want them to call. So, instead of calling object `CoordinateSpace` they will call object you give them instead but the underground work will be done by `CoordinateSpace` – T.S. Aug 23 '13 at 22:31