-3

I am posting yet once again and cannot seem to find an easy way to convert this. I have tried to get rid of the 2 dimensional array for this method, ExtractData and replace it with using a list but am having no luck. Would you guys be able to give me some examples or ideas I can go off of? Thank you.

private void ExtractData( DateTime dtmDay )
  {
     // set to selected date in MonthCalendar control
     int intChosenDay = dtmDay.Day;
     int intFileDay; // day of event from file
     int intLineNumbers; // counts lines to skip

     m_intNumberOfEvents = 0; // set number of events to 0

     // initialize StreamReader to read lines from file
     StreamReader objInput = 
        new StreamReader( "calendar.txt" );

     // read first line before entering loop
     string strLine = objInput.ReadLine();

     // loop through lines in file
     while ( objInput.Peek() > -1 &&
        m_intNumberOfEvents < 10 )
     {
        intFileDay = Int32.Parse( strLine ); // extract day

        // if event scheduled for specified day,
        // store information
        if ( intFileDay == intChosenDay )
        {
           m_strData[ m_intNumberOfEvents, 0 ] = strLine;
           m_strData[ m_intNumberOfEvents, 1 ] = 
              objInput.ReadLine();
           m_strData[ m_intNumberOfEvents, 2 ] = 
              objInput.ReadLine();
           m_strData[ m_intNumberOfEvents, 3 ] = 
              objInput.ReadLine();
           m_strData[ m_intNumberOfEvents, 4 ] = 
              objInput.ReadLine();
           m_intNumberOfEvents++;
        }
        else
        {
           // skip to next event in file
           for ( intLineNumbers = 0; intLineNumbers <= 3;
              intLineNumbers++ )
              strLine = objInput.ReadLine();
        }

        // read day of next event in file
        strLine = objInput.ReadLine();

     } // end while

  } // end method ExtractData
4

3 回答 3

1

所以你有可变数量的事件,每个事件都有 5 行你希望单独存储的文本?创建一个类来表示每个事件数据,并创建它们的列表。

public class EventData {
    public string Line1 { get; set; }
    public string Line2 { get; set; }
    public string Line3 { get; set; }
    public string Line4 { get; set; }
    public string Line5 { get; set; }
}

...

var results = new List<EventData>();

// loop through lines in file
 while ( objInput.Peek() > -1 &&
    m_intNumberOfEvents < 10 )
 {
    intFileDay = Int32.Parse( strLine ); // extract day

    // if event scheduled for specified day,
    // store information
    if ( intFileDay == intChosenDay )
    {
        var foo = new EventData();

        foo.Line1 = strLine;
        foo.Line2 = objInput.ReadLine();
        foo.Line3 = objInput.ReadLine();
        foo.Line4 = objInput.ReadLine();
        foo.Line5 = objInput.ReadLine();

        results.Add(foo);

        m_intNumberOfEvents++;
    }
    else
    {
       // skip to next event in file
       for ( intLineNumbers = 0; intLineNumbers <= 3;
          intLineNumbers++ )
          strLine = objInput.ReadLine();
    }

    // read day of next event in file
    strLine = objInput.ReadLine();

 } // end while
于 2013-04-26T21:44:46.253 回答
0

我看到在这种情况下定义一个新类是没有意义的。您的所有字段都是字符串,并且您在类中没有方法或属性。它只是一个字符串数组。利用:

var results = new List<string[]>(); 
...
var lines = new string[5];

lines[0] = strLine;
lines[1] = objInput.ReadLine();
lines[2] = objInput.ReadLine();
lines[3] = objInput.ReadLine();
lines[4] = objInput.ReadLine();

results.Add(lines);
于 2013-04-26T22:25:04.213 回答
0

必须重复Dylan Smith所说的;上课。

我不完全确定你想要做什么,但看起来你想要每天发生的一系列事件。如果是这样,那么一堂课真的是最有意义的。该类不仅会存储这些字符串,而且您还可以拥有越来越多的易于管理的数据(例如事件发生的时间)。

真的,如果你发现自己遇到了这样的问题,你想要的不仅仅是一个变量来保存一些关于某事的信息,那就创建一个类或一个结构。以后你会感谢自己的。

于 2013-04-26T21:57:45.167 回答