0

I am starting to learn C# and can not figure out how to call base class method from within interface method. In this particular case, I need to somehow call sendData from within onRecievemethod.

WebSocketBase.cs

using WebSocketSharp;
using System;
using System.Collections.Generic;
using System.Threading;

class WebSocketBase
{  
    private WebSocket webSocketClient = null;
    private WebSocketService webService = null;
    private string url = null;

    public WebSocketBase(string url, WebSocketService webService)
    {
        this.webService = webService;
        this.url = url;
    }

    public void start()
    {
        webSocketClient = new WebSocket(url);
        webSocketClient.OnError += new EventHandler<WebSocketSharp.ErrorEventArgs>(webSocketClient_Error);
        webSocketClient.OnOpen += new EventHandler(webSocketClient_Opened);
        webSocketClient.OnClose += new EventHandler<WebSocketSharp.CloseEventArgs>(webSocketClient_Closed);
        webSocketClient.OnMessage += new EventHandler<MessageEventArgs>(webSocketClient_MessageReceived);
        webSocketClient.ConnectAsync();
    }

    private void webSocketClient_MessageReceived(object sender, MessageEventArgs e)
    {
        webService.onReceive(e.Data);
    }

    public void send(string channel)
    {
        webSocketClient.Send(channel);
    }

    public void sendData(string data)
    {
        send("data");
    }

}

interface WebSocketService
{
    void onReceive(String msg);
}

Logic.cs

class Logic : WebSocketService
{
    public void onReceive(string msg)
    {
         Console.WriteLine("Received message")
    }
}
Ilya Cherevkov
  • 1,743
  • 2
  • 17
  • 47
  • There's no instance of `WebSocketBase` in this code. You'd need to have an instance of a given object to call a method on that object. – David Aug 02 '16 at 15:53
  • In your example the `Logic` Class only implements the interface, not the `WebSocketBase`. How can you call a base class if you are not inheriting from one? – Louis Aug 02 '16 at 15:54
  • Would you mind to provide a complete example? this seems to miss a part of the code (like where does that "webService" object that you use come from?) – Leonardo Spina Aug 02 '16 at 15:56
  • I have updated my post – Ilya Cherevkov Aug 02 '16 at 16:02

3 Answers3

3

Maybe this was not your intent (comment and I will edit) but I the simplest thing would be to extend the base type AND implement your interface.

class Logic : WebSocketBase, WebSocketService
{
    public void onReceive(string msg)
    {
         Console.WriteLine("Received message")

         // Call method defined in base
         sendData(msg);
    }
}
blins
  • 2,515
  • 21
  • 32
2

As indicated by other comments, you're not doing anything with the WebSocketBase class.

A few other stylistic points:
a) WebSocketService should be "IWebSocketService" by convention
b) If WebSocketBase's sole purpose is to be a base class you should make it abstract
c) Be sure to mark your classes and interfaces themselves with access modifiers too (not just the methods inside them)

public interface IWebSocketService
{
   void onReceive(String msg);
}

public abstract class WebSocketBase : IWebSocketService
{

...


private void webSocketClient_MessageReceived(object sender, MessageEventArgs e)
{
    webService.onReceive(e.Data);
}

  public void sendData(string data)
  {
      send("data");
  }

}

public class Logic : WebSocketBase
{
    public void onReceive(string msg)
    {
         Console.WriteLine("Received message")
    }
}
1

So it looks like your WebSocketBase has a dependency on the service. Why not make the service know about what WebSocketBase is sending it messages?

interface WebSocketService
{
    void onReceive(WebSocketBase sender, String msg);
}

class Logic : WebSocketService
{
    public void onReceive(WebSocketBase sender, string msg)
    {
         sender.send("blah");
    }
}

class WebSocketBase
{  
    ...

    private void webSocketClient_MessageReceived(object sender, MessageEventArgs e)
    {
        webService.onReceive(this, e.Data);
    }

    public void send(string channel)
    {
        webSocketClient.Send(channel);
    }

    ...    
}
Michael Coxon
  • 5,311
  • 1
  • 24
  • 51