-1

これは SetKey のコードです。Lists をループして数値を文字列に変換し、それを SetKey に入れます。今度は GetKey を使用してアクションを逆にし、数値を Lists に戻す必要があります。数字は元通り。

string[] xFrames = new string[wocl.Count];
                string[] yFrames = new string[wocl.Count];

                string X="";
                string Y="";
                for (int i = 0; i < wocl.Count; i++)
                {
                    X  = string.Format("Frame_X_{0} ", i + 1);
                    Y  = string.Format("Frame_Y_{0} ", i + 1);

                    for (int j = 0; j < wocl[i].Point_X.Count; j++)
                    {
                        xFrames[i] += string.Format("{0},", wocl[i].Point_X[j]);
                        yFrames[i] += string.Format("{0},", wocl[i].Point_Y[j]);


                    }

                    string tt = xFrames[i].Trim(",".ToCharArray());
                    string yy =  yFrames[i].Trim(",".ToCharArray());



                     setting_file.SetKey(X, tt);
                     setting_file.SetKey(Y, yy);   

                }

tt は、122,33,44,55,121 などの数字の文字列です。

ここで、数値を解析する必要があります。次に、文字列を取得して数値を解析し、それらを float リストに戻す必要があります。

リスト a = setting_file.GetKey(X);

しかし、X は、数値のリストではなく、数値の文字列を表すキーです。

これは、関数 GetKey および SetKey の OptionsFile 内のコードです。

/*----------------------------------------------------------
 * Function     : GetKey
 * Description  : gets the value of the key.
 * Parameters   : key
 * Return       : value of the key if key exist, null if not exist
 * --------------------------------------------------------*/
    public string GetKey(string key)
    {

      //  string value_of_each_key;
        string key_of_each_line;
        string line;
        int index;
        string key_value;
        key_value = null;

        sr = new StreamReader(Options_File);
        while (null != (line = sr.ReadLine()))
        {


            index = line.IndexOf("=");


           //    value_of_each_key = line.Substring(index+1);



            if (index >= 1)
            {
                key_of_each_line = line.Substring(0, index);
                if (key_of_each_line == key)
                {
                    key_value = line.Substring(key.Length + 1);
                }

            }
            else
            {
            }


        }
        sr.Close();
        return key_value;
    }


/*----------------------------------------------------------
 * Function     : SetKey
 * Description  : sets a value to the specified key
 * Parameters   : key and a value
 * Return       : none
 * --------------------------------------------------------*/
    public void SetKey(string key , string value)
    {
        bool key_was_found_inside_the_loop;
        string value_of_each_key;
        string key_of_each_line ;
        string line;
        int index;
        key_was_found_inside_the_loop = false;

        temp_settings_file = "\\temp_settings_file.txt";
        temp_settings_dir = path_exe + @"\temp_settings";
        if (!Directory.Exists(temp_settings_dir))
        {
            Directory.CreateDirectory(temp_settings_dir);
        }

        sw = new StreamWriter(temp_settings_dir+temp_settings_file);
        sr = new StreamReader(Options_File);
        while (null != (line = sr.ReadLine()))
        {

            index = line.IndexOf("=");
            key_of_each_line = line.Substring(0, index);
            value_of_each_key = line.Substring( index + 1);
         //   key_value = line.Substring(0,value.Length);
            if (key_of_each_line == key)
            {
                sw.WriteLine(key + " = " + value);
                key_was_found_inside_the_loop = true;

            }
            else
            {
                sw.WriteLine(key_of_each_line+"="+value_of_each_key);
            }

        }

        if (!key_was_found_inside_the_loop)
        {
           sw.WriteLine(key + "=" + value);
        }
        sr.Close();
        sw.Close();
        File.Delete(Options_File);
        File.Move(temp_settings_dir + temp_settings_file, Options_File);
        return;

    }

私が必要とするのは、リストaに文字列Xの数字が含まれることです

X はキーのようなもので、SetKey 関数の結果は Key = Value です

例: Hello = 122,33,44,55,66 Hello は変数 X のようなもので、右側の数字はキー値です。

だから今、キーX値を取得してリストに入れる必要があります

それを行う方法を理解できません。


リストを持っていて、それをループしてリストから数字を取り出し、数字の文字列を作成してSetKeyに入れる前に、GetKeyを使用して数字を取得し、それらをリストに戻す必要があります


編集:

public void Load(string path,string fileName)
        {
            string X = "";
            string t = path + "\\" + fileName;
            OptionsFile setting_file = new OptionsFile(t);
            for (int i = 0; i <= wocl.Count ; i++)
            {
            X  = string.Format("Frame_X_{0} ", i + 1);

            }
            string test = setting_file.GetKey(X);
        }

問題は、リスト wocl のループで実行している場合、プログラムを実行しているときに、このリストが 0 または 1 になることです。しかし、テキスト ファイルの GetKey には、4 フレームまたは 1 フレームがある可能性があります。ループでどれだけカウントするか?

テスト用に wocl List を試してみましたが、文字列テストで最初の Frame_X_1 の番号を取得していますが、それだけです。

ファイル自体は次のようになります。

Frame_X_1 =332,325,336,334,332,325,333,328,332
Frame_Y_1 =218,217,202,212,211,210,204,202,204
Frame_X_2 =270,325,336,347,321,325,333,328,332
Frame_Y_2 =257,217,202,282,156,210,204,202,204
Frame_X_3 =270,325,336,347,321,336,270,371,332
Frame_Y_3 =257,217,202,282,156,250,199,135,204

つまり、プログラムを実行すると、すべてのリストが空のカウントで0になりますが、Frame_X_1、Frame_Y_1などの各キーを取得する必要があります...そして、キーがいくつあるかわかりません。

4

2 に答える 2

0

「123,33.44.55.66」のような文字列があり、この文字列がコンマで区切られていることがわかっているとします。番号を取得するには:

string str = "123,33,44,55,66";
string[] strArray = str.Split(',');

strArrayその後、互換性のある任意のタイプに変換できます。

于 2012-07-18T06:43:45.873 に答える
0

これを静的ヘルパー クラスに追加します。

public static List<T> ToListOf<T>(this IEnumerable enumerable)
        {
            var list = new List<T>();

            foreach (var item in enumerable)
            {
                list.Add(item.ConvertTo<T>());
            }

            return list;
        }

次に:var myFloats = tt.Split(',').ToListOf<float>(); エレガントですね?:)

編集

もう1つの拡張メソッドを忘れました:

public static T ConvertTo<T>(this object source)
        {
            return (T)Convert.ChangeType(source, typeof(T));
        }
于 2012-07-18T06:57:50.417 に答える