开发者

How to call a method in FORM class from another class but both are same namespace

开发者 https://www.devze.com 2023-01-27 08:38 出处:网络
class Form1 : Form开发者_Go百科 { public void enable() { //1st method which i want to call from another class
class Form1 : Form开发者_Go百科 
{ 
  public void enable() 
  { 
  //1st method which i want to call from another class 
  } 
  public void display() 
  { 
  //2nd method which i want to call from another class 
  } 
} 
class Buffer : signal 
{ 
  protected override Analyse() 
  { 
  //from here i want to call two functions in form class 
  } 
} 

this is how my code looks like anyone please reply this tread.........


When creating the Buffer class, you have to pass reference to the real instance of Form1 then just use that instance. Sample code:

class Form1 : Form 
{ 
  public void InitBuffer()
  {
    Buffer b = new Buffer(this);
    ...
  }

  public void enable() 
  { 
  //1st method which i want to call from another class 
  } 
  public void display() 
  { 
  //2nd method which i want to call from another class 
  } 
} 
class Buffer : signal 
{ 
  private Form1 form;

  public Buffer(Form1 parent)
  {
    form = parent;
  }
  protected override Analyse() 
  { 
    form.enable();
    form.display();
  } 
} 

You can't grab the true instance of Form1 just like that out of nowhere.

0

精彩评论

暂无评论...
验证码 换一张
取 消