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(Dat eTime, 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.No w.ToString ("yyyy-MM- dd")).AddD ays(-1);
var toDate = DateTime.Parse(fromDate.To String("yy yy-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.validatingxx xDesignato r, list);
if (details != null)
{
InsertDetailTableRaw(detai ls);
}
}
}
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;
}
}
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(Dat
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.No
var toDate = DateTime.Parse(fromDate.To
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.validatingxx
if (details != null)
{
InsertDetailTableRaw(detai
}
}
}
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;
}
}
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!
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
public static void Main(string[] args)
{
DataSet ds = new DataSet();
var fromDate = DateTime.Parse(DateTime.No w.ToString ("yyyy-MM- dd")).AddD ays(-1);
var toDate = DateTime.Parse(fromDate.To String("yy yy-MM-dd") ).AddDays( 0);
var mainHub = MainHub();
mainHub.PersistWhatever(fr omDate, 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.validatingxx xDesignato r, list);
if (details != null)
{
InsertDetailTableRaw(detai ls);
}
}
}
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;
}
}
{
DataSet ds = new DataSet();
var fromDate = DateTime.Parse(DateTime.No
var toDate = DateTime.Parse(fromDate.To
var mainHub = MainHub();
mainHub.PersistWhatever(fr
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.validatingxx
if (details != null)
{
InsertDetailTableRaw(detai
}
}
}
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;
}
}
ASKER
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!
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.
Please, use the code that I have provided to you as a starting point.
ASKER
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.No w.ToString ("yyyy-MM- dd")).AddD ays(-1);
6 . var toDate = DateTime.Parse(fromDate.To String("yy yy-MM-dd") ).AddDays( 0);
7.
8. var mainHub = MainHub();
9. mainHub.PersistWhatever(fr omDate, toDate);
10.
11. Environment.Exit(0);
12. }
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.No
6 . var toDate = DateTime.Parse(fromDate.To
7.
8. var mainHub = MainHub();
9. mainHub.PersistWhatever(fr
10.
11. Environment.Exit(0);
12. }
Hi,
Sorry, I was tiered and I forgot to add the new operator:
var mainHub = new MainHub();
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.
ASKER
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.
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.
Just create a new class where to place all these methods, instantiate it on main and invoke the object methods.