Another problem with method override

Discussion about the open-source Phalanger [?] project.

Another problem with method override

Postby strfrank » May 28th, 2013, 9:50 am

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
strfrank
 
Posts: 20
Joined: May 30th, 2012, 5:05 pm

Re: Another problem with method override

Postby Jakub Misek » May 28th, 2013, 10:38 am

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!
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2092
Joined: January 4th, 2012, 2:42 pm
Location: Prague

Re: Another problem with method override

Postby strfrank » May 28th, 2013, 11:22 am

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
strfrank
 
Posts: 20
Joined: May 30th, 2012, 5:05 pm

Re: Another problem with method override

Postby Jakub Misek » May 28th, 2013, 1:10 pm

Great!

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

Thanks,
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2092
Joined: January 4th, 2012, 2:42 pm
Location: Prague

Re: Another problem with method override

Postby strfrank » May 28th, 2013, 1:21 pm

Understood, thanks a lot again!!
Frank
strfrank
 
Posts: 20
Joined: May 30th, 2012, 5:05 pm


Return to Phalanger project

Who is online

Users browsing this forum: No registered users and 5 guests

User Control Panel

Login

Who is online

In total there are 5 users online :: 0 registered, 0 hidden and 5 guests (based on users active over the past 5 minutes)
Most users ever online was 151 on December 6th, 2020, 7:46 am

Users browsing this forum: No registered users and 5 guests