Reputation: 368
I'm having problems with the code below. Whenever I create a new instance of the class below and call "Select" I get an object reference not set to an instance of an object on the "this.db"
namespace SPI {
class CompaniesDB
{
private DataContainer db;
public void New() {
this.db = new DataContainer();
}
public Company Select(int companyID) {
return this.db.Company_Get(companyID).SingleOrDefault();
}
}
}
Can someone point me at why my "New()" doesn't seem to be creating a new object?
I'm relatively new to C#.
Upvotes: 0
Views: 209
Reputation: 13780
In this case, the DataContainer constructor requires no arguments and instantion isn't affected by a variable within your New function, therefore you could do away with that function entirely. You could instantiate the DataContainer upon declaration of the private db variable:
namespace SPI {
class CompaniesDB
{
private DataContainer db = new DataContainer();
public Company Select(int companyID) {
return this.db.Company_Get(companyID).SingleOrDefault();
}
}
}
Edit, further information as per Mike Jolley's request:
This is really down to preference, both solutions will work and are safe.
You could use the following code:
namespace SPI {
class CompaniesDB
{
private DataContainer db;
public CompaniesDB()
{
db = new DataContainer();
}
public Company Select(int companyID) {
return this.db.Company_Get(companyID).SingleOrDefault();
}
}
}
This code is good, you are creating an instance of DataContainer whose reference will be assigned to the db variable. You are ensuring that the db variable contains a reference when you want to start using it.
However, this instantiation is always the same, therefore you could get rid of the assignment within the constructor and just create a normal DataContainer by default. This would also ensure that the variable always contains a reference to a DataContainer upon instantiation of a CompaniesDB object:
namespace SPI {
class CompaniesDB
{
private DataContainer db = new DataContainer();
public Company Select(int companyID) {
return this.db.Company_Get(companyID).SingleOrDefault();
}
}
}
The need for a constructor would be clear if the DataContainer constructor took an argument:
namespace SPI {
class CompaniesDB
{
private DataContainer db;
public CompaniesDB(string name)
{
db = new DataContainer(name);
}
public Company Select(int companyID) {
return this.db.Company_Get(companyID).SingleOrDefault();
}
}
}
The introduction of an argument would mean that it would be better to create the instance of DataContainer in the constructor.
Upvotes: 0
Reputation:
You don't name a constructor as New
. You name it with the class Name.
Try
namespace SPI {
class CompaniesDB
{
private DataContainer db;
public CompaniesDB() {
this.db = new DataContainer();
}
public Company Select(int companyID) {
return this.db.Company_Get(companyID).SingleOrDefault();
}
}
}
MSDN page on constructors: http://msdn.microsoft.com/en-us/library/ms173115.aspx
Upvotes: 11
Reputation: 519
Do you intend for New() to be your constructor? If so, the syntax for constructors in C# asks for the name of the class. So your constructor should be something like:
public CompaniesDB()
{
this.db = new DataContainer();
}
hope that helps.
Upvotes: 1
Reputation: 22731
Rename the New method to CompaniesDB? Also remove the 'void' modifier.
Upvotes: 1