Witam. Jestem w trakcie tworzenia gry strategicznej w Visual Studio 2015.
Na samym początku przytoczę błąd:
Member 'Form1.Ship.ship_name' cannot be accessed with an instance reference; qualify it with a type name instead
Otóż posiadam form1, a tam klasa, służąca do 'obsługi' moich okrętów:
namespace Second_Tutorial
{
public partial class Form1 : Form
{
private static Ship[] submarine; // nie mogę z tego usunąć 'static' ponieważ wywala błąd: "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; // indeks, do wyświetlania w ShipControl elementu tablicy submarine[selected_ship].jakieś_pola
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 method of ship moving
public void move(int heading, int speed)
{
if (heading == 0) ship_posY -= speed; // ship is heading NORTH
if (heading == 45) // ship is heading NORTH-EAST
{
ship_posX += speed; ship_posY -= speed;
}
if (heading == 90) ship_posX += speed; // ship is heading EAST
if (heading == 135) // ship is heading SOUTH-EAST
{
ship_posX += speed; ship_posY += speed;
}
if (heading == 180) ship_posY += speed; // ship is heading SOUTH
if (heading == 225) // ship is heading SOUTH-WEST
{
ship_posX -= speed; ship_posY += speed;
}
if (heading == 270) ship_posX -= speed; // ship is heading WEST
if (heading == 315) // ship is heading NORTH-WEST
{
ship_posX -= speed; ship_posY -= speed;
}
}
// 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);
}
Chciałbym wyświetlić form ShipControl za pomocą kliknięcia w PictureBox, co mi się udaje:
```csharp
private void PictureBoxSub1_DoubleClick(object sender, EventArgs e)
{
selected_ship = 0;
ShipControl FormShipControl = new ShipControl();
FormShipControl.ShowDialog();
}
A w ShipControl chciałbym mieć możliwość wyświetlić w label.Text zawartości np submarine[0].ship_name:
namespace Second_Tutorial
{
public partial class ShipControl : Form
{
int displaying_ship = Form1.selected_ship; // odczytuję z Form1 zmienną public static int selected_ship = 0;
public ShipControl()
{
InitializeComponent();
/*
poniższa linijka wywala błąd:
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;
/*
za to działa to, co poniżej:
ale w tym wypadku wyświetla mi tylko submarine[1].ship_name o ile displaying_ship będzie równe 1
problem pojawi się, jeśli z Form1 przekazana zmienna selected_ship będzie == 0
*/
string displaying_name2 = Form1.Ship.ship_name;
}
}
}
Ok. Wyświetlony mam ShipControl a następnie w różnych labelach chciałbym móc wyświetlić np submarine[0].ship_name lub pozostałe z klasy z Form1. Jak zmodyfikować kod? Dziękuję za pomoc.