Инициализировать массив в конструкторе структуры - C#
Формулировка задачи:
Привет всем.
Знатоки подскажите. Как инициализировать массив в конструкторе структуры, массив объявлен в майне и пользователь сам задает размер массива через переменную,при создании нового объекта конструктор каждый раз требует указать размер массива, а мне нужно указать размер только один раз.Но если я не объявляю массив в структуре то ест-но никто его там не видит.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace kurs2_21_9_2
{
enum Positions { menedjer = 1, programist = 2, stajor = 3 }
struct Employee
{
//Employee[] masivMan;
private string name;
private string famName;
private Positions doljnost;
private float oklad;
private byte staj;
public Employee(string name, string famName, Positions doljnost, float oklad, byte staj//,intb)
{
this.name = name;
this.famName = famName;
this.oklad = oklad;
this.staj = staj;
this.doljnost = doljnost;
//Employee[] masivMan=new Employee[b]
}
public string pName
{
set { pName = value; }
get { return name; }
}
public string pfamName
{
set { pfamName = value; }
get { return famName; }
}
public Positions pdoljnost
{
set { pdoljnost = value; }
get { return doljnost; }
}
public float pOklad
{
set { pOklad = value; }
get { return oklad; }
}
public byte pStaj
{
set { pStaj = value; }
get { return staj; }
}
/* public override void Tostring()
{
for (int i = 0; i <= masivMan.Length; i++)
Console.WriteLine(masivMan[i].name,masivMan[i].famName,masivMan[i].doljnost,masivMan[i].oklad,masivMan[i].staj);
} */
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("vvedite razmer bazi");
int b = Convert.ToInt32(Console.ReadLine());
Employee[] masivMan = new Employee[b];
Positions newPositions = Positions.menedjer;
Console.WriteLine("vvedite imya");
string name = Console.ReadLine();
Console.WriteLine("vvedite familiu");
string famName = Console.ReadLine();
Console.WriteLine("viberite odnu iz doljnostey");
Console.WriteLine("dlya vebora menedjer 1");
Console.WriteLine("dlya vebora programist 2");
Console.WriteLine("dlya vebora stajor 3");
int a = Convert.ToInt32(Console.ReadLine());
if (a == 1)
{
newPositions = Positions.menedjer;
}
else if (a == 2)
{
newPositions = Positions.programist;
}
else if (a == 3)
{
newPositions = Positions.stajor;
}
Console.WriteLine("vvedtte jelarmiy oklad");
float oklad = Convert.ToSingle(Console.ReadLine());
Console.WriteLine("ukajite svoy staj");
byte staj = Convert.ToByte(Console.ReadLine());
Employee man = new Employee(name, famName, newPositions, oklad, staj//,b);
Console.WriteLine("dlya vvoda usera 1/n");
Console.WriteLine("dlya vivoda vseh userov 2/n");
Console.WriteLine("dlya vihoda 3/n");
int r=Convert.ToInt32(Console.ReadLine());
switch(r)
{
case 1:
{
for (int i = 1; i <= masivMan.Length; i++)
{
Console.WriteLine("vvedite imya");
string name = Console.ReadLine();
Console.WriteLine("vvedite familiu");
string famName = Console.ReadLine();
Console.WriteLine("viberite odnu iz doljnostey");
Console.WriteLine("dlya vebora menedjer 1");
Console.WriteLine("dlya vebora programist 2");
Console.WriteLine("dlya vebora stajor 3");
int q = Convert.ToInt32(Console.ReadLine());
if (q == 1)
{
newPositions = Positions.menedjer;
}
else if (q == 2)
{
newPositions = Positions.programist;
}
else if (q == 3)
{
newPositions = Positions.stajor;
}
Console.WriteLine("vvedtte jelarmiy oklad");
float oklad = Convert.ToSingle(Console.ReadLine());
Console.WriteLine("ukajite svoy staj");
byte staj = Convert.ToByte(Console.ReadLine());
Employee man = new Employee(name, famName, newPositions, oklad, staj);
}
}
break;
case 2:
{
//man.Tostring();
}
break;
default:
break;
}
Console.ReadKey();
}
}
}Решение задачи: «Инициализировать массив в конструкторе структуры»
textual
Листинг программы
Employee[] masivMan;
private string name;
private string famName;
private Positions doljnost;
private float oklad;
private byte staj;
public Employee(string name, string famName, Positions doljnost, float oklad, byte staj, int b)
{
this.name = name;
this.famName = famName;
this.oklad = oklad;
this.staj = staj;
this.doljnost = doljnost;
masivMan=new Employee[b]
}