Results 1 to 8 of 8

Thread: Using new/overrides etc methods? im a bit in a worry

  1. #1

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729

    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/

  2. #2

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    bump1
    \m/\m/

  3. #3
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    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 Mainstring[] 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 RawCommandReceivedstring command )
            {
                
    //do nothing, subclass must provide implementation
            
    }
        }

        public class 
    MySuperIrcPlugin IrcPluginBase 
        
    {
            public 
    override void RawCommandReceivedstring command )
            {
                
    Console.WriteLine"{0}'s command was:{1}"this.GetType(), command );
            }
        }
        public class 
    MyCrazyIrcPlugin IrcPluginBase 
        
    {
            public 
    override void RawCommandReceivedstring command )
            {            
                
    Console.WriteLine"{0}'s command was:{1}"this.GetType(), command );
            }
        }


  4. #4

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    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/

  5. #5
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    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.

  6. #6
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    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.
        
    }


  7. #7

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729

    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/

  8. #8

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    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
  •  



Click Here to Expand Forum to Full Width