C# Can';不要在另一个函数中使用函数

C# Can';不要在另一个函数中使用函数,c#,C#,我无法在我的函数中使用函数“transferfiles” 我要在哪里换这个 这个功能应该是公共功能吗?或者只能在创建新类时使用其他函数 namespace Webshopfiletransfer { public partial class Webshopfiletransfer : ServiceBase { private static System.Timers.Timer aTimer; public Webshopfiletransfe

我无法在我的函数中使用函数“transferfiles”

我要在哪里换这个

这个功能应该是公共功能吗?或者只能在创建新类时使用其他函数

namespace Webshopfiletransfer
{
    public partial class Webshopfiletransfer : ServiceBase
    {
        private static System.Timers.Timer aTimer;

        public Webshopfiletransfer()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            SetTimer();

            aTimer.Start();
        }

        private static void SetTimer()
        {
         // Create a timer with a two second interval.
            aTimer = new System.Timers.Timer(30000);
         // Hook up the Elapsed event for the timer. 
            aTimer.Elapsed += OnTimedEvent;
            aTimer.AutoReset = true;
            aTimer.Enabled = true;
        }

        private static void OnTimedEvent(Object source, ElapsedEventArgs e)
        {            
            transferfiles("download");
          //transferfiles("upload");
        }

        private void transferfiles(string modus)
        {
        }
    }
}
我无法在函数中使用函数“transferfiles”

因为您的
ontimevent()
是静态函数,而您的
transferfiles()
函数不是静态函数,所以请将
transferfiles()
函数更改为静态函数

通过将
transferfiles()
函数更改为static,将调用该函数,而不创建其类的实例

private static void transferfiles(string modus)
{
   //Your code
}

静态方法和属性无法访问非静态字段和属性 事件在其包含类型中,并且它们无法访问实例 任何对象的变量,除非它在方法中显式传递 参数


不能在静态方法中调用非静态方法。静态方法或属性只能调用同一类的其他静态方法或属性

定义静态方法或字段时,它无权访问为类定义的任何实例字段,只能使用标记为静态的字段

private static void transferfiles(string modus)
静态(C#参考):

改变

private void transferfiles(string modus)
{

}

或者您可以创建该类的实例

 private static void OnTimedEvent(Object source, ElapsedEventArgs e)
 {
        Webshopfiletransfer webshopfiletransfer  = new Webshopfiletransfer();
        webshopfiletransfer.transferfiles("download");
        //transferfiles("upload");
 }

私有函数只能在同一类中使用。 如果您想在外部(从另一个类)使用它,您必须将其更改为public

另外还要检查前面提到的静态内容。静态函数可以在不从类实例化对象的情况下使用。如果不想创建对象,请将其更改为静态。
否则,从类创建一个对象并从该对象调用transferfiles。

您应该将
transferfiles
方法标记为静态

private static void transferfiles(string modus)

在这里,
private
的函数是不相关的。