-2

When I try to run the code below I am getting an error that says "Cannot implicitly convert type void to "Program.trainDataResult". I am just trying to return my trainDataResult object form the async method so I can use it with other methods in my app. Any suggestions? I am confused as to why the async method is returning void when I have specified that it should return the trainDataResult object wrapped in a task.

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

namespace ctaapijim
{
   public class Program
   {
       [XmlRoot(ElementName = "ctatt", DataType = "string", IsNullable = true)]
       public class trainDataResult
       {
           [XmlElement(ElementName = "tmst")]
           public string timeStamp { get; set; }

           [XmlElement(ElementName = "errCd")]
           public byte errorCode { get; set; }

           [XmlElement(ElementName = "errNm")]
           public string errorName { get; set; }

           [XmlElement(ElementName = "eta")]
           public trainData eta { get; set; }
       }

       [Serializable()]
       public class trainData
       {
           [XmlElement(ElementName = "staId")]
           public ushort stationId { get; set; }

           [XmlElement(ElementName = "stpId")]
           public ushort stopId { get; set; }

           [XmlElement(ElementName = "staNm")]
           public string stationName { get; set; }

           [XmlElement(ElementName = "stpDe")]
           public string stopDesc { get; set; }

           [XmlElement(ElementName = "rn")]
           public ushort runNum { get; set; }

           [XmlElement(ElementName = "rt")]
           public string routeName { get; set; }

           [XmlElement(ElementName = "destSt")]
           public ushort destStation { get; set; }

           [XmlElement(ElementName = "destNm")]
           public string destName { get; set; }

           [XmlElement(ElementName = "trDr")]
           public byte trainDir { get; set; }

           [XmlElement(ElementName = "prdt")]
           public string prdTime { get; set; }

           [XmlElement(ElementName = "arrT")]
           public string arrTime { get; set; }

           [XmlElement(ElementName = "isApp")]
           public ushort isApp { get; set; }

           [XmlElement(ElementName = "isSch")]
           public ushort isSch { get; set; }

           [XmlElement(ElementName = "isDly")]
           public ushort isDly { get; set; }

           [XmlElement(ElementName = "isFlt")]
           public ushort isFlt { get; set; }

           [XmlElement(ElementName = "flags")]
           public string flags { get; set; }

           [XmlElement(ElementName = "lat")]
           public double lat { get; set; }

           [XmlElement(ElementName = "lon")]
           public double lon { get; set; }

           [XmlElement(ElementName = "heading")]
           public ushort heading { get; set; }
      }

      private const string URL = "http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=d94463bf32094ac6bb1417cd8f850a23&mapid=41300&max=1";

      static void Main(string[] args)
      {
            trainDataResult td = RunAsync().Wait();
      }
      static async Task<trainDataResult> RunAsync()
      {
          using (var client = new HttpClient())
          {                                  
              var response = await client.GetAsync(URL);
              if (response.IsSuccessStatusCode)
              {
                    //get data from CTA API
                  var xml = await response.Content.ReadAsStringAsync();
                  var ds = new XmlSerializer(typeof(trainDataResult), new XmlRootAttribute("ctatt"));
                  using (StringReader sr = new StringReader(xml))
                  {
                      using (XmlReader xr = XmlReader.Create(sr))
                      {
                          var trainDataResult = (trainDataResult)ds.Deserialize(xr);

                            return trainDataResult;
                            //calculate the difference in time between arrival time and current time
                           /* trainDataResult.eta.arrTime = trainDataResult.eta.arrTime.Substring(9);
                            TimeSpan trainTime = TimeSpan.Parse(trainDataResult.eta.arrTime);
                            var timeOfDay = DateTime.Now.TimeOfDay;
                            TimeSpan arrMins = (trainTime - timeOfDay);

                          Console.WriteLine("Station Name: {0}\nRoute Name: {1}\nArrival Time: {2}\nRun Number: {3}\nDirection: {4}", trainDataResult.eta.stationName, trainDataResult.eta.routeName, arrMins, trainDataResult.eta.runNum, trainDataResult.eta.stopDesc);
                          */                                               
                      }
                  }
              }
              else
              {
                  Console.WriteLine("There was an error!");
              }
           }
        }
    }
}
orajestad9
  • 261
  • 1
  • 2
  • 10
  • Possible duplicate of [async/await - when to return a Task vs void?](http://stackoverflow.com/questions/12144077/async-await-when-to-return-a-task-vs-void) – Shachaf.Gortler Jul 21 '16 at 17:53
  • 1
    please show which line you get the error on, also show the rest of your function. – Scott Chamberlain Jul 21 '16 at 17:57
  • 1
    Please provide [MCVE]. I cannot understand what is going on – Khalil Khalaf Jul 21 '16 at 18:00
  • 2
    @Shachaf.Gortler OP need help in reading MSDN documentation about `Task,.Wait`, the duplicate you are suggesting has nothing to do with the problem in the post (unfortunately the code show is not [MCVE] which would make it easier to see). – Alexei Levenkov Jul 21 '16 at 18:00
  • 1
    `Wait` doesn't return the result. You want `trainDataResult td = RunAsync().Result` – juharr Jul 21 '16 at 18:07
  • I am getting the error on this page code above in my main method. 'trainDataResult td = RunAsync().Wait();' – orajestad9 Jul 21 '16 at 18:28

1 Answers1

0

You can't have a local variable with the same name as its type; the local variable will "hide" the name of the type.

Change the name of your class from trainDataResult to TrainDataResult, and the error should clear up.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810