Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 7th, 2005, 03:57 PM
Registered User
 
Join Date: Dec 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jcsdeveloper
Default Most efficient overriding technique

If there is an abstract class that has a virtual method called, for simplicity sake, submit(). It has no parameters at all. But, we need the derived class to implement the functionality (obviously). The derived class needs to take a parameter called orderVO. We have another derived class that takes a parameter of companyVO.

public abstract class parent{

    public virtual void submit(){}
}

public class order : parent{

    public override void submit(orderVO vo){

    //implementation
}

public class company : parent {

    public override void submit(companyVO){
    //implementation
}

Basically, the whole purpose of the parent class is to define the contract that these objects must follow. With its lack of implementation is could also be an interface. I'm trying to write as little conditional/jump through code as possible and this is probably a trivial question.

Thanks in advance,
Jason
 
Old December 7th, 2005, 04:01 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I see no question, but if the question is if you can use an interface instead, I would say yes. And I might add that your child classes would then have to implement the method always, and not as using the abstract approach.

However, the benefit of an interface is also that you can type the object you are using more abstractly (e.g. IMyInterface), and then know the methods which is always implemented, but when you use different signatures for the methods, this will not be a solution.

I think, that if I were you I would make a more abstract argument to the submit method, e.g. a Hashtable. Then if the Submit method do not need arguments you can just set it to null. Another approach would be to pass the nessesary arguments to global variables before calling the argument-less Submit method.

Jacob.
 
Old December 7th, 2005, 04:06 PM
Registered User
 
Join Date: Dec 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jcsdeveloper
Default

I would also like to add that I don't want to create an interface type for the 2 vo classes to implement and then just use that in all three of the method declarations above:

parent
    submit(vo)
order : parent
    submit(vo)
company : parent
    submit(vo)

I'm also seking the most efficient way.

Any feedback would be greatly appreciated.

Thanks again,
Jason
 
Old December 7th, 2005, 04:10 PM
Registered User
 
Join Date: Dec 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jcsdeveloper
Default

I'm sorry. Visual Studio is telling me I cannot override because my derived classes do not have identical declarations as the parent class. There is no parameter in the parent class but I want them in the derived classes. The method names on the other hand are the same.

So my question is what is the best way to mimic this behavior without writing too much conditional code and/or ctyping?

Thanks,
Jason
 
Old December 7th, 2005, 04:18 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, in code I would probably do something like this ...
Code:
public abstract class Parent
{
    public virtual void Submit(Hashtable properties)
    {
        throw new Exception("Submit not implemented.");
    }
}

public class Child : Parent
{
    public override void Submit(Hashtable properties)
    {
        // Implementation.
    }
}
But the developer who should develop the child class are not forced to implement the Submit method. Therefore, you should/could do it the interface way if that is your approach, or I guess simply make the method abstract.

Hope it helps, Jacob.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Swiping card technique?? rajanikrishna Pro VB Databases 1 December 18th, 2003 07:36 AM
The Technique for Scrolling Without Break [email protected] Flash (all versions) 0 August 11th, 2003 06:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.