0
public partial class FormRestaurant : Form
     public Tables[] AllTables = new Tables[4];

I create an array of type Tables which is a class that I made. If I try to reference this from a third class (Parties.cs) by doing:

public void AssignTable()
    {
        for (int i = 0; i < **FormRestaurant.AllTables**.Length; i++)
        {
            if (**FormRestaurant.AllTables[i]**.TableCapacity <= PartySize)
            {

            }
        }
    }

I am told that I need an object reference. How exactly do I do this?

I tried making the array static but it didn't seem to work. Also I get this error when I try to make the AllTables array public.

Error 1 Inconsistent accessibility: field type 'Restaurant_Spil.Tables[]' is less accessible than field 'Restaurant_Spil.FormRestaurant.AllTables'

5
  • 2
    Make sure your Tables class is also publicly accessible. Commented Feb 12, 2015 at 18:42
  • is the Tables Class public ? Commented Feb 12, 2015 at 18:43
  • Can you show the actual code that is giving you these errors? Commented Feb 12, 2015 at 18:46
  • What is Restaurant_Spil.Tables[]? your instance variable? Commented Feb 12, 2015 at 18:48
  • Restaurant_Spil.Tables[] is my solution Commented Feb 12, 2015 at 19:21

2 Answers 2

3

When it says you need an object reference, it's trying to tell you that it needs an instance of your class. Say you have your class:

public partial class FormRestaurant : Form
{
     public Tables[] AllTables = new Tables[4];
}

If you want to get the length of the Tables[] array in Parties.cs, then Parties needs an instance of FormRestaurant; it makes no sense to ask for the length of FormRestaurant.AllTables. Instead you should:

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

the f variable is the object reference that was mentioned in your first error.

Sign up to request clarification or add additional context in comments.

Comments

-1

All you need to do is put your Tables class public. You cannot expose private field type in a public class

1 Comment

So in order to make an array of type Tables public, the class must be public?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.