Overriding a Function in a class...

This forum is meant for examples of X# code.

Anonymous

Overriding a Function in a class...

Post 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.
User avatar
Chris
Posts: 4907
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Overriding a Function in a class...

Post 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()
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
FoxProMatt

Overriding a Function in a class...

Post 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#
User avatar
lumberjack
Posts: 727
Joined: Fri Sep 25, 2015 3:11 pm
Location: South Africa

Overriding a Function in a class...

Post 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,
______________________
Johan Nel
Boshof, South Africa
Jamal
Posts: 315
Joined: Mon Jul 03, 2017 7:02 pm

Overriding a Function in a class...

Post by Jamal »

Maybe like the following:

Code: Select all

CLASS B INHERIT A
OVERRIDE METHOD X() AS VOID
     SUPER:X()
     Console.WriteLine("y") 
RETURN
User avatar
Chris
Posts: 4907
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Overriding a Function in a class...

Post 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.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
FoxProMatt

Overriding a Function in a class...

Post by FoxProMatt »

Yes. Indeed his answer with Super:X() was exactly what I was looking for.
User avatar
lumberjack
Posts: 727
Joined: Fri Sep 25, 2015 3:11 pm
Location: South Africa

Overriding a Function in a class...

Post 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
______________________
Johan Nel
Boshof, South Africa
FoxProMatt

Overriding a Function in a class...

Post 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.
User avatar
Chris
Posts: 4907
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Overriding a Function in a class...

Post 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.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
Post Reply