By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,422 Members | 1,455 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,422 IT Pros & Developers. It's quick & easy.

Calling constructor override after doing some checks

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Josef Meile" <Jo********@discussions.microsoft.com> wrote in message
news:BA**********************************@microsof t.com...
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

Nov 16 '05 #2

P: n/a
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
Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.