1

This is my main class. I need to basically come up with a function that accepts my 2D array as an argument.

public static void Main (string[] args)
{
    dailyMenu [,] daysOfMonth = new dailyMenu[4,5];

    for (int column = 0; column < daysOfMonth.GetLength(0); column++) 
    {
        for (int row = 0; row < daysOfMonth.GetLength(1); row++) 
        {
            dailyMenu dm = new dailyMenu ();
            daysOfMonth[column,row] = dm; 
            Console.WriteLine (dm.ToString ());
        }
    } 
}

This is my attempt at creating a function that asks the user to enter an entree and then the function displays all days in which this entree is being served. I want to call the method above in my main method

static void entreeSearch(dailyMenu [,] entrees)
{
    Console.WriteLine ("Please enter the entree you'd like to search for today :)");
    string response = Console.ReadLine ();
    response.ToUpper ();

    if(response == entrees)

} 
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • It seems like your question isn't too clear this time... :( what is your expected input? what is your expected output? can you give examples for that? And what is the problem which you encounter? – Ian Apr 21 '16 at 03:05
  • So basically i want to write a method that accepts my 2d array as an arguement. Then my expected input is one of the 12 entrees i have listed and my output should be all the days in which the entree that the user entered are being served. I want to call the method from the main class – Nnacheta Nnyagu Apr 21 '16 at 03:08
  • In that case, you should start learning `LINQ` `Select`... Also, if your input is 12 entrees, how do you decide which entree is to be used to search? Also, you may need to make your `entree` property readable outside of the class to make your `LINQ` `Select` works... otherwise, you should prepare a `get` method... you are a fairly more complex situation here than before. Not that it is not doable, but to do it right, you need to have *some* changes - not just *one*... so probably you can only get your answer little by little less you try to pick up sometime to learn more - I guess.. – Ian Apr 21 '16 at 03:13
  • Alright so how would i write my 2d array as an arguement – Nnacheta Nnyagu Apr 21 '16 at 03:20
  • OK, I put up a rather long answer this time, because I made three changes to your code. Please take time to take a look, especially on `LINQ` part. – Ian Apr 21 '16 at 03:30

1 Answers1

1

OK, supposing this is your dailyMenu class, you could see that currently your entree is private:

private string entree ="";

firstly, you would need to change it to be a public property with private setter rather private field:

public string entree {get; private set;}

secondly, you probably need to put a method in your dailyMenu to GetEntreeWeekDays:

public string[] GetEntreeWeekDays(){
    //implement something
}

Then given your current code:

static void entreeSearch(dailyMenu [,] entrees)
{
    Console.WriteLine ("PLease enter the entree you'd like to search for today :)");
    string response = Console.ReadLine ();
    response.ToUpper ();
    if(response== entrees 
} 

You could continue from it (and fixing some errors) and using LINQ to find your element:

static void entreeSearch(dailyMenu [,] entrees)
{
    Console.WriteLine ("PLease enter the entree you'd like to search for today :)");
    string response = Console.ReadLine ();
    response = response.ToUpper (); //value must be returned here
    dailyMenu dm = entrees.FirstOrDefault(x => x.entree.ToUpper() == response);
    if (dm == null) //not found
        return;
    string[] weekDays = dm.GetEntreeWeekDays();
    //here you populate the weekdays of a single entree, then just print them out
} 
Community
  • 1
  • 1
Ian
  • 30,182
  • 19
  • 69
  • 107
  • It is giving a ConsoleApp,MainClass.dailyMenu does not contain a definition for FirstorDefault – Nnacheta Nnyagu Apr 21 '16 at 03:55
  • you use `entrees`, not `entree`. And you use `FirstOrDefault()` not `FirstorDefault()`. You also should use `using System.Linq` – Ian Apr 21 '16 at 03:59
  • FirstOrDefault isnt coming up even though I'm using System.Linq – Nnacheta Nnyagu Apr 21 '16 at 04:02
  • I see what's the issue here... the 2d array needs to be converted to 1d array before using `LINQ`, check this out: http://stackoverflow.com/questions/30079485/how-to-use-c-sharp-convert-2-dimensional-array-string-to-string the example is for `string[,]`, but that goes the same for your `dailyMenu[,]`. *temporary* Conversion from `dailyMenu[,]` to `dailyMenu[]` or `IEnumerable` is needed – Ian Apr 21 '16 at 04:05
  • @NnachetaNnyagu that being said, as I previously said, your problem this time is more complex than before. Please relax and take time, ;) and you should actually try to find the answer one by one. Solving *all* problems at the same time does not seem to be a very good idea after all because things like this could be coming... ;) – Ian Apr 21 '16 at 04:09
  • @NnachetaNnyagu relax, take your time. Try to solve one by one... ;) normally, when I first learn how to code - even throughout my first year of coding, I got change my code 5-6 times just to make it *having good design* - not only to make it *work*. So, just take your time to look at your issue, and then you could come back to SO few days later. ;) – Ian Apr 21 '16 at 04:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109750/discussion-between-nnacheta-nnyagu-and-ian). – Nnacheta Nnyagu Apr 21 '16 at 04:43