473,756 Members | 1,964 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling constructor override after doing some checks

Hi,

I have this constructor:

public CExcelDatabase( string host, string user, string password, string
database,
bool promptCredentia ls, int
findExcelInstan ce, bool readOnly)
{
//Some code comes here
}

Then I wrote an override like this:

public CExcelDatabase( string host, string user, string password, string
database,
bool promptCredentia ls, Excel.Applicati on
excelApp,
bool readOnly)
{
if (excelApp == null)
{
//An error must be raised
}
else
{
this.ExcelAppli cation = excelApp;
//Note: PROVIDED_EXCEL_ INSTANCE is a constant I defined
CExcelDatabase( host,user, password, database, promptCredentia ls,
PROVIDED_EXCEL_ INSTANCE,readOn ly);
}
}

I get an error:
....(360): 'EVA.CExcelData base' 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 promptCredentia ls, Excel.Applicati on excelApp,
bool readOnly):
this(host,user, password, database, promptCredentia ls,
PROVIDED_EXCEL_ INSTANCE,readOn ly)
{
//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 "ExcelApplicati on"
3) finally call the override, which uses the value of ExcelApplicatio n.

How can I do this?

Regards,
Josef
Nov 16 '05 #1
2 3875
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.co m
"Josef Meile" <Jo********@dis cussions.micros oft.com> wrote in message
news:BA******** *************** ***********@mic rosoft.com...
Hi,

I have this constructor:

public CExcelDatabase( string host, string user, string password, string
database,
bool promptCredentia ls, int
findExcelInstan ce, bool readOnly)
{
//Some code comes here
}

Then I wrote an override like this:

public CExcelDatabase( string host, string user, string password, string
database,
bool promptCredentia ls, Excel.Applicati on
excelApp,
bool readOnly)
{
if (excelApp == null)
{
//An error must be raised
}
else
{
this.ExcelAppli cation = excelApp;
//Note: PROVIDED_EXCEL_ INSTANCE is a constant I defined
CExcelDatabase( host,user, password, database, promptCredentia ls,
PROVIDED_EXCEL_ INSTANCE,readOn ly);
}
}

I get an error:
...(360): 'EVA.CExcelData base' 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 promptCredentia ls, Excel.Applicati on excelApp,
bool readOnly):
this(host,user, password, database, promptCredentia ls,
PROVIDED_EXCEL_ INSTANCE,readOn ly)
{
//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 "ExcelApplicati on"
3) finally call the override, which uses the value of ExcelApplicatio n.

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
1919
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 pointer to the child class and call the function on_mdiactivate() using this pointer. For some reason, the program executes MDIChildClass::on_mdiactivate() and not Document::on_mdiactivate(). Why? on_mdiactivate() is a virtual function in...
4
1628
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 header. I want to call the BaseResult class to let it write shared data before I let the derived class write its own data. What am I doing wrong please. public abstract class BaseResult {
20
4259
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 interface and accessing the datastore MUST pass in a UserToken in the constructor of the object. Is this not possible? Am I forced to add the UserToken as a property on the object instead? /Ole
5
3092
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 System.Web.UI.Page. Two questions: 1) I don't know much about C# but I was under the impression that if certain classes and functions were available in a namespace (as the result of a "using" statement at the top of a file), then they were...
5
3434
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 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
12
5812
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. The subclass's constructor sets this variable to a real value. So first the variable is set to 0, the variable is never used before it once again gets set to a real value. It's a waste. Any ideas anyone?
1
1723
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
5641
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 from. A has a pure virtual function virtual in f1() = 0. In B I implement this function and the compiler works out the code. However from A's destructor B can be compiled and then the runtime aborts with saying that a call to a pure virtual...
0
1737
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 works fine when called from a win32 app. The problem I am encountering is that the string being returned to the caller is in Chinese (No, I'm not kidding). (I need it to be in English). Background: The DLL uses 3 pchar parms, 2 in and one out. In...
0
9462
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9287
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10046
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9886
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8723
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7259
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.