Error - invalid token '(' in class struct or interface member declaration in c#

user2380428 picture user2380428 · May 14, 2013 · Viewed 29.1k times · Source

My code is -

public partial class App : Application
{

    HarvestApp.GoogleAPIManager GAPImanager = new HarvestApp.GoogleAPIManager();

    List<Event>todayCalendar = GAPImanager.GetCalendarEventsForDate(DateTime.Today);

    HarvestApp.HarvestManager HAPIManager = new HarvestApp.HarvestManager();

    Console.WriteLine("Entries found for Today :" + todayCalendar.Count);

    foreach(Event todayEvent in todayCalendar)
    {
        var addEvent = new HarvestApp.Harvest_TimeSheetEntry(todayEvent);
        EntryList.Add(addEvent);
        HAPIManager.postHarvestEntry(addEvent);
    }

 }

It gives me token error. Please help.

Answer

Daniel Hilgarth picture Daniel Hilgarth · May 14, 2013

The problem is that you did put your code directly in the class and not inside a member like a constructor:

public partial class App : Application
{
    public App()
    {
        HarvestApp.GoogleAPIManager GAPImanager = new HarvestApp.GoogleAPIManager();

        List<Event>todayCalendar = GAPImanager.GetCalendarEventsForDate(DateTime.Today);

        HarvestApp.HarvestManager HAPIManager = new HarvestApp.HarvestManager();

        Console.WriteLine("Entries found for Today :" + todayCalendar.Count);

        foreach(Event todayEvent in todayCalendar)
        {
            var addEvent = new HarvestApp.Harvest_TimeSheetEntry(todayEvent);
            EntryList.Add(addEvent);
            HAPIManager.postHarvestEntry(addEvent);
        }
    }
 }