Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / December 2005

Tip: Looking for answers? Try searching our database.

Getting TargetInvocationException when calling a virtual function from a constructor

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JP - 20 Dec 2005 10:15 GMT
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
Larry Lard - 20 Dec 2005 10:42 GMT
> 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:
>
[quoted text clipped - 8 lines]
> 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.

Signature

Larry Lard
Replies to group please


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.