public partial class Form1 : Form
{
public disp(string strVal)
{
lbl1.text = strVal;
}
private void button1_Click(object sender, EventArgs e)
{
class1 cl = new class1;
cl.Show1("test",this);
}
}
—————- Class ————
public class class1
{
private Form frm1;
public void Show1(string xName , object xfrmObj)
{
frm1 = (form) xfrmObj;
frm1.disp(xName ); // here I am getting error .
}
}
——————————————/
here i am trying to access ‘disp’ function from class and i have pass ‘form1’ as a object , but i am getting error
The error message that I am getting is
Error 3
‘System.Windows.Forms.Form’ does not contain a definition for ‘disp’
and no extension method ‘disp’ accepting a first argument of type ‘System.Windows.Forms.Form’ could be found
(are you missing a using directive or an assembly reference?)
this syntax in vb.net is working perfect.
please help me…..
Rajesh.
you need to cast
frm1 = (Form1)xfrmObj;instead of casting it toformin yourShow1(xName,xfrmObj)method.EDIT: OP has stated in a comment that he needs this to work for several different forms.
You can make all your forms implement the same
Interface, like so:then, change your method so it casts to ICanDisplay:
}
However, as @Heinzi has noted, you should change your Show1-method to the following:
this will make the cast entirely unnecessary. The next step is to select meaningful names for your variables, functions and classes.