-2
String status;
            IEnumerable<TwitterStatus> twitterStatus = twitterService.ListTweetsOnPublicTimeline();
            foreach(String status in twitterStatus)
            {
                Console.WriteLine(twitterStatus);
            }

Why it give can not convert string type error in foreach loop ? this is my whole code

namespace TweetingTest
{
    class Program
    {
        static void Main(string[] args)
        {

            TwitterClientInfo twitterClientInfo = new TwitterClientInfo();
            twitterClientInfo.ConsumerKey = ConsumerKey; //Read ConsumerKey out of the app.config
            twitterClientInfo.ConsumerSecret = ConsumerSecret; //Read the ConsumerSecret out the app.config

            TwitterService twitterService = new TwitterService(twitterClientInfo);

            if (string.IsNullOrEmpty(AccessToken) || string.IsNullOrEmpty(AccessTokenSecret))
            {
                //Now we need the Token and TokenSecret

                //Firstly we need the RequestToken and the AuthorisationUrl
                OAuthRequestToken requestToken = twitterService.GetRequestToken();
                string authUrl = twitterService.GetAuthorizationUri(requestToken).ToString();

                //authUrl is just a URL we can open IE and paste it in if we want
                Console.WriteLine("Please Allow This App to send Tweets on your behalf");
                //Process.Start(authUrl); //Launches a browser that'll go to the AuthUrl.

                //Allow the App
                Console.WriteLine("Enter the PIN from the Browser:");
                string pin = Console.ReadLine();

                OAuthAccessToken accessToken = twitterService.GetAccessToken(requestToken, pin);

                string token = accessToken.Token; //Attach the Debugger and put a break point here
                string tokenSecret = accessToken.TokenSecret; //And another Breakpoint here

                Console.WriteLine("Write Down The AccessToken: " + token);
                Console.WriteLine("Write Down the AccessTokenSecret: " + tokenSecret);
            }

            twitterService.AuthenticateWith(AccessToken, AccessTokenSecret);

            //Console.WriteLine("Enter a Tweet");
            //string tweetMessage;
            //string data;
            //string ListTweetsOnPublicTimeline;
            //string TwitterUserStreamStatus = ListTweetsOnPublicTimeline();
            //TwitterStatus=ListTweetsOnPublicTimeline();
            //tweetMessage = Console.ReadLine();
            //ListTweetsOnPublicTimeline = Console.ReadLine();
            //TwitterStatus twitterStatus = twitterService.SendTweet(tweetMessage);
            //TwitterStatus twitterStatus = twitterService.ListTweetsOnPublicTimeline();
            //String status;
            IEnumerable<TwitterStatus> tweets = twitterService.ListTweetsOnPublicTimeline();
           foreach(var tweet in tweets)
            {
               Console.WriteLine(tweet);
                //Console.WriteLine("{0} says '{1}'", tweet.User.ScreenName, tweet.Text);
            }
            //twitterStatus=Console.ReadLine();
        }

This is my whole code on which I am working and facing just one error on foreach loop which is my lack of knowledge in C#

Smart Angel
  • 27
  • 1
  • 1
  • 6

2 Answers2

0

The object you are itterating through is of type "TwitterStatus", not string... so you are confusing it when you try to automatically cast a TwitterStatus object as a string.

Your question is very vague, so I'm going to assume your TitterStatus object has a "Text" property for the purposes of this answer.

foreach(TwitterStatus status in twitterStatus)
{
   Console.WriteLine(status.Text);
}

(just replace the ".Text" with whatever property of TwitterStatus holds the status text)

Ray K
  • 1,452
  • 10
  • 17
  • Use the new keyword to create an object instance !!! this is now the error this shows on foreach loop – Smart Angel Nov 23 '12 at 18:18
  • Sounds like twitterStatus is null. is your TwitterService call returning values? – Ray K Nov 23 '12 at 18:26
  • Thanks! when you break after your service call to ListTweetsOnPublic...(), is the ienumerable list of "tweets" getting filled? – Ray K Nov 23 '12 at 18:34
  • this is thing which confusing me , thats why I upload my code , I think ienumerable list of tweets is empty – Smart Angel Nov 23 '12 at 18:36
  • I'm seeing your code snippet looks like it came from https://github.com/danielcrenna/tweetsharp. The only difference I see here is that he declares his service: TwitterService service = new TwitterService(); (without passing in the client info parameter) – Ray K Nov 23 '12 at 18:41
  • Also - just checking that your "twitterService" is authenticating ok and that you are registered (good comment from amitd) – Ray K Nov 23 '12 at 18:44
  • I am registered , as I already crawl dataset from twitter from python , and now am using c# , am registered and I update my statues on twitter from the same code – Smart Angel Nov 23 '12 at 18:50
  • 1
    Have you been able to verify if the ienumerable list is empty after the ListTweetsOnPublicTimeline() call? If so, do you get any error info in why it failed? – Ray K Nov 23 '12 at 18:54
0

You will need something like this..

 using TweetSharp;

    TwitterService service = new TwitterService();
    IEnumerable<TwitterStatus> tweets = service.ListTweetsOnPublicTimeline();
    foreach (var tweet in tweets)
    {
        Console.WriteLine("{0} says '{1}'", tweet.User.ScreenName, tweet.Text);
    }

Also try this code to see why it is failing.. try to debug response.StatusCode

using TweetSharp;

TwitterService service = new TwitterService();
IAsyncResult result = service.ListTweetsOnPublicTimeline(
    (tweets, response) =>
        {
            if(response.StatusCode == HttpStatusCode.OK)
            {
                foreach (var tweet in tweets)
                {
                    Console.WriteLine("{0} said '{1}'", tweet.User.ScreenName, tweet.Text);
                }
            }
        });

More here : https://github.com/danielcrenna/tweetsharp

Amitd
  • 4,769
  • 8
  • 56
  • 82
  • Object reference not set to an instance of an object. – Smart Angel Nov 23 '12 at 18:25
  • you need to follow this article.. step by step guide ..you will need register your app with twiiter. http://www.d80.co.uk/post/2011/02/13/A-Simple-Twitter-Client-in-C-with-OAUTH-using-TweetSharp.aspx – Amitd Nov 23 '12 at 18:44
  • I am registered , as I already crawl dataset from twitter from python , and now am using c# , am registered – Smart Angel Nov 23 '12 at 18:49
  • updated my answer can u check what you get for response.StatusCode ? – Amitd Nov 23 '12 at 19:02
  • put a break point on the "if" line and check add a watch to response.StatusCode value and tweets list. – Amitd Nov 23 '12 at 19:11