| re: Getting TargetInvocationException when calling a virtual function from a constructor
JP wrote:[color=blue]
> Hi,
>
> I am facing a strange problem, please take a look at the code below:[/color]
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()
{
}
}
}
[color=blue]
> 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.[/color]
The code above matches this description and performs without error or
exception.
[color=blue]
>
> Anyone have any idea what am i doing wrong? can i invoke a virtual
> function from inside a constructor?[/color]
Show us your code.
--
Larry Lard
Replies to group please |