Calling constructor override after doing some checks

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have this constructor:

public CExcelDatabase(string host, string user, string password, string
database,
bool promptCredentials, int
findExcelInstance, bool readOnly)
{
//Some code comes here
}

Then I wrote an override like this:

public CExcelDatabase(string host, string user, string password, string
database,
bool promptCredentials, Excel.Application
excelApp,
bool readOnly)
{
if (excelApp == null)
{
//An error must be raised
}
else
{
this.ExcelApplication = excelApp;
//Note: PROVIDED_EXCEL_INSTANCE is a constant I defined
CExcelDatabase(host,user, password, database, promptCredentials,
PROVIDED_EXCEL_INSTANCE,readOnly);
}
}

I get an error:
....(360): 'EVA.CExcelDatabase' denotes a 'class' which is not valid in the
given context

Which I found is quite normal when doing things like that. I figured out
that you can avoid this error by doing:

public CExcelDatabase(string host, string user, string password,
string database, bool promptCredentials, Excel.Application excelApp,
bool readOnly):
this(host,user, password, database, promptCredentials,
PROVIDED_EXCEL_INSTANCE,readOnly)
{
//Some code comes here
}

But the override will be called before doing some checks I need to excecute:

1) First I need to check that I got an excelApp object (It isn't null),
2) then I need to initialize the private property "ExcelApplication"
3) finally call the override, which uses the value of ExcelApplication.

How can I do this?

Regards,
Josef
 
Josef,

Generally, you won't need to do this kind of stuff before your base
constructor is called. If you have logic in a derived class which affects
the base constructor, then that logic really should be in the base. Also,
if you have a condition under which you need to throw an exception and keep
the object from being created, it shouldn't matter if the base constructor
is called (generally).

If you really need to control the order of these operations, then you
should move the code out into a protected method called Initialize (or
something of the sort) which is also virtual at the base. Then, in the base
constructor, call the Initialize method, and override it in each subclass.
Then, in those overrides, you can call the base implementation of Initialize
when you choose.

Hope this helps.
 
Hi Nicholas,
Generally, you won't need to do this kind of stuff before your base
constructor is called. If you have logic in a derived class which affects
the base constructor, then that logic really should be in the base. Also,
if you have a condition under which you need to throw an exception and keep
the object from being created, it shouldn't matter if the base constructor
is called (generally).
Actually, the constructor overrides are on the same class. So, I don't have
a base class. What I have is a class with several constructors.
If you really need to control the order of these operations, then you
should move the code out into a protected method called Initialize (or
something of the sort) which is also virtual at the base. Then, in the base
constructor, call the Initialize method, and override it in each subclass.
Then, in those overrides, you can call the base implementation of Initialize
when you choose.
Yes, that did the trick.

Thanks for your reply,
Josef
 
Back
Top