-1

I have an error (i saw other posts about this problem on Stackoverflow but did not find a solution):

Member 'Form1.Ship.ship_name' cannot be accessed with an instance reference; qualify it with a type name instead

I have a Form1 with a class to handle my ships:

namespace Second_Tutorial
{

public partial class Form1 : Form
    {
      public static Ship[] submarine; // can't remove 'static' because of error: "An object reference is required for the non-static field, method, or property 'Form1.submarine'"
      internal static ShipControl FormShipControl;
      internal static Form1 form1;
      public static int selected_ship = 0; // index for displaying ShipControl's array elements submarine[selected_ship].some_fields

public partial class Ship
        {
            public static string ship_name; // name of a ship
            public double ship_posX; // position X on map
            public double ship_posY; // position Y on map
            public int ship_heading; // current heading
            public int ship_surf_speed_max; // max speed of a surfaced ship
            public int ship_submerged_speed_max; // max speed of a submerged ship
            public int ship_current_speed; // current speed of a ship
            public string ship_class_type; // surface or submarine
            public string ship_class_name; // name of class; for example Gato or Balao
            public string ship_foe; // friend or enemy?
            public bool is_controllable; // is controllable by player?
            public int ship_depth; // current depth of a submarine (if a submarine)
            public bool ship_alive; // is ship "alive"

            // initialize instance of 100 ships in array
            // without this there will be errors

            public static void Initial()
            {
                for (int i = 0; i < 100; i++)
                {
                    submarine[i] = new Ship();
                }
            }



            // create ship and give him a starting position

            public void create_ship(string name, double posx, double posy, int heading, int surf_speed_max, int submerged_speed_max, int current_speed, string class_type, string class_name, string foe, bool controllable, int depth, bool alive)
            {
                ship_name = name; // own ship name
                ship_posX = posx; // position X on map
                ship_posY = posy; // position Y on map
                ship_heading = heading; // current heading
                ship_surf_speed_max = surf_speed_max; // max speed of a surfaced ship
                ship_submerged_speed_max = submerged_speed_max; // max speed of a submerged ship
                ship_current_speed = current_speed; // current speed of a ship
                ship_class_type = class_type; // surface or submarine
                ship_class_name = class_name; // name of class; for example Gato or Balao
                ship_foe = foe; // friend or enemy?
                is_controllable = controllable; // is controllable by player?
                ship_depth = depth; // current depth of a submarine (if a submarine)
                ship_alive = alive;
            }
        } // END OF class SHIP
} // END OF PUBLIC PARTIAL CLASS FORM1
} // END OF NAMESPACE

private void Form1_Load(object sender, EventArgs e)
        {
            // forms load == game starts
            submarine = new Ship[100];

            // ==========================================================
            // ==========================================================
            // ================== CREATE SHIPS HERE =====================
            // ==========================================================
            // ==========================================================

            // 1 - name; 2 - position X; 3 - position Y; 4 - heading; 5 - surf speed max; 6 - sub speed max;
            // 7 - current speed;

            Ship.Initial(); //         1         2       3  4    5   6  7
            submarine[0].create_ship("USS Sargo", 780,  200, 0, 21, 9, 2, "surf", "Sargo", "friend", true, 0, true);
            submarine[1].create_ship("USS Saury", 2200, 450, 0, 21, 9, 2, "surf", "Sargo", "friend", true, 0, true);
        }`


I'd like to display another form called ShipControl which works well:

private void PictureBoxSub1_DoubleClick(object sender, EventArgs e)
        {
            selected_ship = 0;
            ShipControl FormShipControl = new ShipControl();
            FormShipControl.ShowDialog();
        }

And here, in ShipControl i need to display in label.Text values of

submarine[selected_ship].ship_name:
namespace Second_Tutorial
{
    public partial class ShipControl : Form
    {
        int displaying_ship = Form1.selected_ship; // reading from Form1 a variable: public static int selected_ship = 0;


        public ShipControl()
        {

            InitializeComponent();

            /*
             below line causes error:

            Member 'Form1.Ship.ship_name' cannot be accessed with an instance reference; qualify it with a type name instead
            */
            string displaying_name = Form1.submarine[displaying_ship].ship_name;
            labelUnitName.Text = displaying_name;

            /*
             but this line below works fine:
             but in this case i can only display submarine[1].ship_name 
            */

            string displaying_name2 = Form1.Ship.ship_name;
        }
    }
}

How can i display submarine[0].ship_name; on another form?

I tried to remove static from:

public partial class Form1 : Form
    {
      public Ship[] submarine;

and remove in Form_Load:

Ship.Initial();

by typing its method values just instead of calling this method

This prevents from displaying error quoted above:

public partial class Form1 : Form
    {
      public static Ship[] submarine; // can't remove 'static' because of error: "An object reference is required for the non-static field, method, or property 'Form1.submarine'"

But it did not help.

Mariusz
  • 45
  • 1
  • 10
  • Remove `static` from `public static string ship_name` if you want `ship_name` to be an instance field. Better yet, you should not have public fields in a `class` - they should be properties. And it's redundant to include the class name in the property name. So it would probably be best to make it a property and remove the class name from it: `public string Name { get; set; }` – Rufus L Feb 05 '19 at 18:18
  • Also, why are you defining the `Ship` class inside the `Form1` class? Shouldn't it be an independent class of it's own? – Rufus L Feb 05 '19 at 18:21
  • I removed 'public static' but it changes nothing. Still same error. – Mariusz Feb 05 '19 at 18:24
  • Don't remove `public`, just `static`. Also, `Initial` should not reference `submarine`, since that is a member of a completely different class. You could re-write it to take in a `Ship[]`, though, and then the `Form1` could pass it's `submarine` array to the method. Try to make the `Ship` class completely separate from `Form1`, and you will have far fewer issues. – Rufus L Feb 05 '19 at 18:29
  • Maybe i defined it inside Form1 because i am still learning vs and c# :). I can add new file called Ship and move this class from Form1 to Ship.cs and add this file to my solution, but when i want to modify my code i get error visible here: [link](https://imgur.com/a/bcCEXmy) – Mariusz Feb 05 '19 at 18:30

1 Answers1

0

It is because you made the ship_name static. You can only access that by typing Ship.ship_name. Static fields are not accessible to objects of the class.