0

I have a user inputting a day (Monday) and a week number in order to search for a value in an array. However, to get the array output, I need the row and column numbers. The row number can be obtained from the user input, 1,2 etc. However, the part where they input Monday, Tuesday etc. is a bit trickier. Is there a way to convert Monday Tuesday etc to an integer (1,2) so that I can use that to find the array element?

I tried this method, but it's not working. The conversion of value to an int obviously doesn't work due to "Monday" not being a number. Is there an alternative way to doing this?

int Value = Convert.ToInt32(value);

DateTime ClockInfoFromSystem = DateTime.Now;
Value = (int)ClockInfoFromSystem.DayOfWeek;

This is the rest of the code if you need it

private void AddToArray()
{
    txtOutput.Text = "Filling the array with user input..." + "\r\n\r\n";

    String value;
    int num;

    for (int week = 0; week < productsArray.GetLength(0); week++)
    {
        for (int day = 0; day < productsArray.GetLength(1); day++)
        {
            value = Microsoft.VisualBasic.Interaction.InputBox("Enter Value for Day " + day + " of Week " + week, "Enter Value");
            try
            {
                while (!(int.TryParse(value, out num)))
                {
                    MessageBox.Show("Not a valid number, try again.");
                    value = Microsoft.VisualBasic.Interaction.InputBox("Enter a Number", "Enter Number");

                }
            }
            catch (Exception)
            {
                MessageBox.Show("Value entered is not in a valid format");
            }

            productsArray[week, day] += int.Parse(value);
        }
    }
    txtOutput.Text += "The product allocation is as follows:" + "\r\n\r\n";
}

private void Array()
{
    txtOutput.Text += "\tMon\tTue\tWed\tThu\tFri\r\n";
    for (int week = 0; week < productsArray.GetLength(0); week++)
    {
        txtOutput.Text += "Week " + (week + 1) + "\t";
        for (int day = 0; day < productsArray.GetLength(0); day++)
        {
            txtOutput.Text += productsArray[week, day] + "\t";
        }
        txtOutput.Text += "\r\n";
    }

    txtOutput.Text += "\r\n" + "Retrieve products completed on a specific day." + "\r\n";
    String value = Microsoft.VisualBasic.Interaction.InputBox("Enter Day", "Enter Day", "Monday");

    int num;

    String value2 = Microsoft.VisualBasic.Interaction.InputBox("Enter Week", "Enter Week");
    try
    {
        while (!(int.TryParse(value2, out num)))
        {
            MessageBox.Show("Not a valid number, try again.");
            value2 = Microsoft.VisualBasic.Interaction.InputBox("Enter Week", "Enter Week");
        }
    }
    catch (Exception)
    {
        MessageBox.Show("Value entered is not in a valid format");
    }
    int Value2 = Convert.ToInt32(value2);
    int Value = Convert.ToInt32(value);

    DateTime ClockInfoFromSystem = DateTime.Now;
    Value = (int)ClockInfoFromSystem.DayOfWeek;

    var output = productsArray[Value2, Value];

    txtOutput.Text += "Products completed on that day are: " + output;

}
Anon
  • 141
  • 1
  • 12
  • Can you explain why _Value = (int)ClockInfoFromSystem.DayOfWeek;_ doesn't work? – Steve Aug 19 '16 at 14:44
  • @Steve its the conversion before that causes an error. Because the user inputs something as a string command, I tried to convert the string to an int, which in the case of "Monday" doesn't really work. I'm trying to see if there's an alternative to that. – Anon Aug 19 '16 at 14:48
  • The duplicate explains how to convert a string (your user input) in an enum (DayOfWeek). Your code, to get the integer, should work then. I should also apologize to the user that was already pointing to the duplicate above. You were right. – Steve Aug 19 '16 at 14:53
  • Is there a simpler way to do this? Or is there an alternative to how I should get the element of the array from the user input – Anon Aug 19 '16 at 14:53
  • This isn't a duplicate if the language being used for the entry of the day name is anything other than English... – Matthew Watson Aug 19 '16 at 14:58
  • Having two variables named `value` and `Value` is a Very Bad Idea (TM). – Code-Apprentice Aug 19 '16 at 19:40

1 Answers1

2

I suggest writing a method such as dayToInt(String day) which is implemented with a simple switch statement or if...else if...else chain.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • This answer looks like the correct answer to me, converting to an enum would be risky and unreliable (especially if, as is mentioned above, other languages are required) – Slicc Aug 19 '16 at 16:55
  • Is there a tutorial or msds resource for this? I can't seem to find one. – Anon Aug 20 '16 at 05:31
  • @Anon I doubt that there is a tutorial for this problem specifically. This is quite basic C#. Any general C# tutorial or textbook will explain how to write methods and how `switch` and `if...else` work. – Code-Apprentice Aug 20 '16 at 19:29