-2

I've written this code but I am missing the part where I can actually type the array out to the console.

public class Program
{
    public static void Main()
    {
        Parties p;
        // What do I type here to print out the array to the console?
    }
}

public partial class FormRestaurant
{
    public int[] AllTables = { 3, 5, 6, 3 };
}

public class Parties
{
    int length;
    FormRestaurant f;
    public Parties()
    {
        f = new FormRestaurant();
        length = f.AllTables.Length;
    }
}

Can somebody help please? I expected the console to print out array.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
Amjad Hani
  • 11
  • 1
  • More or less the same as [here](https://stackoverflow.com/questions/16265247/printing-all-contents-of-array-in-c-sharp) – Julian Aug 25 '23 at 12:04

2 Answers2

1

Does this do it?

public class Program
{
    public static void Main()
    {
        Parties p = new Parties();
        foreach (var x in p.f.AllTables)
            Console.WriteLine(x);
    }
}

public partial class FormRestaurant
{
    public int[] AllTables = { 3, 5, 6, 3 };
}

public class Parties
{
    int length;
    public FormRestaurant f;
    public Parties()
    {
        f = new FormRestaurant();
        length = f.AllTables.Length;
    }
}

That outputs:

3
5
6
3
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
0

create a public void method that can print all the items from the array. This is to make sure you don't need to set FormRestaurant f to public. Yet still accomplish what you want.

public class Program
{
    public static void Main()
    {
        Parties p = new Parties();
        p.print();
    }
}

public partial class FormRestaurant
{
    public int[] AllTables = { 3, 5, 6, 3 };
}

public class Parties
{
    int length;
    FormRestaurant f;
    public Parties()
    {
        f = new FormRestaurant();
        length = f.AllTables.Length;
    }

    public void print()
    {
        foreach (var x in f.AllTables)
            Console.WriteLine(x);
    }
}
Light-Lens
  • 81
  • 7