Link to home
Create AccountLog in
Avatar of RadhaKrishnaKiJaya
RadhaKrishnaKiJaya

asked on

How to call a method with List type

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;
    }
}
Avatar of Eduard Ghergu
Eduard Ghergu
Flag of Romania image

Hi,

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

ASKER

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!
ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
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;
    }
}
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!
Hi,
Please, use the code that I have provided to you as a starting point.
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. }
Hi,
Sorry, I was tiered and I forgot to add the new operator:

var mainHub = new MainHub();
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.
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.
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.