views:

20

answers:

1

Hello, this is my scenario, and I want to know if it's possible to accomplish what I intend to do:

I have a class library (made in c#) that inside has a class (named SForm) that inherits from System.Windows.Forms.Form. In that class I declare some strings, and methods to set those strings' values.

public class SForm : Form
{
  public string _myDate;
  public void setTime(string val) { _mydate = val; }
}

In another class, I make some calls to an API that trigger callbacks, and when a callback occurs, call the methods to set the values in the class that inherits from Form. All working fine, and all classes are packed in a DLL.

public class Events
{
  private SForm _form;
  public void setForm(SForm f) 
  {
    _form = f;
  }
  public void connect()
  {
    //when I call this method, connects to a device using the API, and if
    //it's succesful, triggers OnCallback...
  }
  private void OnCallback(string retVal)
  {
    _form.setTime(retVal); //this works
  }
}

Here is my problem: I have a desktop app, in VB, that uses that DLL, and when I inherit from SForm, I want that the callback triggered by the DLL invokes the method in my form

Public Class Form1
  Inherits SForm

  Private _se As Events

  Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
  Handles Me.Load
    _se = New Events()
    _se.setForm(Me)
    _se.connect()
  End Sub

  Private Overloads Sub setTime(ByVal s As String)
    MessageBox.Show(s)
  End Sub

What I need is that the callback trigger the "setTime" method in this form, and show the value sent by my DLL (I can push a button and access the value of the _myDate string, but I need it to be automatic). Is that possible? How??

Thanks a million

+1  A: 

You need to declare setTime() virtual so you can override it. Don't forget to call the base method.

Hans Passant
I don't get it... where should I call the base method? What I need is that when the API triggers OnCallback, it triggers the method on my VB form asynchronously...
You should call it from the override method. Why does this have to be asynchronous? Very tricky when you work with forms, you can only touch controls in the same thread that created them.
Hans Passant
because I don't know when the OnCallback will be triggered... I just really want to be informed when this method is called, so I thought that making OnCallback call a method on the Form class, will have it "pushed" if this form is inherited...
Yes, declare the method virtual. Have you tried it?
Hans Passant
Ok, I've got it... I declared "abstract" my SForm class as well as the method "setTime()". Now when I run my form that inherits from SForm, all calls to setTime are redirected to the method implemented in this form (using Overrides). I tried using virtual, but didn´t get the expected result. I hope my approach is correct. Thanks Hans for your help!