Sign in to follow this  
g0nzo

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

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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
}

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 => String
a = nil
a.nil? => true
a.class => NilClass

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this