Click here to Skip to main content
15,891,943 members
Home / Discussions / C#
   

C#

 
GeneralRe: Override virtual or abstracts with derived return types Pin
jofli11-Dec-08 22:33
jofli11-Dec-08 22:33 
AnswerRe: Override virtual or abstracts with derived return types Pin
J4amieC11-Dec-08 22:19
J4amieC11-Dec-08 22:19 
GeneralRe: Override virtual or abstracts with derived return types Pin
jofli11-Dec-08 22:27
jofli11-Dec-08 22:27 
GeneralRe: Override virtual or abstracts with derived return types Pin
J4amieC11-Dec-08 22:40
J4amieC11-Dec-08 22:40 
GeneralRe: Override virtual or abstracts with derived return types Pin
jofli11-Dec-08 23:09
jofli11-Dec-08 23:09 
AnswerRe: Override virtual or abstracts with derived return types Pin
J4amieC11-Dec-08 22:46
J4amieC11-Dec-08 22:46 
GeneralRe: Override virtual or abstracts with derived return types Pin
carbon_golem12-Dec-08 2:55
carbon_golem12-Dec-08 2:55 
GeneralRe: Override virtual or abstracts with derived return types Pin
Ben Fair12-Dec-08 11:00
Ben Fair12-Dec-08 11:00 
The easiest way to deal with this is to keep the type declaration of the base class property and cast to the actual type when working with the derived class. You can also consider whether you're going to need any of the functionality specific to FileStream. If Stream has all the functionality you need, then don't even worry about casting. I believe you may be able to do this with an Interface if you explicityly implement the interface item. Here's an example that works, but there's some trickery with an interface where you can have an explicit implementation of the interface defined that accesses the base class property and then a normal property of the same name that provides a different return type. Note that I had to implement the interface in both classes and also in Class_b I had to specifically cast to and from the different stream types. This is because even though Class_a implements the interface, I want to provide an explicit implementation in Class_b, so I have to implement it also in Class_b to do this. When you run this, Class_b.Property returns type FileStream while Class_a.Property returns type Stream.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace TestClass_a
{
    public interface IStream
    {
        Stream Property { get; set; }
    }

    public class Class_a : IStream
    {
        private Stream _stream = null;

        public Stream Property
        {
            get { return _stream; }
            set { _stream = value; }
        }
    }

    public class Class_b : Class_a, IStream
    {
        public FileStream Property
        {
            get { return (FileStream)base.Property; }
            set { base.Property = (Stream)value; }
        }

        #region IStream Members

        Stream IStream.Property
        {
            get { return base.Property; }
            set { base.Property = value; }
        }

        #endregion
    }

    class Program
    {
        static void Main(string[] args)
        {
            Class_a a = new Class_a();
            a.Property = new MemoryStream();
            Class_b b = new Class_b();
            b.Property = File.Open("C:\\test.txt", FileMode.OpenOrCreate);
            Class_a b2 = b;
            bool canRead = b2.Property.CanRead;
        }
    }
}


Keep It Simple Stupid! (KISS)

GeneralRe: Override virtual or abstracts with derived return types Pin
jofli15-Dec-08 4:51
jofli15-Dec-08 4:51 
QuestionFormat a segement of a Excel cell bold programmatically Pin
skyline9211-Dec-08 21:33
skyline9211-Dec-08 21:33 
AnswerRe: Format a segement of a Excel cell bold programmatically Pin
Mycroft Holmes11-Dec-08 21:44
professionalMycroft Holmes11-Dec-08 21:44 
GeneralRe: Format a segement of a Excel cell bold programmatically Pin
skyline9212-Dec-08 0:47
skyline9212-Dec-08 0:47 
QuestionUnicode font sample application Pin
Sameera Afzal11-Dec-08 20:41
Sameera Afzal11-Dec-08 20:41 
AnswerRe: Unicode font sample application Pin
Lev Danielyan11-Dec-08 20:49
Lev Danielyan11-Dec-08 20:49 
AnswerRe: Unicode font sample application Pin
leppie11-Dec-08 20:52
leppie11-Dec-08 20:52 
GeneralRe: Unicode font sample application Pin
Lev Danielyan11-Dec-08 20:58
Lev Danielyan11-Dec-08 20:58 
QuestionResponse file in C# Pin
Sinol Jose11-Dec-08 19:29
Sinol Jose11-Dec-08 19:29 
AnswerRe: Response file in C# Pin
N a v a n e e t h11-Dec-08 19:40
N a v a n e e t h11-Dec-08 19:40 
GeneralRe: Response file in C# Pin
TJS4u11-Dec-08 19:45
TJS4u11-Dec-08 19:45 
GeneralRe: Response file in C# Pin
Sinol Jose11-Dec-08 19:47
Sinol Jose11-Dec-08 19:47 
GeneralRe: Response file in C# Pin
Mycroft Holmes11-Dec-08 20:08
professionalMycroft Holmes11-Dec-08 20:08 
GeneralRe: Response file in C# Pin
Sinol Jose11-Dec-08 20:16
Sinol Jose11-Dec-08 20:16 
GeneralRe: Response file in C# Pin
Mycroft Holmes11-Dec-08 20:32
professionalMycroft Holmes11-Dec-08 20:32 
GeneralRe: Response file in C# Pin
J4amieC11-Dec-08 22:35
J4amieC11-Dec-08 22:35 
AnswerRe: Response file in C# Pin
Mycroft Holmes11-Dec-08 20:06
professionalMycroft Holmes11-Dec-08 20:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.