开发者

Why isn't my New() firing?

开发者 https://www.devze.com 2023-01-10 22:06 出处:网络
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\"

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 se开发者_C百科em to be creating a new object?

I'm relatively new to C#.


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


Rename the New method to CompaniesDB? Also remove the 'void' modifier.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号