Hello,
I am trying to invoke a method that sits in an object that is in a different namespace than the namespace that is doing the call. My problem is that the called object name is not known by the caller. I have prepared two examples that show what my problem is.
In both examples there is a window that has a button and a textbox; when the button is pressed the textbox shows the "Hello world" message. In the first example, since both objects are in the same namespace, the program works without a problem.
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 SameSpace
{
public partial class Form1 : Form
{
WriterDriver writerDoer;
public Form1()
{
InitializeComponent();
WriterDelegateType writerHere = new WriterDelegateType(Writer);
writerDoer = new WriterDriver(this, writerHere);
}
public void Writer()
{
textBox1.Text = "Hello world";
}
private void button1_Click(object sender, EventArgs e)
{
writerDoer.DoIt();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SameSpace
{
delegate void WriterDelegateType();
class WriterDriver
{
Form1 form1Instance;
WriterDelegateType writerDelegateInstance;
public WriterDriver(Form1 form1InstanceArg, WriterDelegateType writerDelegateInstanceArg)
{
form1Instance = form1InstanceArg;
writerDelegateInstance = writerDelegateInstanceArg;
}
public void DoIt()
{
form1Instance.Invoke(writerDelegateInstance);
}
}
}
In this other example I have no way of entering the Invoke statement in the DoIt method so pressing the button does nothing.
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;
using SecondSpace; // This has been added
namespace FirstSpace
{
public partial class Form1 : Form
{
WriterDriver writerDoer;
public Form1()
{
InitializeComponent();
WriterDelegateType writerHere = new WriterDelegateType(Writer);
writerDoer = new WriterDriver(this, writerHere);
}
public void Writer()
{
textBox1.Text = "Hello world";
}
private void button1_Click(object sender, EventArgs e)
{
writerDoer.Doit();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SecondSpace
{
public delegate void WriterDelegateType(); // Here it's public
public class WriterDriver
{
object form1Instance; // form1Instance is type object as Form1 is not known here
WriterDelegateType writerDelegateInstance;
// form1InstanceArg is type object as Form1 is not known here
public WriterDriver(object form1InstanceArg, WriterDelegateType writerDelegateInstanceArg)
{
form1Instance = form1InstanceArg;
writerDelegateInstance = writerDelegateInstanceArg;
}
public void Doit()
{
/*
* Here I would like to type the equivalent to
*
* form1Instance.Invoke(writerDelegateInstance);
*/
}
}
}
How can I Invoke the writerDelegateInstance that sits on a different namespace? I assume that I have to do some kind of type casting but I don't know what.
Thanks,
Enrique