0

数十のエントリを含むテキスト ファイルを解析したいと思います。現在、行ごとに読み取り、ハードコーディングされた文字列と比較する、単純なソリューションがあります。

while ((line = reader.ReadLine()) != null) //returns null if end of stream
{
    cmpStr = "MODE";
    try
    {
        if (line.Equals(cmpStr))                        
            GlobalData.mode = Convert.ToInt32(line.Remove(0, cmpStr.Length));
    }
    catch { }

    cmpStr = "TIME_YEAR";
    try
    {
        if (line.Equals(cmpStr))                        
            GlobalData.time_year = Convert.ToInt32(line.Remove(0, cmpStr.Length));
    }
    catch { }


    // ... repeat to parse the remaining lines
}

GlobalData は静的クラスで、次のようになります。

public static class GlobalData
{

    public static int mode;                 
    public static int time_year;
    public static int time_month;
    public static int time_day;
    public static int time_hour;
    public static int time_minute;
    // other entries omitted

    public static string[] GlobalKeywords = new  string[37] 
    {
        "MODE", 
        "TIME_YEAR",
        "TIME_MONTH",
        "TIME_DAY",
        "TIME_HOUR",
        "TIME_MINUTE",
        // other entries omitted      
    };
}

indexで静的フィールドにアクセスできる場合は、次のようにします。

int i = 0;
while ((line = reader.ReadLine()) != null) 
{
    cmpStr = GlobalData.GlobalKeywords[i];    // when i == 0: cmpStr = "MODE"

    if (line.Equals(cmpStr))
        GlobalData[i] = Convert.ToInt32(line.Remove(0, cmpStr.Length));
        // GlobalData[0] would be GlobalData.mode, and so on (but doesn't work)

    i++;
}
catch { }

では、ループを設定してキーワードの文字列配列と比較することはできますが、静的クラスの特定のフィールドを割り当てるにはどうすればよいでしょうか?

br クリス

4

5 に答える 5

1

問題を解決する洗練された方法は、受け入れ可能な文字列ごとに異なるアクションを用意することです。where Action は、Dictionary(Of String, <Action>)入力で文字列を受け取り、行の先頭にあるキーワードに応じて処理する方法を知っている一般的なデリゲート タイプです。

// The common signature for every methods stored in the value part of the dictionary
public delegate void ParseLine(string line);

// Global dictionary where you store the strings as keyword
// and the ParseLine as the delegate to execute
Dictionary<String, ParseLine> m_Actions = new Dictionary<String, ParseLine>() ;

void Main()
{
    // Initialize the dictionary with the delegate corresponding to the strings keys
    m_Actions.Add("MODE", new ParseLine(Task1));
    m_Actions.Add("TIME_YEAR", new ParseLine(Task2));
    m_Actions.Add("TIME_MONTH", new ParseLine(Task3));
    m_Actions.Add("TIME_DAY", new ParseLine(Task4));
    .....

    while ((line = reader.ReadLine()) != null) 
    {
        // Search the space that divide the keyword from the value on the same line
        string command = line.Substring(0, line.IndexOf(' ')).Trim();
        // a bit of error checking here is required
        if(m_Actions.ContainsKey(command))
            m_Actions[command](line);

    }
}

void Task1(string line)
{
    // this will handle the MODE line
    GlobalData.Mode = Convert.ToInt32(line.Substring(line.IndexOf(' ')+1).Trim());

}
void Task2(string line)
{
     GlobalData.time_year = Convert.ToInt32(line.Substring(line.IndexOf(' ')+1).Trim());
}
void Task3(string line)
{
    .....
}
void Task4(string line)
{
    .....
}
于 2013-10-25T11:57:19.283 に答える
1

これを置き換えます:

public static int mode;                 
public static int time_year;
public static int time_month;
public static int time_day;
public static int time_hour;
public static int time_minute;

これとともに:

public static Dictionary<string, int> my_values = new Dictionary<string, int>();

次に置き換えます:

GlobalData[i] = Convert.ToInt32(line.Remove(0, cmpStr.Length));

と:

GlobalData.my_values[cmpStr] = Convert.ToInt32(line.Remove(0, cmpStr.Length));

あなたがどのように動作することを期待しているのか理解できませんが、それはあなたが望むことをするはずですConvert.ToInt32. あなたが呼び出している方法はRemove、空の文字列を作成します (これは 0 に変換される可能性がありますが、覚えていません)。 "。

于 2013-10-25T11:49:12.343 に答える
0

C#でそれを達成する方法(GlobalData [i])を教えてくれるかもしれませんが、あなたが探している答えではないと思いました。

    class GlobalData
    {
        private string[] array = new string[10];
        public GlobalData()
        {
           //now initialize array
           array[0] = "SomeThingA";
           array[1] = "SomeThingB";//continue initialization.
        }
        public string this[int index]
        {
            get {return array[index];}
        }
    }

クライアントは GlobalData を次のように使用できるようになりました。

    GlobalData gd = new GlobalData();
    gd[1] = "SomeOtherThing" ; //set the value.
    string value = gd[1];//get the value

しかし、 「this」で動作することがわかるように、クラスを静的にすることによってこれを行うことはできません

于 2013-10-25T12:33:50.200 に答える