I recently discovered that if I have a form (say from2) which has a public delegate declared in it as such (I know the delegate is not attached to anything)
namespace SomeTest
{
public partial class Form2 : Form
{
public delegate void mydelegate(string some);
public Form2()
{ InitializeComponent();}
private void Form2_Load(object sender, EventArgs e)
{ }
}
}
Now if I pass an instance of that form to say another form (form1) as such
namespace SomeTest
{
public partial class Form1 : Form
{
Form2 fm = null;
public Form1(Form2 fm_)
{
this.fm = fm_;
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Why cant fm access the public delegate ?
}
}
}
Why can’t we go like fm.begininvoke(fm.mydelegate,"SomeParameter") I know the delegate is not attached to something but I am just curious why a public variable is not accessible?
A delegate is a type — that’s why you can access it from the class, but not from an instance. Consider the case of an enum:
Here
Baris a type, butBazis an object — soFoo.Baris valid, but notFoo.Baz. Likewise, if you have an instancevar foo = new Foo();thenfoo.Baris invalid, butfoo.Bazis ok.Your delegate type
mydelegateis likeBarin this example.If you wanted to add an instance of the delegate:
… then you’d be able to access
mydelegateImplfrom an instance ofForm2.