-1

I have been noodling around in C# and I am doing well so far but I am trying to find out how to Search an index in a multi dimensional array. I have an array which is for products made in each day. This displays the products made each day for each week by asking the user for input (using an InputBox) E.g. Week 1 Monday = 10 Bears, Wednesday = 20 Bears. Week 2 Tuesday = 30, Friday = 11, etc. I know about the IndexOf but this is for finding the index of a given value. I want to search the index and find and display the value.

The code is below:

class Day
{
    private string monday;
    private string tuesday;
    private string wednesday;
    private string thursday;
    private string friday;

    public string Monday
    {
        get { return monday; }
        set { monday = value; }
    }
    public string Tuesday
    {
        get { return tuesday; }
        set { tuesday = value; }
    }
    public string Wednesday
    {
        get { return wednesday; }
        set { wednesday = value; }
    }
    public string Thursday
    {
        get { return thursday; }
        set { thursday = value; }
    }
    public string Friday
    {
        get { return friday; }
        set { friday = value; }
    }
}

private void btnSelect_Click(object sender, EventArgs e)
{
    Day[] week = new Day[4];
    //Week[0] = new Day(); Ask users for input box value
    E.g.Monday = Microsoft.VisualBasic.Interaction.InputBox("Enter the amount of products made on Monday for week 1", "Product Amount"),etc

    //Prints the amounts
    txtOutput.Text += "The product allocation is as follows:" + "\r\n" + "\r\n";
    txtOutput.Text += "                      Mon    Tue     Wed     Thu     Fri  \r\n";

    int weeknum = 0;
    //Iterates through and shows the values for each day of the week.
    foreach (Day days in week)
    {
        weeknum++;
        if (days != null)
        {
            txtOutput.Text += "Week " + weeknum + "         " + days.Monday + "          " + days.Tuesday + "         " + days.Wednesday + "          " + days.Thursday + "         " + days.Friday + "\r\n";
        }
    }
}
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118

3 Answers3

2

"I want to search the index and find and display the value."

Sorry I might not be answering your question, but this sounds like a perfect use-case for Dictionary.

IDictionary<string, int> database = new Dictionary<string, int>
{
    {"Week1 Monday", 10},
    {"Week1 Wednesday", 20}
};

var userinput = "Week1 Monday";
int numberOfBears;
database.TryGetValue(userinput, out numberOfBears);

//numberOfBears = 10

For more information, you can refer to http://www.dotnetperls.com/dictionary

Cubicle.Jockey
  • 3,288
  • 1
  • 19
  • 31
interceptwind
  • 665
  • 4
  • 14
  • 2
    Also, your example could be improved by using `TryGetValue` method instead of `ContainsKey` and returning the item. See more here: https://stackoverflow.com/questions/9382681/what-is-more-efficient-dictionary-trygetvalue-or-containskeyitem – bashis Nov 20 '15 at 07:18
  • @bashis Learnt something today. Thanks! :) – interceptwind Nov 20 '15 at 07:50
0

You'll need to loop through the array using the Array.GetLowerBound and Array.GetUpperBound methods. The Array.IndexOf and Array.FindIndex methods don't support multidimensional arrays.

For example:

string[,,] data = new string[3,3,3];
data.SetValue("foo", 0, 1, 2 );

for (int i = data.GetLowerBound(0); i <= data.GetUpperBound(0); i++)
    for (int j = data.GetLowerBound(1); j <= data.GetUpperBound(1); j++)
        for (int k = data.GetLowerBound(2); k <= data.GetUpperBound(2); k++)
            Console.WriteLine("{0},{1},{2}: {3}", i, j, k, data[i,j,k]);
You might also find the Array.GetLength method and Array.Rank property useful. I recommend setting up a small multidimensional array and using all these methods and properties to get an idea of how they work.
0

There are no built-in functions for multidimensional array like Array.Find().

You basically have two choices: create your own helper methods and implement a generic search pattern there, or generate a list of domain objects correlating to the contents of the multidimensional array. I personally have tended to choose the latter option.

If you choose to write a helper method, it could look something (very roughly) like this:

// you could easily modify this code to handle 3D arrays, etc.

public static class ArrayHelper
{
   public static object FindInDimensions(this object[,] target, 
   object searchTerm)
  {
    object result = null;
    var rowLowerLimit = target.GetLowerBound(0);
    var rowUpperLimit = target.GetUpperBound(0);

    var colLowerLimit = target.GetLowerBound(1);
    var colUpperLimit = target.GetUpperBound(1);

    for (int row = rowLowerLimit; row < rowUpperLimit; row++)
    {
        for (int col = colLowerLimit; col < colUpperLimit; col++)
        {
            // you could do the search here...
        }
    }

    return result;
}

}

Shahid Manzoor Bhat
  • 1,307
  • 1
  • 13
  • 32