locked
Methods with same name but different return type RRS feed

  • Question

  • Hi all,

    I have a class structure that looks something like this, I'll use different names to keep it simple:

    virtual class Info, 2 classes that inherit from it called PhoneNumber and Address
    virtual class Book, 2 classes that inherit from it called PhoneBook and AddressBook
    PhoneBook has a PhoneNumber object and AddressBook has an Address object which both share the same name objInfo
    In the Book class, I use a protected Info objInfo and a method GetInfo() that returns objInfo.

    public class Book
    {
        protected Info objInfo;
    
        Info GetInfo()
        {
             return objInfo;
        }
    }
    
    
    
    public class PhoneBook : Book
    {
        protected PhoneNumber objInfo;
    
        PhoneNumber GetInfo()
        {
             return objInfo;
        }
    }
    
    
    public class AddressBook : Book
    {
        protected Address objInfo;
    
        Address GetInfo()
        {
             return objInfo;
        }
    }


    Then in another class I have a method
    void CheckForInfo(Book book)
    {
        book.GetInfo();
    }

    which has to check the objInfo object in the book object which can be either a PhoneBook or an AddressBook.
    The problem is, when I enter that method, GetInfo() returns null because it returns the objInfo object from the Book class and not the objInfo object from the book object which is a PhoneBook for example.
    How can I get book.GetInfo() to return the object from the inherited classes without changing the return type in those??

    Thanks in advance.

    Tuesday, May 26, 2009 7:01 AM

Answers

  • Hello,

    I recommend you to use here generics:

    public
     class
     Book<T> where T:Info
    {
    protected T objInfo;

    T GetInfo()
    {
    return objInfo;
    }
    }
    public
     class
     PhoneBook : Book<PhoneNumber>
    {
    //nothing is needed
    }


    public class AddressBook : Book<Address>
    {
    //nothing is needed
    }

    Vitaliy Liptchinsky http://dotnetframeworkplanet.blogspot.com/
    • Proposed as answer by Abhishek Maitrey Tuesday, May 26, 2009 10:48 AM
    • Marked as answer by Bin-ze Zhao Wednesday, May 27, 2009 8:58 AM
    Tuesday, May 26, 2009 7:31 AM

All replies

  • Hello,

    I recommend you to use here generics:

    public
     class
     Book<T> where T:Info
    {
    protected T objInfo;

    T GetInfo()
    {
    return objInfo;
    }
    }
    public
     class
     PhoneBook : Book<PhoneNumber>
    {
    //nothing is needed
    }


    public class AddressBook : Book<Address>
    {
    //nothing is needed
    }

    Vitaliy Liptchinsky http://dotnetframeworkplanet.blogspot.com/
    • Proposed as answer by Abhishek Maitrey Tuesday, May 26, 2009 10:48 AM
    • Marked as answer by Bin-ze Zhao Wednesday, May 27, 2009 8:58 AM
    Tuesday, May 26, 2009 7:31 AM
  • What you want to do can be achieved with the aid of polymorphism and virtual functions. Just change your code to:
        public class Book
        {
            protected Info objInfo;
    
            protected virtual Info GetInfo()
            {
                return objInfo;
            }
        }
    
    
    
        public class PhoneBook : Book
        {
            protected new PhoneNumber objInfo = 2;
    
            protected override PhoneNumber GetInfo()
            {
                return objInfo;
            }
        }
    
    
        public class AddressBook : Book
        {
            protected new Address objInfo = 3;
    
            protected override Address GetInfo()
            {
                return objInfo;
            }
        }
    note that you have to hide the objInfo object everytime. If you don't want to do that, use private instead of protected. This is the best approach since you don't want to use it in other classes.
    Now you can use an array of Books and fill it with every type that inherits from it. And call the virtual function (GetInfo) and each object will return the appropriate type. You just have to check it.
    I hope it helped.
    CPU: 3.0 GHz Intel HT, RAM: 1GB, OS:Vista, HDD: 250 SATA II, VGA: 128 ATI, Compiler: VS 2005 Ent.
    Tuesday, May 26, 2009 8:57 AM
  • Thank you!!
    That's exactly what I needed.

    So I have to make 2 overloading Check methods then? 1 for each? Or is there a way around that?
    void
     CheckForInfo(Book<PhoneNumber> book)
    {
    book.GetInfo();
    }

    void CheckForInfo(Book<Address> book)
    {
    book.GetInfo();
    }
    Tuesday, May 26, 2009 8:59 AM
  • ...or you can make also generic method within that class

    void CheckForInfo<T>(Book<T> book)
    {
    book.GetInfo();
    }
    Vitaliy Liptchinsky http://dotnetframeworkplanet.blogspot.com/
    Tuesday, May 26, 2009 9:08 AM