-1

I can't use the function "tranferfiles" in my function.

Where I have to change this?

Should the function be a public function? or can i use other functions only when I create a new class?

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)
        {
        }
    }
}
Amin Golmahalle
  • 2,911
  • 2
  • 20
  • 31
Miracle Johnson
  • 701
  • 2
  • 13
  • 28

4 Answers4

2

you cannot call a non-static method in static method.A static method or property can call only other static methods or properties of the same class.

when you define a static method or field, it does not have access to any instance fields defined for the class, it can use only fields that are marked as static.

static (C# Reference): here

change

private void transferfiles(string modus)
{

}

to

 private static void transferfiles(string modus)
 {

 }

Or you can create an instance of the class

 private static void OnTimedEvent(Object source, ElapsedEventArgs e)
 {
        Webshopfiletransfer webshopfiletransfer  = new Webshopfiletransfer();
        webshopfiletransfer.transferfiles("download");
        //transferfiles("upload");
 }
Reza Jenabi
  • 3,226
  • 23
  • 30
1

I can't use the function "tranferfiles" in my function.

Because your OnTimeEvent() is static function and your transferfiles() function is not static function, Change transferfiles() function to static.

By changing your transferfiles() function to static will call that function without creating instance of its class

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

From MSDN:

Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.

Prasad Telkikar
  • 13,280
  • 4
  • 13
  • 37
1

You should mark the transferfiles method as static.

private static void transferfiles(string modus)
Nguyễn Văn Phong
  • 12,566
  • 16
  • 32
  • 51
0

Private functions can only be used within the same class. If you want to use it outside (from another class) you have to change it to public.

Additionally check the static thing that are already mentioned. Static functions can be used without instanciate an object from a class. Change it to static if you don't want to create objects. Otherwise create a object from the class and call transferfiles from the object.

tux1337
  • 57
  • 5