[.net] [C#] How to force definition of a member in inherited class

Started by
8 comments, last by g0nzo 17 years, 4 months ago
Hi, I've got an abstract class that has "String effectFilename = null" member. Later in the abstract class I've got a virtual function that loads an Effect using this filename. I'd like to somehow force a definition of this member in all inherited classes, so I can be sure that it will be defined when loading the resource. Is it possible to do it, besides passing it as an argument in the constructor? I'm looking for something like abstract method functionality for a member.
Advertisement
The since the member is defined in the base class, it will be existant in the derived classes.

Did you mean you want to ensure the member has a non-null (and perhaps non-empty) value by the time it is use? A constructor in the base class that accepts a string and sets the filename member is the best option.
Thank you.

Quote:
Did you mean you want to ensure the member has a non-null (and perhaps non-empty) value by the time it is use?

Exactly [smile]

Quote:
A constructor in the base class that accepts a string and sets the filename member is the best option.

But is it possible to do it in some other way? Is it possible to have abstract method functionality for a member? So I only declare a member in a base class and if programmer doesn't provide definition for it in the inherited class, he gets a compiler error? This member will behave like static (there'll be only one instance of the class at a time) and constant (but different for every inherited class), so I was wondering if there's a shorter method, than writing it every time I'm creating a new object.

I'll pass it to the contructor, I was just curious if there's another way to do it.

Thanks again.
Quote:Original post by g0nzo
Is it possible to have abstract method functionality for a member? So I only declare a member in a base class and if programmer doesn't provide definition for it in the inherited class, he gets a compiler error?
You could use an abstract property.

public abstract string EffectFilename {    get; // optional    set; // optional}

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

Thanks! Exactly what I was looking for!
Yeah, abstract properties are the way to go. I think for this example you just need the read access, i.e. abstract String EffectFilename { get; }. That won't stop the implementer giving it a null value though. As far as I know there is no way to prevent that.

You could also (although this is officially bad style) make subclasses able to see the member:
protected String effectFilename


(edit: tags)
Quote:Original post by g0nzo
Thank you.

Quote:
Did you mean you want to ensure the member has a non-null (and perhaps non-empty) value by the time it is use?

Exactly [smile]

Quote:
A constructor in the base class that accepts a string and sets the filename member is the best option.

But is it possible to do it in some other way? Is it possible to have abstract method functionality for a member? So I only declare a member in a base class and if programmer doesn't provide definition for it in the inherited class, he gets a compiler error? This member will behave like static (there'll be only one instance of the class at a time) and constant (but different for every inherited class), so I was wondering if there's a shorter method, than writing it every time I'm creating a new object.

I'll pass it to the contructor, I was just curious if there's another way to do it.

Thanks again.


It sounds like what you want is a constructor in the base class which takes a string as an argument. You don't need to write it every time you're creating a new object. It's easier to explain with an example, I think:

abstract class Base{    protected Base(string fileName)     {          if (string.IsNullOrEmpty(fileName)) throw new NullReferenceException();         effectFilename = filename;     }}class DerivedA : Base{    public DerivedA() : base("myfileA.txt") {}}class DerivedB : Base{    public DerivedB() : base("myfileB.txt") {}}


When you're creating DerivedA or B you don't specify the name of the file, basically a constant "myfileA.txt" is passed into the base class's constructor.
So it will give a compiler error if a string is not passed to the contructor and if the string is null/empty will throw an exception?

Quote:Original post by g0nzo
Thanks! Exactly what I was looking for!

[smile]

BTW. Is there any reason why IsNullOrEmpty is not an instance method? I had no idea that it exists.
If you have a null reference, there's no instance to call an instance method for :)
Yeah, that could be it :)

I forgot that C# is not Ruby, where there's a NilClass, so you can do something like this:
a = "string"a.class  => Stringa = nila.nil?   => truea.class  => NilClass

Anyway, thanks again for helping me to solve my problem.

This topic is closed to new replies.

Advertisement