|
-
Mar 8th, 2004, 12:16 PM
#1
Thread Starter
yay gay
Using new/overrides etc methods? im a bit in a worry
I am designing a plugin system by my own to meet my needs.
There is the "interface" for all plugins that is like this:
Code:
public abstract class IrcPluginBase {
private IrcClient _parent;
public IrcClient Parent { get { return _parent; } set { _parent = value; } }
public void SetParent(IrcClient parent) { _parent = parent; }
public virtual void RawCommandReceived(string command) {}
public void SendRawCommand(string command) {
_parent.SocketRef.Send(command);
}
public void SendText(string nick, string channel, string txt) {
}
}
then here is a plugin's 'RawCommandReceived' function:
Code:
public void RawCommandReceived(string command) {
MessageBox.Show("hey");
}
}
My main class should then do this:
Code:
foreach (IrcPluginBase plugin in _plugins) {
plugin.RawCommandReceived(msg);
}
The problem is that this last line is calling the abstract class' 'RawCommandReceived' and not the plugin's overrided one
How should I go around this? I also tried removing the virtual/override keywords and put "new" but still the same problem
\m/  \m/
-
Mar 8th, 2004, 03:59 PM
#2
Thread Starter
yay gay
\m/  \m/
-
Mar 9th, 2004, 08:37 AM
#3
Hyperactive Member
In each subclass of IrcPluginBase, assuming that RawCommandReceived is public virtual, you must override the base class and provide the subclass implementation:
PHP Code:
using System;
using System.Collections;
namespace IrcPluginBaseTest
{
public class MainApp
{
public static void Main( string[] args )
{
ArrayList plugins = new ArrayList();
plugins.Add(new MySuperIrcPlugin());
plugins.Add(new MyCrazyIrcPlugin());
foreach(IrcPluginBase plugin in plugins)
{
plugin.RawCommandReceived( "Hello World" );
}
Console.WriteLine("done");
Console.ReadLine();
}
}
public abstract class IrcPluginBase
{
public virtual void RawCommandReceived( string command )
{
//do nothing, subclass must provide implementation
}
}
public class MySuperIrcPlugin : IrcPluginBase
{
public override void RawCommandReceived( string command )
{
Console.WriteLine( "{0}'s command was:{1}", this.GetType(), command );
}
}
public class MyCrazyIrcPlugin : IrcPluginBase
{
public override void RawCommandReceived( string command )
{
Console.WriteLine( "{0}'s command was:{1}", this.GetType(), command );
}
}
}
-
Mar 9th, 2004, 10:42 AM
#4
Thread Starter
yay gay
Isn't that what the above post says?
If you're working with abstract classes and to a abstractclass.function .net will not call the derived class' function but the base class's one and until now i only found a way to go go around this :
Code:
foreach (IrcPluginBase plugin in _plugins) {
plugin.GetType().InvokeMember("RawCommandReceived", BindingFlags.InvokeMethod, null, plugin, new object[] { msg });
}
\m/  \m/
-
Mar 9th, 2004, 11:02 AM
#5
Hyperactive Member
did you run that code? it calls the derived classes method, if it was overridden, and the base class if not overridden. Actually you didn't show any of your subclasses in your post, post one.
-
Mar 9th, 2004, 11:12 AM
#6
Hyperactive Member
here's another example:
PHP Code:
using System;
using System.Collections;
namespace AbstractTest
{
public class MainApp
{
public static void Main(string[] args)
{
ArrayList items = new ArrayList();
items.Add(new MyDerivedClass());
items.Add(new MyDerivedClass2());
foreach(MyAbstractClass item in items)
{
item.SomeMethod( "Whatever" );
}
Console.WriteLine("done");
Console.ReadLine();
}
}
public abstract class MyAbstractClass
{
public virtual void SomeMethod ( string myParam )
{
Console.WriteLine( "Base method called from {0}",
this.GetType().ToString());
}
}
public class MyDerivedClass : MyAbstractClass
{
public override void SomeMethod(string myParam)
{
Console.WriteLine( "Derived method called from {0}",
this.GetType().ToString());
}
}
public class MyDerivedClass2 : MyAbstractClass
{
//don't override anything so
//the base method will be used.
}
}
-
Mar 9th, 2004, 01:14 PM
#7
Thread Starter
yay gay
Re: Using new/overrides etc methods? im a bit in a worry
then here is a plugin's 'RawCommandReceived' function:
Code:
public void RawCommandReceived(string command) {
MessageBox.Show("hey");
}
}
\m/  \m/
-
Mar 9th, 2004, 01:26 PM
#8
Thread Starter
yay gay
Yeah now it works I don't get why. I could swear I had it just like it should be and it still didnt work.
Much thanks anyways
\m/  \m/
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|