Followers 0

Multiple Inheritence.

6 posts in this topic

What I am trying to accomplish is a form of inheriting multiple classes, I tried interfaces but they are rather limited, then i tried abstract classes. Everything seemed fine but as it turns out (as i forgot) a class can only have 1 base class.
Is there a way to have a bunch of base classes and have them inherit/ be inherited ?
Below is 2 of the class files im trying to inherit.

• interface - FAILED, cant enable external acces (public) for members, cant use virtual, cant specify default values
• class (abstract) - FAILED
• layer inheriting this inherits this which inherits that, i know this is a bad idea and wouldnt work. - NOT A GOOD IDEA , FUNDAMENTALLY FLAWWED

[SPOILER]

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace game

{

public class ILogical

{

public bool ShouldLogic = true;

public void Logic( float mul ) { if ( ShouldLogic ) OnLogic ( mul ); }

public virtual void OnLogic( float mul )

{

}

}

}

[/SPOILER]
[SPOILER]
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace game

{

public class IRenderable

{

public bool ShouldRender = true;

public void Render( ) { if ( ShouldRender ) OnRender ( ); }

public virtual void OnRender( ) { }

}

public class IDrawable : IRenderable

{

public bool ShouldDraw { get { return ShouldRender; } set { ShouldRender = value; } }

public void Draw( ) { Render ( ); }

}

}

[/SPOILER]

Also found this, currently trying to make sense of it.
[url="http://msdn.microsoft.com/en-us/library/64hstbtx.aspx"]http://msdn.microsof...y/64hstbtx.aspx[/url]
0

Share on other sites
In C# you use interfaces instead of inheriting from multiple base classes. What is it that you're trying to do? If you wind up with multiple inheritance in your design, you should try to rethink your design more often than not.
The link you found is for C++, in which multiple inheritance is allowed. It's not allowed in C#. Edited by Knorben
0

Share on other sites
My end goal is being able to inherit varius members from varius files without haveing to retype. Along with being able to use casting to invoke/access. where interfaces fail is the inability to specify values such as "public bool ShouldDoStuff = false;" or even specifying the members to be public/private. I will probably adjust my design model to something that uses interfaces. I'm just wondering if i can still assign values, use virtual, ect ...

Thought it appears not so nuts to me, Time to get complicated or sloppy.
0

Share on other sites
An interface only defines the behavior of a class, not the state. In other words, an interface doesn't (can't) have variables, only methods. It's basically a class consisting of only abstract methods, with the distinction that you can implement several interfaces, where you can only extend one class.
0

Share on other sites
Well this sucks, oh well. Guess i'll specify these things on a per class basis.
Though 1 more thing.

If i have a class with.
public String SomeVariable = "";
and i cast another class that has that property defined to this one.
will i be able to access its value ? (acces classA's version of the value ?)

ClassA : ClassB
0

Share on other sites
If Bar extends Foo, and you have a variable a of type Bar, you can cast it into a Foo type. You need the inheritance, not just the same fields.
My advice is to rethink your design, it sounds as if you could maybe fashion something less complicated.
0

0

Create an account

Register a new account