Page 1 of 1

Another problem with method override

PostPosted: May 28th, 2013, 9:50 am
by strfrank
Hi, referring to this post:

viewtopic.php?f=3&t=707&hilit=override

I have a similar problem with a method being called by a third one.
This is the php class:

Code: Select all
<?php

[\Export]
class Test
{
   function __construct()
   {
   }

    public function TestMethod()
    {
        return 5;
    }

    public function CallTestMethod()
    {
        $aa = $this->TestMethod();
        $aa = $aa + 2;
        return $aa;
    }
   
    public function RunTest()
    {
        return $this->CallTestMethod();
    }
}

?>


I override the TestMethod in c# like this:

Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PHP.Core;


namespace TestOverrideSharp
{
    class TestOverrideSharpClass : Test
    {
        public override object TestMethod()
        {
            return 7;
        }
    }
}


And this is the running example:

Code: Select all
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestOverrideSharp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string s;
            TestOverrideSharpClass c;

            c = new TestOverrideSharpClass();
            s = c.RunTest().ToString();
            MessageBox.Show(s);
        }
    }
}


The problem is that the c# TestMethod() is not being called, I expected the result to be 9 but instead it's 7,
Many thanks,
Frank

Re: Another problem with method override

PostPosted: May 28th, 2013, 10:38 am
by Jakub Misek
I see. The point is, 'object TestMethod()' is just a dummy method that calls the actual implementation of TestMethod.

This is little bit confusing. Anyway instead of object TestMethod(), override this
Code: Select all
override object TestMethod(ScriptContext context)


(the same, just add 'ScriptContext context' as the first parameter.)

Let me know if it works for you, Thanks!

Re: Another problem with method override

PostPosted: May 28th, 2013, 11:22 am
by strfrank
Hi Jakub,
Thanks for the quick answer, the ScriptContext argument worked in both the example and the original library that I'm writing, thanks!
So adding this argument is the correct practice while overriding methods? What if the original method has arguments?
Thanks again,
Frank

Re: Another problem with method override

PostPosted: May 28th, 2013, 1:10 pm
by Jakub Misek
Great!

Just keep the arguments, and add 'ScriptContext context' as the first argument.

Thanks,

Re: Another problem with method override

PostPosted: May 28th, 2013, 1:21 pm
by strfrank
Understood, thanks a lot again!!
Frank