使用foreach时,索引超出了数组的范围

时间:2013-08-13 06:37:30

标签: c# asp.net foreach

我正在尝试将社交媒体与siebel整合在一起

我收到错误

  

“索引超出了数组范围”

我无法解决错误

代码:

    public void Call_Tweet_Update()
    {
        var service = new TwitterService(Consumer_Key, Consumer_Secret);
        service.AuthenticateWith(Access_Token, AccessToken_Secret);

        var tweets = service.ListTweetsOnHomeTimeline(new ListTweetsOnHomeTimelineOptions { Count = 100 });
        string[] twt_id = new string[100];
        long id = 0;
        int i = 0;
        int increment = 0;
        string twtid;
        string screenname;

        foreach (var tweet in tweets)
        {
            if (tweet.InReplyToStatusId.ToString() != "")
            {
                if ((tweet.User.ScreenName == "IIPL_LTD") || (tweet.Text.StartsWith("@IIPL_LTD")))
                {
                    string replyid = tweet.InReplyToStatusId.ToString();

                    while (replyid != "")
                    {
                        if (i == 0)
                        {
                            twt_id[i] = tweet.Id.ToString();
                        }
                        id = Convert.ToInt64(replyid);
                        twtid = Convert.ToInt64(tweet.Id).ToString();
                        i = i + 1;
                        twt_id[i] = twtid;
                        increment = increment + 1;//Here I get an error
                    }

                    if (increment == 1)
                    {
                        //Reply related reply information
                        i = 0;
                        tweet_id = tweet.Id.ToString();
                        DbCommand = new OleDbCommand("select cust.first_name from mw_response resp, mw_customer cust where resp.response_id = '" + twt_id[i] + "' and resp.post_id is null and resp.customer_id= cust.customer_id", DbConnection);
                        OleDbDataReader DbReader = DbCommand.ExecuteReader();
                        while (DbReader.Read())
                        {
                            screenname = DbReader[0].ToString();
                            DbCommand = new OleDbCommand("select post_id,prod_id from mw_post where post_id = '" + id + "'", DbConnection);
                            OleDbDataReader DbReader0 = DbCommand.ExecuteReader();
                            while (DbReader0.Read())
                            {
                                post_id = DbReader0[0].ToString();
                                prod_id = DbReader0[1].ToString();

                                DbCommand = new OleDbCommand("update mw_response set prod_id = '" + prod_id + "',post_id = '" + post_id + "' where response_id = '" + twt_id[i] + "'", DbConnection);
                                DbCommand.ExecuteNonQuery();

                                //Invoking Siebel Web Service
                                if (screenname != "IIPL_LTD")
                                {
                                    createComment(twt_id[i]);
                                }
                            }
                            DbReader0.Close();
                        }
                        DbReader.Close();
                        increment = 0;
                        i = 0;
                    }
                    else
                    {
                        i = 0;
                        while (increment > 0)
                        {
                            //Reply related reply information 
                            DbCommand = new OleDbCommand("select cust.first_name from mw_response resp, mw_customer cust where resp.response_id = '" + twt_id[i] + "' and resp.post_id is null and resp.customer_id= cust.customer_id", DbConnection);
                            OleDbDataReader DbReader = DbCommand.ExecuteReader();
                            while (DbReader.Read())
                            {
                                screenname = DbReader[0].ToString();
                                DbCommand = new OleDbCommand("select post_id,prod_id from mw_post where post_id = '" + id + "'", DbConnection);
                                OleDbDataReader DbReader0 = DbCommand.ExecuteReader();
                                while (DbReader0.Read())
                                {
                                    post_id = DbReader0[0].ToString();
                                    prod_id = DbReader0[1].ToString();

                                    DbCommand = new OleDbCommand("update mw_response set prod_id = '" + prod_id + "',post_id = '" + post_id + "' where response_id = '" + twt_id[i] + "'", DbConnection);
                                    DbCommand.ExecuteNonQuery();

                                    //Invoking Siebel Web Service
                                    if (screenname != "IIPL_LTD")
                                    {
                                        createComment(twt_id[i]);
                                    }
                                }
                                DbReader0.Close();
                            }
                            DbReader.Close();
                            increment = increment - 1;
                            i = i + 1;
                        }
                        i = 0;
                    }
                }
            }
        }
        DbConnection.Close();
    }

这是程序被Index终止的地方,是数组的界限

 if(i == 0)
 {
    twt_id[i] = tweet.Id.ToString();
 }
 id = Convert.ToInt64(replyid);
 twtid = Convert.ToInt64(tweet.Id).ToString();
 i = i + 1;
 twt_id[i] = twtid;
 increment = increment + 1;

有什么想法吗?提前谢谢。

1 个答案:

答案 0 :(得分:1)

如果replyid不是空字符串,则代码会进入while循环,并且永远不会退出。当i递增到100时,代码会尝试访问导致此异常的twt_id[100]。人们会认为这意味着你打算在循环体结束之前修改replyid但是没有接触到它。

当然,单步执行代码 - 或在异常点检查i的值并推断为什么 - 会显示此问题。