Передача параметра в конструктор - C#
Формулировка задачи:
Дамы и господа, нужна помощь!
Есть следующий класс с конструктором Tank(int sizeField)
в классе Model создаю объект tank и использую параметр sizeField (поле класса Model), но выдает ошибку: Error 1 A field initializer cannot reference the non-static field, method, or property 'Tanks.Model.sizeField' E:\C#\MyProject\Tanks\Tanks\Tanks\Model.cs 28 37 Tanks
Хотелось бы именно поле sizeField использовать как аргумент в конструкторе. Жду совета, плиззз)))
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace Tanks
{
public class Tank : IRun, ITurn, ITransparent
{
//движение танка (координаты)
int x, y, direct_x, direct_y; //координаты танка, напоавление танка
int sizeField;
public int Direct_Y
{
get { return direct_y; }
set
{
if (value == -1 || value == 0 || value == 1)
direct_y = value;
}
}
public int Direct_X
{
get { return direct_x; }
set
{
if (value == -1 || value == 0 || value == 1)
direct_x = value;
}
}
public int X
{
get { return x; }
set { x = value; }
}
public int Y
{
get { return y; }
set { y = value; }
}
Random r;
//Изображение танка
TankImg tankImg = new TankImg();
Image image;
public Image Image
{
get { return image; }
set { image = value; }
}
public Tank(int sizeField)
{
this.sizeField = sizeField;
image = tankImg.Img;
Direct_X = 0;
Direct_Y = 1;
X = 80;
Y = 80;
}
//метод движение танка
public void Run()
{
x += Direct_X;
y += Direct_Y;
if (Math.IEEERemainder(x, 40) == 0 && Math.IEEERemainder(y, 40) == 0)
Turn();
Transparent();
}
public void Turn()
{
r = new Random();
{
if (r.Next(5000) < 2500)//двигаемся по горизонтали
{
if (Direct_X != 0)
Direct_X = Direct_X;
else
{
Direct_Y = 0;
while (Direct_X == 0)
Direct_X = r.Next(-1, 2);
}
}
else//двигаемся по вертикали
{
if (Direct_Y != 0)
Direct_Y = Direct_Y;
else
{
Direct_X = 0;
while (Direct_Y == 0)
Direct_Y = r.Next(-1, 2);
}
}
}
}
//метод прозрачности стен
public void Transparent()
{
if (x == -1)
x = sizeField - 21;
if (x == sizeField - 19)
x = 1;
if (y == -1)
y = sizeField - 21;
if (y == sizeField - 19)
y = 1;
}
}
}namespace Tanks
{
public class Model
{
int sizeField;
int amountTanks;
int amountApples;
public int speedGame;
public GameStatus gameStatus;//статус игры
public Model(int sizeField, int amountTanks, int amountApples, int speedGame)
{
this.sizeField = sizeField;
this.amountTanks = amountTanks;
this.amountApples = amountApples;
this.speedGame = speedGame;
gameStatus = GameStatus.stoping;//первоначальный статус игры
}
public Tank tank = new Tank();
public Wall wall = new Wall();
//запуск игры
public void play()
{
while (gameStatus==GameStatus.playing)
{
Thread.Sleep(speedGame);
tank.Run();
}
}
}
}Решение задачи: «Передача параметра в конструктор»
textual
Листинг программы
public Tank tank = new Tank(this.sizeField);