会员不能与实例引用来访问[复制]

问题描述 投票:-1回答:1

这个问题已经在这里有一个答案:

我有一个错误(我看到其他职位有关#2这个问题,但并没有找到一个解决方案):

会员“Form1.Ship.ship_name”不能用一个实例引用来访问;与类型名来限定它,而不是

我有一个Form1中有一个类来处理我的船:

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();
        }

在这里,在ShipControl我需要的label.Text值显示

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;
        }
    }
}

如何显示潜艇[0] .ship_name;在另一种形式?

我试图从消除静电:

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

并删除的Form_Load:

Ship.Initial();

by typing its method values just instead of calling this method

这可以防止从上方显示所引述错误:

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'"

但它并没有帮助。

c#
1个回答
0
投票

这是因为你所做的ship_name静态的。您只能访问通过键入Ship.ship_name。静态字段是不是类的对象访问。

© www.soinside.com 2019 - 2024. All rights reserved.