如何使用按钮调用此方法?

时间:2015-10-02 05:29:41

标签: c#-4.0

    public static bool WriteBeamDataToFile(string Filename, List<Part> Parts)
        {
            // Open a Streamwriter to write data to the specified Filename
            using (StreamWriter TeklaDataWriter = new StreamWriter(Filename))
            {
                // Connect to the Currently Open Tekla Model
                Model Model = new Model();
                foreach (Part CurrentPart in Parts)
                {
                    if (CurrentPart != null)
                    {
                        string Name = CurrentPart.Name;
                        string Profile = CurrentPart.Profile.ProfileString;
                        string Material = CurrentPart.Material.MaterialString;
                        string Finish = CurrentPart.Finish;

                        TeklaDataWriter.WriteLine(Name + "," + Profile + "," + Material + "," + Finish);
                    }

                }
            }

            return File.Exists(Filename);
        }

示例:

   private void button1_Click(object sender, EventArgs e)
    {
        How to call above method here?
    }

2 个答案:

答案 0 :(得分:0)

private void button1_Click(object sender, EventArgs e) {
    private bool isFileExists;
    List<Parts> partsList = new List<Parts>();
    isFileExists = WriteBeamDataToFile("example.txt",partsList)

    if(isFileExists){
       //do something..
    }
}

答案 1 :(得分:0)

上面的方法被标记为静态。这就是为什么您遇到一些问题。 静态方法可以从Class自身调用。 非静态方法可以从类实例中调用。

查看示例:

class MyClass { 
  //static method
  public static void Method1() {}
  //non static method
  public void Method2() {}
}

class MyForm:Form { 
 ...
   private void button1_Click(object sender, EventArgs e)
    {
      //here we call static method of MyClass
      MyClass.Method1(); 
    }
//or
   private void button1_Click(object sender, EventArgs e)
    {
     // Here we create an instance of MyClass
     var class = new MyClass(); 
     // and call non static Method
     class.Method2(); 
    }
  }
}