Reputation: 17
I am making a sports calender which has venues, events and athletes.
basically when I try to run the program, I get the error that "Sport2012.Venue does not contain a constructor that takes 0 arguments.
//Method to add a new Venue to program
public void addVenue(Venue v)
{
m_Venue.Add(new Venue());
}
The code above errors at the m_Venue.Add(new Venue());
public String m_venueName;
public String m_venueAddress;
private List<Event> m_Events;
public Venue(String venueName, String venueAddress)
{
m_venueName = venueName;
m_venueAddress = venueAddress;
m_Events = new List<Event>();
}
The code above shows the constructor for Sport2012.Venue.
Basically do i create a new constructor or alter the first extract
Thanks in advance
Daniel
Upvotes: 0
Views: 363
Reputation: 1758
In C# all non-static classes have an implicit default, parameter-less (no args) constructor, unless you explicitly create a constructor. The act of defining any constructor means there is no longer an implicit constructor. In this case, your class has a constructor that takes two arguments, therefore, you have no implicit constructor. Your compiler catches it. You need to either a) add a parameter-less constructor or b) change your instantiation (new Venue..) to use the existing constructor.
Just be aware of why your constructor takes those arguments in the first place. Can your Venue object 'work' without a name or address? And, is there another way to supply a name or address (e.g., using public property setters). If so, you can think of those as optional. In that case, your existing constructor is for convenience and you can safely add a parameterless constructor. If you absolutely need Venue name/address and there is no way to set them without breaking encapsulation, then you should change the instantiation logic to supply the arguments.
Good rule of thumb is that the creator of the object supplies the object with its essential components/services/dependencies via the constructor. Optional components/services/dependencies can be supplied after construction.
Upvotes: 1
Reputation: 20314
You have two minor conceptual errors here.
The first problem is you are creating a new Venue with no arguments -> .Add(new Venue())
. The constructor of Venue
takes two strings. This is the source of your compile error. .Add(new Venue("foo", "bar"));
would compile, for example.
The second problem is that you are passing a Venue
to the addVenue
method but not adding it to the list. You probably meant m_Venue.Add(v);
and this is probably what you should do.
Upvotes: 0
Reputation: 14453
m_Venue.Add(v); would get rid of that error
All classes in C# have a default constructor one with 0 arguments until you define a constructor. Once you have explicitly defined a constructor you also need to add one with 0 arguments (if that is what you need)
Upvotes: 0
Reputation: 2961
m_Venue.Add(new Venue("lords", "london"));
if the add venue method already takes in a venue, then just do m_Venue.Add(v)
Upvotes: 0
Reputation: 8408
I suppose your addVenue method is not correct
public void addVenue(Venue v)
{
// add the provided instance, not a NEW one
m_Venue.Add(v);
}
Upvotes: 5
Reputation: 241583
From your code, it looks like you're trying to add v
to m_Venue
so you should just say
public void addVenue(Venue v) {
m_Venue.Add(v);
}
You want to add the instance of Venue
that was passed in as a parameter v
, not a new instance which is what new Venue()
would be creating if a parameterless constructor existed.
Upvotes: 5