Reputation: 95
Consider:
using System;
namespace TuristickaAgencija
{
public class World
{
protected char[] dest;
public World(char[] dest)
{
this.dest = dest;
}
}
class Client : World
{
private char[] name;
private char[] surname;
private int age;
private int noCounter;
private bool hasVehicle;
public Client(char[] name, char[] surname, int age, int noCounter, bool hasVehicle) : base(dest)
{
this.name = name;
this.surname = surname;
this.age = age;
this.noCounter = noCounter;
this.hasVehicle = hasVehicle;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
I'm getting 2 errors:
An object reference is required for the non-static field, method or property 'World.dest'
Missing partial modifier on declaration of type 'Program'
I want all classes to be in the same file.
Upvotes: 0
Views: 483
Reputation: 111
You need to pass a valid value in order to initialize dest. dest is not defined in the csope of constructor yet, you are calling the base ctor and should intialzie it.
you can pass name, or surename or add another param and use it.
As for the partial error probably you have already a Program class defined somewhere else in your code and you cannot declare 2 classes with the same name.
hope this helps.
Upvotes: 1
Reputation: 169190
It's unclear what dest
you are trying to pass the constructor of the base class. You can't pass a reference to a field of an instance that has not yet been instantiated.
You should either add a parameter to Client
constructor and pass this one to the base constructor:
public Client(char[] name, char[] surname, int age, int noCounter, bool hasVehicle,
char[] dest) : base(dest)
{
this.name = name;
this.surname = surname;
this.age = age;
this.noCounter = noCounter;
this.hasVehicle = hasVehicle;
}
Or you should pass some default value like for example null
or an empty array:
public Client(char[] name, char[] surname, int age, int noCounter, bool hasVehicle) : base(default)
Upvotes: 2