?
Solved

Basic inheritance

Posted on 2006-05-18
4
Medium Priority
?
257 Views
Last Modified: 2008-03-10

At the moment i'll use activeReports as an example only.

I have two reports which are identical (apart from the format)

we'll call them report1 and report2, both of which have a base class of ActiveReport

What i want to do is create one instance of Report called rpt and use that everywhere without having to instanciate two different versions.

So i want to do something like:

ActiveReport rpt = null;

if (somecriteria)
{
    rpt = new Report1();
}
else
{
  rpt = new Report2();
}

// set other options and run report

rpt.property1 = "something here";
rpt.property2 = "something else";

rpt.Run();

How do i go about casting it to the appropriate report class?

Does that make sense?
0
Comment
Question by:Type25
  • 2
  • 2
4 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 2000 total points
ID: 16707866
ActiveReport property1, property2 properties and Run method should be virtual. report1 and report2 classem must override them if it is necessary, providing their own functionality. In this case call using base class pointer is redirected to right type.
0
 
LVL 9

Author Comment

by:Type25
ID: 16707868
Can you give an example please
?
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16707930
The following code illustrates this: virtual properties and methods can be called using base class pointer, this is OOP feature called polymorphism.

        class ActiveReport
        {
            public ActiveReport()
            {
            }

            public virtual string Name
            {
                get
                {
                    return "";
                }
                set
                {
                }
            }

            public virtual void Run()
            {
                Console.WriteLine("ActiveReport.Run");
            }
        };

        class Report1 : ActiveReport
        {
            public Report1()
            {
            }

            public override string Name
            {
                get
                {
                    Console.WriteLine("Report1.Name.Get");
                    return "";
                }
                set
                {
                    Console.WriteLine("Report1.Name.Set");
                }
            }

            public override void Run()
            {
                Console.WriteLine("Report1.Run");

                // Optionally:
                base.Run();
            }
        };


        class Report2 : ActiveReport
        {
            public Report2()
            {
            }

            public override string Name
            {
                get
                {
                    Console.WriteLine("Report2.Name.Get");
                    return "";
                }
                set
                {
                    Console.WriteLine("Report2.Name.Set");
                }
            }

            public override void Run()
            {
                Console.WriteLine("Report2.Run");

                // Optionally:
                base.Run();
            }
        };


        static void Main(string[] args)
        {
            ActiveReport rpt1 = new Report1();
            rpt1.Name = "";
            rpt1.Run();

            ActiveReport rpt2 = new Report2();
            rpt2.Name = "";
            rpt2.Run();
        }

Result:
Report1.Name.Set
Report1.Run
ActiveReport.Run
Report2.Name.Set
Report2.Run
ActiveReport.Run

0
 
LVL 9

Author Comment

by:Type25
ID: 16707965
Perfect.

Cheers!
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question