2016-09-06 4 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; 
} 

가 AddParameterValues를 참조

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); 
} 

를 통과 그리고 여기 수 없습니다() 메소드 :

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

사실이 아닙니다 - 함수는 로컬에서 생성 된 배열을 올바르게 반환 할 수 있습니다. –