I have been trying to link my "person" class values with "workers" class in the "Read" method.
When I write
Console.WriteLine(person.GetName());
it gives me all the values perfectly, but if I write
Console.WriteLine(workers.Get(i).GetName());
it shows "Object reference not set to an instance of an object." I have written "workers.Set(person);" and I thought that this would link the "person" with "workers", but as I can see, it doesn't, and I dont know what I did wrong and how to fix it. How do I properly link "workers" with "person"?
My code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace L5
{
class Program
{
class Person
{
private string surname;
private string name;
private string bank;
private string num;
private int pieces;
public Person()
{
surname = "";
name = "";
bank = "";
num = "";
pieces = 0;
}
public void Set(string surname, string name, string bank, string num)
{
this.surname = surname;
this.name = name;
this.bank = bank;
this.num = num;
}
public void SetPieces(int piece) { pieces = piece; }
public string GetName() { return name; }
public string GetSurname() { return surname; }
public string GetBank() { return bank; }
public string GetNum() { return num; }
public int GetPieces() { return pieces; }
public override string ToString()
{
string s;
s = string.Format("{0, -15} {1, -10} {2,-10} {3, -30}",
surname, name, bank, num);
return s;
}
}
class Matrix
{
const int CMaxEil = 10;
const int CMaxSt = 100;
private Person[] A;
private int[,] W;
public int n { get; set; }
public int m { get; set; }
public Matrix()
{
n = 0;
A = new Person[CMaxSt];
m = 0;
W = new int[CMaxEil, CMaxSt];
}
public Person Get(int nr) { return A[nr]; }
public void Set(Person ob) { A[n++] = ob; }
public void SetW(int i, int j, int r) { W[i, j] = r; }
public int GetW(int i, int j) { return W[i, j]; }
}
static void Main(string[] args)
{
const string Cfd = "..\\..\\data.txt";
const string Cfr = "..\\..\\results.txt";
Matrix workers = new Matrix();
Read(Cfd, ref workers);
if (File.Exists(Cfr))
File.Delete(Cfr);
}
static void Read(string fd, ref Matrix workers)
{
int nn, mm, number;
string surname, name, bank, num;
double price;
string line;
Person person;
using (StreamReader reader = new StreamReader(fd))
{
line = reader.ReadLine();
string[] parts = line.Split(' ');
nn = int.Parse(parts[0]);
mm = int.Parse(parts[1]);
workers.n = nn;
workers.m = mm;
price = double.Parse(parts[2]);
for (int i = 0; i < nn; i++)
{
line = reader.ReadLine();
string[] partss = line.Split(' ');
surname = partss[0];
name = partss[1];
bank = partss[2];
num = partss[3];
person = new Person();
person.Set(surname, name, bank, num);
workers.Set(person);
}
for (int i = 0; i < nn; i++)
{
line = reader.ReadLine();
string[] partsss = line.Split(' ');
for (int j = 0; j < workers.m; j++)
{
number = int.Parse(partsss[j]);
workers.SetW(i, j, number);
}
}
}
}
}
}
And my data that I read (just in case):
4 7 2.99
Surname Name Seb 988957532897855
Surnamee Namee Swedbank 623378348393499
Surnameee Nameee Luminor 234132535698906
Surnameeee Nameeee Seb 989677678986896
5 7 2 9 9 3 5
1 2 3 1 4 1 1
8 9 8 7 6 9 9
4 6 7 4 3 8 3
What I have tried:
-----------------------------------------------------