Home c# Passing an array to another function

Passing an array to another function

Author

Date

Category

How can you implement the transfer of an array from one function to another? I’ve tried several methods already, but unfortunately they don’t want to work. I need the array elements from the Add () function to be passed to the Dump () function and display a list of these elements there. Or is this implemented with a List?

using System;
namespace organizer
{
internal class Program
{
  public static int Day {get; set; }
  public static int Quantity {get; set; }
  public static int Month {get; set; }
  private static void Add (string [] months)
  {
    Console.WriteLine ("Enter the number of the month in which you want to add the case");
    int counter = 1;
    foreach (string mont in months)
    {
      Console.WriteLine (months [counter ++] + $ "- {counter - 1}");
      if (counter & gt; 12)
      {
        break;
      }
    }
    Console.WriteLine (new string ('-', 25));
    Console.Write ("Month number:");
    Month = Convert.ToInt32 (Console.ReadLine ());
    Console.Write ("Enter the day of the month on which you want to add the case:");
    Day = Convert.ToInt32 (Console.ReadLine ());
    if (Month == 2)
    {
      while (Day & gt; 28)
      {
        Console.WriteLine ("There are 28 days in February.");
        Console.Write ("Please re-enter the value:");
        Day = Convert.ToInt32 (Console.ReadLine ());
        if (Day & lt; 28 || Day & lt; = 0)
        {
          break;
        }
      }
    }
    Console.WriteLine (new string ('-', 25));
    Console.Write ("Enter the number of cases you want to add:");
    Quantity = Convert.ToInt32 (Console.ReadLine ());
    Console.WriteLine ($ "What activities do you want to add in {months [Month]} for the day {Day}?");
    Console.WriteLine (new string ('-', 25));
    Console.WriteLine ($ "The number of cases to add: {Quantity}");
    string [] affairs = new string [Quantity];
    for (int i = 0; i & lt; affairs.Length; i ++)
    {
      affairs [i] = Console.ReadLine ();
      Console.WriteLine ($ "Left to add cases: { --Quantity}");
    }
  }
  private static void Dump (string month, int day)
  {
    Console.WriteLine ($ "To-do list for {month [Month]} on day {day}");
    // foreach (var aff in affairs)
    // {
    // Console.Write (aff);
    //}
  }
  private static void Main (string [] args)
  {
    string [] months = new string [13];
    #region Months
    months [0] = "Months:";
    months [1] = "January";
    months [2] = "February";
    months [3] = "March";
    months [4] = "April";
    months [5] = "May";
    months [6] = "June";
    months [7] = "July";
    months [8] = "August";
    months [9] = "September";
    months [10] = "October";
    months [11] = "November";
    months [12] = "December";
    #endregion
    Console.WriteLine ("LKL");
    Console.WriteLine ("Select the number of the operation you want to perform:" +
      "\ n1 - Add to-do list" +
      "\ n2 - Display the to-do list" +
      "\ n3 - Move things over to the next month");
    int choice;
    while (true)
    {
      choice = Convert.ToInt32 (Console.ReadLine ());
      if (choice == 1)
      {
        Add (months);
      }
      else if (choice == 2)
      {
        Dump (months [Month], Day);
      }
      else if (choice == 3)
      {
      }
      else
      {
      }
    }
  }
}

}


Answer 1

This is how you have everything static, so they will refer to all elements of the class through
. Example: Program.

private static void Dump ()
      {
        foreach (var month in Program.Add)
        {
          Console.WriteLine ($ "To-do list for {month [month]} on day {Program.Day}"); 
}
       }

Answer 2

Get the dictionary inside the class in which you will store everything, and transmit it to DUMP. Only Static Remove.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions