In the following code when I loop through the values of the float array in either the first or second dictionary within the dictionary and modify any of the values, it modifies the corresponding value in the other sub dictionary. If I for example run this code agg_dict[report_type][0][unit_type_key][j] += 1
it's as if the computer runs agg_dict[report_type][1][unit_category_key][j] += 1;
before it even reaches that part of the code, causing an unwanted doubling effect by the end.
This is the main function that has the bug:
private void add_values_to_dict(Dictionary<string, Dictionary<string, float[]>[]> agg_dict, float[] hour_columns, string report_type, string region, string unit_type, string unit_category)
{
try
{
if (agg_dict.ContainsKey(report_type))
{
string unit_type_key = region + "," + unit_type;
string unit_category_key = region + "," + unit_category;
if (agg_dict[report_type][0].ContainsKey(unit_type_key))
{
for (int j = 0; j < agg_dict[report_type][0][unit_type_key].Length; j++)
{
agg_dict[report_type][0][unit_type_key][j] += (float)Convert.ToDouble(hour_columns[j]);
}
}
else
{
agg_dict[report_type][0].Add(unit_type_key, hour_columns);
}
if (agg_dict[report_type][1].ContainsKey(unit_category_key))
{
for (int j = 0; j < agg_dict[report_type][1][unit_category_key].Length; j++)
{
agg_dict[report_type][1][unit_category_key][j] += (float)Convert.ToDouble(hour_columns[j]);
}
}
else
{
agg_dict[report_type][1].Add(unit_category_key, hour_columns);
}
}
else
{
Dictionary<string, float[]>[] dict_array = new Dictionary<string, float[]>[2]; // 0 == unit type 1 == unit category
dict_array[0] = new Dictionary<string, float[]>();
dict_array[1] = new Dictionary<string, float[]>();
dict_array[0].Add(region + "," + unit_type, hour_columns);
float[] hour_columns_copy = hour_columns;
dict_array[1].Add(region + "," + unit_category, hour_columns_copy);
agg_dict.Add(report_type, dict_array);
}
}
catch (Exception ex)
{
Logger.log(ex);
}
}
And this is the function that creates the hour_columns array:
public void make_hours_array(string[] split_array, ref float[] hour_columns)
{
try
{
hour_columns[0] = (float)Convert.ToDouble(split_array[max_capacity_index]);
hour_columns[1] = (float)Convert.ToDouble(split_array[min_capacity_index]);
int cnt = 2;
for (int i = static_col_len; i < split_array.Length; i++)
{
//check to account for non number entries
if (!Char.IsDigit(split_array[i][0]))
{
int looper = 0;
bool has_num = false;
foreach (char c in split_array[i])
{
if (c == '-')
{
has_num = true;
looper++;
break;
}
looper++;
}
if (has_num)
{
string finding_num = split_array[i].Trim();
string num = "";
char new_c = finding_num[looper];
while (!Char.IsDigit(new_c) && looper < finding_num.Length)
{
looper++;
new_c = finding_num[looper];
}
while (looper < finding_num.Length)
{
new_c = finding_num[looper];
num += new_c;
looper++;
}
hour_columns[cnt] = (float)Convert.ToDouble(num);
}
else
{
hour_columns[cnt] = 0.0f;
}
}
else //else copies the string array into float array
{
hour_columns[cnt] = (float)Convert.ToDouble(split_array[i]);
}
cnt++;
}
}
catch(Exception ex)
{
Logger.log(ex);
}
}
and here's both funcitons being called:
make_hours_array(row_split_into_array, ref hour_columns);
add_values_to_dict(agg_dict, hour_columns, report_type, region,unit_type, unit_category);
the one thing I know is that the float conversion in the first function is redundant. I meant to remove it prior to my last bug fixing attempt but forgot, so I left it in to remain true to what was run when I got the latest error. I figured, though redundant, it wouldn't cause this linking issue I'm having.
I was originally passing hours_column and split_array by reference and thought that was what was causing the issue. But I removed reference and the values still appear to be linked. I'm assuming it has something to do with when the sub dictionaries are created.
I fixed it by separating the nested dictionary into two dictionaries and changing make_hours_array to not pass with the "ref" keyword and then initializing two different hour_colmns arrays from the start and passing each dictionary a separate array.