Another problem with method override
5 posts
• Page 1 of 1
Another problem with method override
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:
I override the TestMethod in c# like this:
And this is the running example:
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
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
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
(the same, just add 'ScriptContext context' as the first parameter.)
Let me know if it works for you, Thanks!
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. | @misekjakub │ jakub@devsense.com │
-
Jakub Misek - Posts: 2092
- Joined: January 4th, 2012, 2:42 pm
- Location: Prague
Re: Another problem with method override
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
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
Great!
Just keep the arguments, and add 'ScriptContext context' as the first argument.
Thanks,
Just keep the arguments, and add 'ScriptContext context' as the first argument.
Thanks,
Jakub Misek │ DEVSENSE s.r.o. | @misekjakub │ jakub@devsense.com │
-
Jakub Misek - Posts: 2092
- Joined: January 4th, 2012, 2:42 pm
- Location: Prague
Re: Another problem with method override
Understood, thanks a lot again!!
Frank
Frank
- strfrank
- Posts: 20
- Joined: May 30th, 2012, 5:05 pm
5 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 0 guests