473,396 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Calling constructor override after doing some checks

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
2 3837
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: William Payne | last post by:
Hello, consider these following two classes. A base class, class MDIChildWindow, and a class inherting from that base class, class Document. In the static base member function callback() I obtain a...
4
by: Claire | last post by:
I'm having real brain failure today. I've done this lots of times with constructors but not with virtual methods and the compiler complains because Ive put the :base(foo) after the function...
20
by: Ole Hanson | last post by:
I am accessing my database through an interface, to allow future substitution of the physical datastore - hence I would like to declare in my Interface that my DAL-objects implementing the...
5
by: Bennett Haselton | last post by:
I've noticed that if you enter the following code in the codebehind page for an .aspx page, it won't compile because the call to Trace.Write() is not valid except in methods of a class derived from...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
12
by: st_ev_fe | last post by:
I've noticed that when constructing a subclass, the base class get's it's contructors called. Is there some way to avoid this? The base class has one variable, which gets initialised to 0. ...
1
by: JP | last post by:
Hi, I am facing a strange problem, please take a look at the code below: public SaveTree() { ........ ...... ......//some code Initranges()
6
by: Henrik Goldman | last post by:
Hi I've had a problem with gcc mac osx which I think I figured out. I would like to double check with people here to see if my understanding is correct: I have a class A which class B inherit...
0
by: BornTOCode | last post by:
Hello, I am attempting to call a (Delphi) win32 DLL from a Delphi.Net webservice. I am using a slightly modified version of the hello world webservice that comes with Delphi 2006. The DLL...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.