How to call a method with List type

RadhaKrishnaKiJaya
RadhaKrishnaKiJaya used Ask the Experts™
on
Hello Experts,
I have a Console App, which imports some data thru an API call.  In this all the methods are defined inside the Main().  I am trying to define some methods such as xxxSearch and XxxxDetail, return type List, outside of the Main() method.  When I do that, I am getting the Error: CS0120 An object reference is required for the non-static field, method, or property 'XxxxImport.XxxxSearch(DateTime, DateTime)'.

The reason I need to do this, because I need to convert this to a Windows Service Program comfortably.

Please let me know how to define them outside of Main() method and call them,

Thank you very much in advance.

Below is the Skeleton of the Code
----------------------------------------------------

public static void Main(string[] args)
{
    DataSet ds = new DataSet();

    var fromDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd")).AddDays(-1);
    var toDate = DateTime.Parse(fromDate.ToString("yyyy-MM-dd")).AddDays(0);

    MainHub(fromDate, fromDate);

    Environment.Exit(0);


    void MainHub(DateTime FromDate, DateTime ToDate)
    {
      var xxxFound = xxxSearch(FromDate, ToDate);

      if (xxxFound != null)
      {
          InsertxxxRaw(xxxFound, FromDate);
      }

      foreach (var xxx in xxxFound)
      {
          var list = new List<string>();
          list.Add(xxx.xxxNumber);

          var details = xxxDetail(xxx.validatingxxxDesignator, list);

          if (details != null)
          {
            InsertDetailTableRaw(details);
          }
      }
    }

    IList<Xxxx> xxxSearch(DateTime from, DateTime to)
    {
      IList<Xxxx> searchResults = new List<Xxxx>();

      try
      {
          while (true)
          {
          }
      }
      catch (SqlException ex)
      {
      }

      return searchResults;
    }

    IList<XxxxDetail> XxxxDetail(string yyyCode, List<string> xxxNumber)
    {
      .
      .
      .
      return detailResults;
    }
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Eduard GherguArchitect - Coder - Mentor

Commented:
Hi,

Just create a new class where to place all these methods, instantiate it on main and invoke the object methods.

Author

Commented:
Hello Eduard Ghergu,
Thank you for your quick reply.  Is it possible for you to provide the code.  Please look at the skeleton I provided.

Thank you!
Senior Software Engineer
Top Expert 2009
Commented:
You can easily resolve your issue by making your methods static, for example:
static IList<Xxxx> xxxSearch(DateTime from, DateTime to)

Open in new window

You could consider refactor your methods as shown below:
public class MainHub
{
	public void RunMainHub(DateTime FromDate, DateTime ToDate)
    {
      var xxxFound = xxxSearch(FromDate, ToDate);

      if (xxxFound != null)
      {
          InsertxxxRaw(xxxFound, FromDate);
      }

      foreach (var xxx in xxxFound)
      {
          var list = new List<string>();
          list.Add(xxx.xxxNumber);

          var details = xxxDetail(xxx.validatingxxxDesignator, list);

          if (details != null)
          {
            InsertDetailTableRaw(details);
          }
      }
    }

    private IList<Xxxx> xxxSearch(DateTime from, DateTime to)
    {
      IList<Xxxx> searchResults = new List<Xxxx>();

      try
      {
          while (true)
          {
          }
      }
      catch (SqlException ex)
      {
      }

      return searchResults;
    }

    private IList<XxxxDetail> XxxxDetail(string yyyCode, List<string> xxxNumber)
    {
      .
      .
      .
      return detailResults;
    }
}

Open in new window

Eduard GherguArchitect - Coder - Mentor

Commented:
public static void Main(string[] args)
{
    DataSet ds = new DataSet();

    var fromDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd")).AddDays(-1);
    var toDate = DateTime.Parse(fromDate.ToString("yyyy-MM-dd")).AddDays(0);

    var mainHub = MainHub();
      mainHub.PersistWhatever(fromDate, toDate);

    Environment.Exit(0);  
}

public class MainHub {

      public MainHub()    {   }

      public void PersistWhatever(DateTime fromDate, DateTime toDate) {
      
        var xxxFound = xxxSearch(fromDate, toDate);

      if (xxxFound != null)
      {
          InsertxxxRaw(xxxFound, fromDate);
      }

      foreach (var xxx in xxxFound)
      {
          var list = new List<string>();
          list.Add(xxx.xxxNumber);

          var details = xxxDetail(xxx.validatingxxxDesignator, list);

          if (details != null)
          {
            InsertDetailTableRaw(details);
          }
      }
      }

    private IList<Xxxx> xxxSearch(DateTime from, DateTime to)
    {
      IList<Xxxx> searchResults = new List<Xxxx>();

      try
      {
          while (true)
          {
          }
      }
      catch (SqlException ex)
      {
      }

      return searchResults;
    }

    private IList<XxxxDetail> XxxxDetail(string yyyCode, List<string> xxxNumber)
    {
      .
      .
      .
      return detailResults;
    }
}

Author

Commented:
Eduard Ghergu,
As suggested by you, I moved xxxSearch out of the Main() and it worked.  When tried to move XxxxDetail, I got this error as below.  Any idea?
Error      CS0102      The type 'XxxxImport' already contains a definition for 'XxxxDetail'

Putting everything inside another class, I will try next, upon resolving the above issue.

Thank you!
Eduard GherguArchitect - Coder - Mentor

Commented:
Hi,
Please, use the code that I have provided to you as a starting point.

Author

Commented:
In your code what is line 8, var mainHub = MainHub()?

I am getting error: local variable? mainHub
Cannot use local variable 'mainHub' before it is declared.


1. public static void Main(string[] args)
2. {
3.     DataSet ds = new DataSet();
4.
5.    var fromDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd")).AddDays(-1);
6 .   var toDate = DateTime.Parse(fromDate.ToString("yyyy-MM-dd")).AddDays(0);
7.
8.    var mainHub = MainHub();
9.      mainHub.PersistWhatever(fromDate, toDate);
10.
11.   Environment.Exit(0);  
12. }
Eduard GherguArchitect - Coder - Mentor

Commented:
Hi,
Sorry, I was tiered and I forgot to add the new operator:

var mainHub = new MainHub();
AndyAinscowFreelance programmer / Consultant

Commented:
Don't forget to look up Error: CS0120 and see why you get this error.  If you don't understand that you won't know why you original code wasn't working and what the implications of the modifications supplied to make it work are.

Author

Commented:
Thank you Eduard Ghergu for your wonderful and timely support.  I added "static " in front of the method and did some code correction, which fixed the issue.  The 2nd option was also very helpful too.  Thank you again for your support.
Eduard GherguArchitect - Coder - Mentor

Commented:
Hi,
Sure, adding static is always an option, but you have to think of solution design, if it makes sense or not. Please, let me know if you'll need more help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial