0

Not sure if this is the best way to do this but I've created a 2D array from values in an excel file that i want to use as variables within my application that control where to look for things like file paths and files.

my data

The array is built and all data from the file is contained within it:

   for (int i = 0; i <= bindingSourceConfig.Count - 1; i++) // for each row in the binding source data
   {
       for (int j = 0; j <= 2 - 1; j++) // for each column, only need one and 2
       {
       System_Var_Array[i, j] = (bindingSourceConfig.DataSource as DataTable).Rows[i][j].ToString();
       }
   }

Now i want to be able to look in the array for my variable say "Project_Directory" and have it return "C:\Users\User\Dropbox\default\master\support"

Is this even possible?

EDIT 1: Purpose of doing it this way is to make an easily customizable/configurable multi project environment where by anyone can simply edit the paths in the excel file and import that file into the application.

I can not in my incompetence see a way of setting these 'variables' at a class level without first extracting the 'variable' and the 'value' from the excel data.

Is there a simple way of looking up the 'variable' in bindingSource?

EDIT 2:

debugger image

  • Possible, yes, just loop through the array, though you probably want to use Dictionary instead. – Martheen Jun 12 '21 at 04:38
  • Why do you need `System_Var_Array` with such a for loop since you can optionally work directly on `Rows` or perhaps `Rows.Cast<>().Select(...).ToList()` or `ToArray`? What is your use and purpose? Otherwise to search in the data you can use Linq `Where` and perhaps `FirstOrDefault`. –  Jun 12 '21 at 04:47
  • Does this answer your question? [How to search in 2D array by LINQ ?\[version2\]](https://stackoverflow.com/questions/18673822/how-to-search-in-2d-array-by-linq-version2) –  Jun 12 '21 at 04:49
  • You're reinventing the wheel. .NET has long supported storing and loading application settings from XML and JSON. Use a native solution, don't write your own. – Daniel Mann Jun 12 '21 at 04:55

2 Answers2

1
var name = "Project_Directory";
string value = null;
for (int i = 0; i<System_Var_Array.GetLength(0); i++) {
   if (System_Var_Array[i,0]==name) {
      value = System_Var_Array[i,1];
      break;
   }
}

After the loop stop, value will contain the data you need, assuming it exists (otherwise it will be null. A far simpler approach will be using Dictionary<string,string>, if you generate it with

var systemVars = new Dictionary<string,string>();
var dt = bindingSourceConfig.DataSource as DataTable;
for (int i = 0; i < bindingSourceConfig.Count; i++) // for each row in the binding source data
   {
       systemVars[dt.Rows[i][0].ToString()] = dt.Rows[i][1].ToString();
   }

then to get the value for "Project_Directory", simply call

var value = systemVars["Project_Directory"];
Martheen
  • 5,198
  • 4
  • 32
  • 55
  • The dictionary does seem a simpler approach and looks like it will work however i get an error "the given key was not present in the dictionary" –  Jun 12 '21 at 05:35
  • https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.trygetvalue?view=net-5.0#examples – Martheen Jun 12 '21 at 05:59
  • crashes here not when retrieving data systemVars[dt.Rows[i][0]] = systemVars[dt.Rows[i][1]]; –  Jun 12 '21 at 06:14
  • Tried .ToString() and dont doubt that its working on your end. Infact it returns the value [i][1] but still crashes and throws the key not found error. Will edit my question to show screenshot of debugger –  Jun 12 '21 at 06:26
  • the culprit was that line. this is what worked for me: systemVars.Add(dt.Rows[i][0].ToString(),dt.Rows[i][1].ToString()); –  Jun 12 '21 at 07:39
-1

You should use an appsettings file for this instead, it's the dotnet standard solution for this sort of thing. Please see https://learn.microsoft.com/en-us/dotnet/core/extensions/configuration.

Jeremy C.
  • 74
  • 4