xsharp.eu • Overriding a Function in a class...
Page 1 of 2

Overriding a Function in a class...

Posted: Wed Apr 10, 2019 12:32 am
by Anonymous
When you override a Function in X#, what is the code required to call the base function from your override, and may need to pass along parameters as well.

Overriding a Function in a class...

Posted: Wed Apr 10, 2019 2:03 am
by Chris
Depending on in which dll the function is defined:

XSharp.RT.Functions.<function name>
XSharp.VO.Functions.<function name>
XSharp.Core.Functions.<function name>

for example XSharp.RT.Functions.AAdd()

Overriding a Function in a class...

Posted: Wed Apr 10, 2019 2:39 am
by FoxProMatt
I should have been more clear in stating that I want to override a Function or Method inside of a Class definition and call the base method from my override.


In C#, overriding a method and calling its base code looks like this:

Code: Select all

class B : A
{
  override void X() { 
    base.X();
    Console.WriteLine("y"); 
  }
}
I’m trying to understand how to do this same type of construct in X#

Overriding a Function in a class...

Posted: Wed Apr 10, 2019 3:37 am
by lumberjack
Hi Matt,
FoxProMatt_MattSlay wrote:I should have been more clear in stating that I want to override a Function or Method inside of a Class definition and call the base method from my override.
In C#, overriding a method and calling its base code looks like this:

Code: Select all

class B : A
{
  override void X() { 
    base.X();
    Console.WriteLine("y"); 
  }
}
I’m trying to understand how to do this same type of construct in X#
And from what Chris said:
Depending on in which dll the function is defined:

Code: Select all

XSharp.RT.Functions.<function name>
XSharp.VO.Functions.<function name>
XSharp.Core.Functions.<function name>
for example XSharp.RT.Functions.AAdd()

Code: Select all

begin namespace XSharp.RT.VFP
static partial class Functions
    static method AAdd() as void
        XSharp.RT.Functions.AAdd()
        Console.Writeline("y")
    return
end class
end namespace
Or if you want to keep the Functional look of it:

Code: Select all

begin namespace XSharp.RT.VFP
    function AAdd() as void
        XSharp.RT.Functions.AAdd()
        Console.Writeline("y")
    return
end namespace
Ok this was quick and out of the top of my head, hopefully Chris or somebody will chirp in to show the exact details where I went wrong, but this is the basic concept.

HTH,

Overriding a Function in a class...

Posted: Wed Apr 10, 2019 3:49 am
by Jamal
Maybe like the following:

Code: Select all

CLASS B INHERIT A
OVERRIDE METHOD X() AS VOID
     SUPER:X()
     Console.WriteLine("y") 
RETURN

Overriding a Function in a class...

Posted: Wed Apr 10, 2019 12:41 pm
by Chris
Matt, just in case this wasn't clear, due to mine and Johan's misunderstanding on what you were asking, it's what Jamal mentioned what you're looking for, the use of "SUPER:" to call a method of a parent class.

Overriding a Function in a class...

Posted: Wed Apr 10, 2019 7:13 pm
by FoxProMatt
Yes. Indeed his answer with Super:X() was exactly what I was looking for.

Overriding a Function in a class...

Posted: Thu Apr 11, 2019 6:16 am
by lumberjack
Hi Matt,
FoxProMatt_MattSlay wrote:Yes. Indeed his answer with Super:X() was exactly what I was looking for.
Yes that is the short answer. Also look at what I said, I was assuming you want to "override" a X# function, with same name as in VFP, but with totally different behavior, or some extended behavior. Will also have the same effect as SUPER:X(), but with a "Functional" approach.

Code: Select all

begin namespace XSharp.RT.VFP
static partial class Functions
    static method AAdd() as void
        XSharp.RT.Functions.AAdd() // This is same behavior as SUPER:AAdd()
        Console.Writeline("y") // We extended our method/function with a "Method" approach
    return
end class
end namespace
Or if you want to keep the Functional look of it:

Code: Select all

begin namespace XSharp.RT.VFP
    function AAdd() as void
        XSharp.RT.Functions.AAdd() // Again same behavior as SUPER:AAdd()
        Console.Writeline("y") // Same extended functionality, using a functional approach
    return
end namespace

Overriding a Function in a class...

Posted: Thu Apr 11, 2019 3:15 pm
by FoxProMatt
Something that has to be considered is sublclasses of subclasses...

If you just call: XSharp.RT.Functions.AAdd() , then if you are 3 level deep in subclassing, then you will skip the other levels of the inherited classes and juMP straight to the lowest level that you are basically hard-coding to.

Whereas, if you use Super() at each subclass along the way, you can rest assured that you are not skipping another essential code in the immediate parent class.

I'm sure there could be a time when one *wanted* to have this effect, bu they way I code most of my multi-level inherited classes, I will use Super() to be sure each layer gets called.

Overriding a Function in a class...

Posted: Thu Apr 11, 2019 4:02 pm
by Chris
Calling XSharp.RT.Functions.AAdd() has absolutely nothing to do with subclassing, I mentioned it only because I misinterpreted your term "override".

The issue is that DotNet has no concept of functions (in the sense that they live "outside" of a class), so they are implemented actually as methods of a special class in each X# dll. The compiler just hides all those implementation details and allows you to simply call ALen(), while under the hood what you are really calling is XSharp.RT.Functions.AAdd(), where "XSharp.RT.Functions" is the name of that special class, and "ALen" is a method of that class.

If you had redefined ALen() in your own code:

FUNCTION ALen(a AS ARRAY) AS DWORD
// my own version of ALen() for any reason
RETURN 17 // just because :)

then any of your calls to ALen() in your code would resolve to that version of the function (so you would "override" the standard implementation of the function). In order to call the original version in the X# dll, you would need to call it with the full name mentioned in the beginning.

I hope this clears up the confusion.