2016-09-06 10 views
0

データベースから値を読み込んで配列に割り当てるのに少し問題があります。私の単体テストではうまくいくようですが、実際にはいくつかの値がありません。C#リファレンス配列の割り当て問題

private void GetParameterValuesFromDatabase() 
{ 
     this.parameterValues = (from DataRow r in this.database.RunCommand("select * from KST_PARAM_VALUES v join DM_PARM_NAME p on v.PARM_NAME_KEY = p.PARM_NAME_KEY").Rows 
           where (int)r["SCENARIO_KEY"] == this.scenario.ScenarioKey 
           select new DatabaseParameter 
           { 
            ParameterValuesKey = r.Field<int>(0), 
            ProfileType = r.Field<string>(1), 
            ScenarioKey = r.Field<int>(2), 
            StressEditorKey = r.Field<int>(3), 
            StressClassKey = r.Field<int>(4), 
            PeriodKey = r.Field<int>(5), 
            ParameterNameKey = r.Field<int>(6), 
            ParameterValue = r.Field<double>(7), 
            ActiveStress = (r.Field<string>(8) == "Y") ? true : false, 
            ParameterKey = (int)r["PARM_NUMBER"] 
           }).ToDictionary(r => r.ParameterValuesKey, r => r); 
    } 

だけの完全性のため示す、私のコードのこの部分に問題がない:

は、ここに私のデータベース・コードです。

private void LoadParameters() 
{ 
    this.GetParameterValuesFromDatabase(); 

    // TODO: Assuming 9 periods for now, change to allow for variable periods 
    for (int i = 1; i <= MaxNumberOfStressPeriods; i++) 
    { 
     this.parametersByPeriod.Add(i, this.parameterValues.Where(t => t.Value.PeriodKey == i).ToDictionary(t => t.Key, t => t.Value)); 
    } 

    Log.Instance.LogMessage(LogLevel.Debug, "Created parameter dictionaries from database"); 

    // For every stress editor in the dictionary of stress editors 
    foreach (KeyValuePair<int, ClassList> ed in this.stressParams) 
    { 
      // For every type of class selector 
      foreach (ClassSelector c in Enum.GetValues(typeof(ClassSelector))) 
      { 
       // For each of the classes within each class list within the editor 
       for (int i = 0; i < ed.Value.ClassLists[c].Count; i++) 
       { 
        string className = ed.Value.ClassLists[c][i].Name; 

       // For each double array in each class 
        foreach (KeyValuePair<int, double[]> t in ed.Value.ClassLists[c][i].ClassVariables.EditorParameters) 
        { 
         double[] values = this.GetParameterValues(t.Key, ed.Key, className); 

         BasicStressEditorVariables.AddParameters(values, ed.Value, className, t.Key); 
        } 
       } 
      } 
     } 
    } 
} 

上記は、LoadParameters()メソッド全体を示しています。 データベースから構築された辞書から9つの値を選択するコードがあり、配列に追加する準備ができています。辞書からコピー先の配列を取得ショー以下

private double[] GetParameterValues(int paramKey, int editorKey, string className) 
{ 
    double[] values = new double[9]; 

    for (int i = 1; i <= MaxNumberOfStressPeriods; i++) 
    { 
     Dictionary<int, DatabaseParameter> temp = this.parametersByPeriod[i]; 

     foreach (KeyValuePair<int, DatabaseParameter> d in temp) 
     { 
      if (d.Value.ParameterKey == paramKey && d.Value.PeriodKey == i && d.Value.StressEditorKey == editorKey && d.Value.ProfileType == className) 
      { 
       values[i - 1] = d.Value.ParameterValue; 
      } 
     } 
    } 

    return values; 
} 

、インデックスが参照

public static void AddParameters(double[] values, ClassList editor, string className, int paramKey) 
{ 
    // TODO: Maybe search all lists to eliminate the need for the class selector as a parameter 
    // TODO: Will throw an exception when nothing is found. Handle it 
    ParameterClass p = null; 

    foreach (ClassSelector c in Enum.GetValues(typeof(ClassSelector))) 
    { 
     p = editor.ClassLists[c].FirstOrDefault(f => f.Name == className); 

     if (p != null) 
     { 
       break; 
     }     
    } 
    // TODO: Notify that could not be found 
    if (p == null) 
    { 
     Log.Instance.LogMessage(LogLevel.Error, $"Unable to find class {className}"); 
     return; 
    } 

    double[] dest = p.ClassVariables.editorParameters[paramKey]; 

    AddParameterValues(values, ref dest); 
} 

によって渡され、ここでAddParameterValues()メソッドだことができないよう:

private static void AddParameterValues(double[] values, ref double[] destination) 
{ 
    if (values.Length != destination.Length) 
    { 
     return; 
    } 

    for (int i = 0; i < values.Length; i++) 
    { 
     destination[i] = values[i]; 
    } 
} 

デバッグは、いくつかのことを示してい値が宛先配列にロードされていますが、一部の配列にはロードされていません。誰が私にこの理由が教えてもらえますか?それとも、そうでない場合は、私はいくつかの材料に向いていますか

は、私はそのC#の専門家ではないが、私は の寿命が唯一の範囲内であることを前提としています

private double[] GetParameterValues(int paramKey, int editorKey, string className) 
{ 
    double[] values = new double[9]; 
    //...  
    return values; 
} 

Cプログラマとして、次のコードを探して、あなたの時間

+1

のようなものにプロトタイプを変更する場合は、「いくつかがされていない」あなたはどれがでないかについての詳細を伝えることができますか?これは常に同じインデックスですか、コードを実行するたびに変更されますか? –

+0

@ C.Evenhuis - 常に同じインデックスで、同じものが正しく埋められ、同じものが正しく埋められません...デバッグでは、正しい値がある時点で配列に入力されているようですが、 –

+0

フィールドを維持するには??? –

答えて

0

いただき、ありがとうございます関数GetParameterValuesと関数GetParameterValuesは、死んだ変数を参照して呼び出し元を配信します。 あなたが

private void GetParameterValues(ref double[] values, int paramKey, int editorKey, string className) 
+0

本当ではない - 関数はローカルで作成された配列をうまく返すことができます。 –