Reputation: 21
I'm learning OOP in C# and I get a problem during coding. I would like to create a class with constructor, which will read attributes by Console.ReadLine
. Unfortunately, I get a strange error which I can't understand it.
This is my class:
public class Klasa
{
int zarobki;
string nazwisko;
string stanowisko;
public Klasa(string a, string b, string c)
{
a = (Console.ReadLine());
b = (Console.ReadLine());
c = (Console.ReadLine());
zarobki = int.Parse(a);
nazwisko = b;
stanowisko = c;
}
}
After compile, I got the following error:
"Severity Code Description Project File Line Suppression State Error CS7036 There is no argument given that corresponds to the required formal parameter 'a' of 'Klasa.Klasa(string, string, string)' ConsoleApplication17 c:\users\dell\documents\visual studio 2015\Projects\ConsoleApplication17\ConsoleApplication17\Program.cs 13 Active ".
Upvotes: 1
Views: 97
Reputation: 3939
You should call the constructor from some other place. Like this in a console application:
void Main()
{
var a = (Console.ReadLine());
var b = (Console.ReadLine());
var c = (Console.ReadLine());
Klasa k = new Klasa(a,b,c);
}
And then your class should be something like this:
public class Klasa
{
int zarobki;
string nazwisko;
string stanowisko;
public Klasa(string a, string b, string c)
{
zarobki = int.Parse(a);
nazwisko = b;
stanowisko = c;
}
}
This solves your problem but you are structurally misguided. I think you should start by reading a book or an article before jumping into code.
Upvotes: 0