我有一个错误(我在Stackoverflow上看到了有关此问题的其他帖子,但没有找到解决方案):
无法通过实例引用访问成员“ 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'"
但这没有帮助。
答案 0 :(得分:0)
这是因为您将ship_name设为静态。您只能通过输入Ship.ship_name来访问它。该类的对象无法访问静态字段。