views:

129

answers:

6

Hi, I have a number of different clases located in a class library in my project. I am using Quartz.NET (a scheduling system) to schedule and load jobs, and the actual job execution is done in these class libraries. I plan to have many types of job types, and each one will have their own class for execution in the class library.

An issue I have is that I can't nest methods in these classes. For example, here is my class:

public class FTPtoFTP : IJob
{
    private static ILog _log = LogManager.GetLogger(typeof(HTTPtoFTP));

    public FTPtoFTP()
    {

    }

    public virtual void Execute(JobExecutionContext context)
    {          
        //Code that executes, the variable context allows me to access the job information
    }
}

If I try to put a method in the execution part of the class, such as...

 string[] GetFileList()
 { 
    //Code for getting file list
 }

It expects the end of the execution method before my GetFileList one begins, and also doesn't let me access the context variable which I need.

I hope this makes sense, thanks again - you guys rule

+1  A: 

You seem to have misunderstood how class code works?

GetFileList() doesn't execute just because you have placed it in the class after Execute() - you have to actually call it, like this:

public class FTPtoFTP : IJob
{
    private static ILog _log = LogManager.GetLogger(typeof(HTTPtoFTP));

    public FTPtoFTP()
    {

    }

    public virtual void Execute(JobExecutionContext context)
    {
        string[] files = GetFileList();

        //Code that executes, the variable context allows me to access the job information
    }

    string[] GetFileList()
    { 
        //Code for getting file list
    }
}

or have i totally misunderstood your question?

slugster
+1  A: 

You could use lambda expressions:

public virtual void Execute(JobExecutionContext context) 
{ 

    Func<string[]> getFileList = () => { /*access context and return an array */};

    string[] strings = getFileList();

} 
Henrik
+2  A: 

No, you can't nest methods. Here are a couple of approaches you can use instead:

  • You can create anonymous functions inside methods and call them in a similar way to calling a method.
  • You can promote local variables in one method to member fields and then you can access them from both methods.
Mark Byers
... or you can just pass the context variable into a private `GetFileList` as a parameter!
Timwi
+1  A: 

Are you trying to get the results from the GetFileList function and use them in Execute? If so, then just try this:

public class FTPtoFTP : IJob
{
    private static ILog _log = LogManager.GetLogger(typeof(HTTPtoFTP));

    public FTPtoFTP()
    {

    }

    public virtual void Execute(JobExecutionContext context)
    {
        //Code that executes, the variable context allows me to access the job information
        string[] file_list = GetFileList();
        // use file_list
    }

    private string[] GetFileList()
    { 
       //Code for getting file list
       return list_of_strings;
    }
}
SwDevMan81
+1  A: 

Execute is a virtual method, its not a space to declare additional methods, inside you're meant to place any logic for the job, its not a namespace to declare new methods. If you want to use methodised logic, just declare them within the class definition and call them from the execute function.

public virtual void Execute(JobExecutionContext context)
{

    mymethod1();
    mymethod2();
}

private void mymethod1()
{}

private void mymethod2()
{}
Val
+1  A: 

It seems you want to get the file list based on some context information - in this case just add a parameter to the GetFileList method and pass it from Execute:

public virtual void Execute(JobExecutionContext context)
{
    string[] fileList = this.GetFileList(context);
    ...
}

private string[] GetFileList(JobExecutionContext) { ... }
Lee