471,347 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Getting TargetInvocationException 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.Reflection.TargetInvocationException' 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 1634

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 ConsoleApplication4
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
SaveTree t = new SaveSingleTree();

Console.ReadLine();
}
}

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.Reflection.TargetInvocationException' 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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

31 posts views Thread by Peter E. Granger | last post: by
8 posts views Thread by Alex Vinokur | last post: by
16 posts views Thread by plmanikandan | last post: by
3 posts views Thread by Biswajit Jena | last post: by
4 posts views Thread by rakesh.usenet | last post: by
reply views Thread by Ronak mishra | last post: by

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.