473,769 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting TargetInvocatio nException when calling a virtual function from a constructor

JP
Hi,

I am facing a strange problem, please take a look at the code below:

public SaveTree()
{
........
......
......//some code
Initranges()
}

protected virtual Initranges()
{
........//some code
.......
}

Somewhere else, i have a derived class:

public SaveSingleTree: SaveTree
{
.........
//constructor not being overridden
}

Now, when i do something like this:

SaveTree tree = new SaveSingleTree( )

I get an exception with the following text:
'System.Reflect ion.TargetInvoc ationException' occurred in mscorlib.dl
Exception has been thrown by the target of an invocation.

i have a set of base class and derived class in which a protected
virtual function is being overridden by the derived class. This
function is called internally by the constructor of the base class and
the constructor is NOT being overridden by the derived class.

Anyone have any idea what am i doing wrong? can i invoke a virtual
function from inside a constructor?

Jaspinder

Dec 20 '05 #1
1 1724

JP wrote:
Hi,

I am facing a strange problem, please take a look at the code below:
It really helps us to help you if you post real code. Below is some
real code which is the result of me attempting to deduce what you
meant:

using System;

namespace ConsoleApplicat ion4
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
SaveTree t = new SaveSingleTree( );

Console.ReadLin e();
}
}

public class SaveTree
{
public SaveTree()
{
//some code
Initranges();
}

protected virtual void Initranges()
{
//some code
}
}

public class SaveSingleTree: SaveTree
{
//constructor not being overridden
protected override void Initranges()
{
}
}
}
Now, when i do something like this:

SaveTree tree = new SaveSingleTree( )

I get an exception with the following text:
'System.Reflect ion.TargetInvoc ationException' occurred in mscorlib.dl
Exception has been thrown by the target of an invocation.

i have a set of base class and derived class in which a protected
virtual function is being overridden by the derived class. This
function is called internally by the constructor of the base class and
the constructor is NOT being overridden by the derived class.
The code above matches this description and performs without error or
exception.

Anyone have any idea what am i doing wrong? can i invoke a virtual
function from inside a constructor?


Show us your code.

--
Larry Lard
Replies to group please

Dec 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3696
by: Robert | last post by:
Hello all... In my code below, the Notify Constructor and Destructor is getting called twice and it appears that a new Notify object is created on the 2nd call. The 2nd call is caused by this line below: pNotify = new EMAILnotify; that lives in the Notification Constructor. One theory is that the Notify base class is not completely constructed prior to using it in the Notification Constructor code: pNotify = new EMAILnotify;
31
5195
by: Peter E. Granger | last post by:
I'm fairly new to C++ and VC++, but for the most part it seems to do most of the same things that can be done in Java, with just some syntactic and structural adjustments. However, one thing I haven't been able to figure out is how to call one constructor from another within a class. It's easy enough to call the base class's constructor from the derived class, but that's not what I'm trying to do. For example, in Java (or J#) it's easy...
22
2413
by: ypjofficial | last post by:
Hello All, I have following doubt.. class abstractclass { public: abstractclass(){} virtual void method()=0; };
8
2680
by: Alex Vinokur | last post by:
Microsoft C++ Version 13.10.3077 Why is virtual function init() called in constructor here? ====== foo.cpp ====== #include "iostream" using namespace std; class Base {
16
3757
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
3
4203
by: Biswajit Jena | last post by:
Is it possible to call a member function in constructor of the same class ? how ? I have a templated member function which I want to call in the constructor of the same class. How will I do it ?
17
3547
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;" instead? I think declaring a function as "=0" is the same
4
6415
by: rakesh.usenet | last post by:
For a particular application of mine - I need a simulation of byte array output stream. * write data onto a stream * getback the contiguous content as an array later for network transport. My code looks as follows. #include <iostream>
0
2008
by: aboutjav.com | last post by:
Hi, I need some help. I am getting this error after I complete the asp.net register control and click on the continue button. It crashed when it tries to get it calls this Profile property ((string)(this.GetPropertyValue("Address1")));
0
9423
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
10212
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
10047
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...
1
7410
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
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
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
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3962
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3563
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.