Hell, the title probably wasn’t the best, but I’m a fairly new programmer and haven’t had much experience with inherited classes. I’m trying to initialize a class (my own Stream derived from the normal FileStream class) and have the option of initializing the base from the derived’s arguments. For example…
public class Example : FileStream
{
public Example(FileStream FS) : base = FS
}
Obviously I can’t just do that, but it best shows what I’d like to do. The main reason why I’m doing this is because of contradicting streams — and what I mean by that is that within this class, another class automatically opens the file (and does some reading and whatnot) and I get thrown an exception that the file is inaccessible. Maybe I’m doing this wrong, but thanks for everyone’s time!
You can’t do that, no. But for
Streamspecifically, you can derive fromStream, store theFileStreamin a private field and pass all the method calls to it:If you move the text cursor to the word
Streamafter theExample :, press Alt+Shift+F10 and choose “Implement abstract class Stream”, it will generate all the method declarations for you, but you will still have to change all thethrow new NotImplementedException()into the proper calls to_underlying.