J Buckman

How do I "disable" a method from a base class in a derived class I could throw an exception in the derived class, but that doesn't alert me until run time.

As an example, suppose Class A has three methods X(), Y(), and Z() and then I derive classes B, C, D, and E from A. But, I don't want class D to have access to method Y().

I know I could make a class A1 with only X() and Z() and derive A2 from A1, adding Y(), then derive D from A1, and B, C, and E from A2, but that seems to make the class heirarchy a bit cumbersome.

Thanks in advance,

Jay



Re: Visual C# Language Disabling a method in a derived class

Nimrand

Your only options are the pretty much that ones you mentioned in your post.





Re: Visual C# Language Disabling a method in a derived class

IsshouFuuraibou

the sealed keyword is what you want,

when applied to a class it means the class can not be inherited.

When applied to a method:

public sealed Y(){ ... }

would mean that ALL derived classes would not be able to derive this method.

So you're only way is to inherit by heirarchy:

Class A
// X, Y, Z all public

Class B : A

Class C : A

Class A2 : A
{
// Hide Y from derivers
public sealed new Y()
{
}
}

Class D: A2

Note that due to heirarchy that D is still a derived class from A (just indirectly)





Re: Visual C# Language Disabling a method in a derived class

Billy Burton

I would go for the last approach that you mention (ie. deriving A2 from A1 etc).

While I agree that it may make the class hierarchy seem a bit cumbersome, I think it's more 'correct'. I also think it'll be easier to understand in a couple of months when you need to debug it and you're wondering where Y() has disappeared to when you're using the D class.

Just my thoughts.





Re: Visual C# Language Disabling a method in a derived class

Ja Buckman

Sealed might be the way to go. The methods I want to disable don't really match up with the class hierarchy (at least not yet). I can just define a few extra classes to hide the methods. In this case, A2 is really a D0 or DBase.





Re: Visual C# Language Disabling a method in a derived class

J Buckman

Unless I've done something wrong, Y() is still accessible from an instance of D. What I'm looking for is a way to prevent Y() from being accessible from an instance of D. Looks like I'm back to redefining the classes.





Re: Visual C# Language Disabling a method in a derived class

Sharad Nair

Hi,

I was just wondering if you change the access modifier of function Y() to internal and then define class D in another assembly while keeping the other classes in the same assembly as that of the Class containing the definintion for function Y(). Though this may not be a very efficient solution but might help you achieve what you are looking for.

regards

Sharad






Re: Visual C# Language Disabling a method in a derived class

J Buckman

That might work, but like you say, it would be a bit cumbersome.





Re: Visual C# Language Disabling a method in a derived class

J Buckman

It does seem the simplest way if I can get the hierarchy right. But now I'm wondering if I should be constructing interfaces...





Re: Visual C# Language Disabling a method in a derived class

Nimrand

If Y() might be implemented in classes outside the hierarchy you're working on, interfaces are the way to go. Otherwise, I think using the second suggestion of your original post is the most correct solution.

Sealed does not accomplish what you want. Sealed only prevents a virtual method from being overriden in a derived class. It is still accessible.





Re: Visual C# Language Disabling a method in a derived class

J Buckman

I'll have to agree. I probably just need to spend more time on the design of the classes to get this right. I was just hoping that C# 3.0 had "hide" or "disable" attribute for methods...

Thanks.





Re: Visual C# Language Disabling a method in a derived class

Rudedog2

Wouldn't be possible to declare a an interface containing just a struct, which could then be used as a type declaration in a method. Like this.
Code Block
interface IBInterface : IBInterfaceEnable
{
void BMethodA(IBInterfaceEnable EnableB);
void BMethodB(IBInterfaceEnable EnableB);
new bool BInterfaceEnable
{
get;
}
}

internal interface IBInterfaceEnable
{
bool BInterfaceEnable
{
get;
}
}


protected class ClassB : IBInterface
{


#region IBInterface Members

public void BMethodA(IBInterfaceEnable EnableB)
{
return;
}

public void BMethodB(IBInterfaceEnable EnableB)
{
if (EnableB.ToString() == "true")
{
throw new Exception("This valid method is not yet implemented.");
}
else
{
throw new Exception("This method or operation is invalid.");
}
}
private bool propBInterfaceEnable = true;
public bool BInterfaceEnable
{
get
{
return propBInterfaceEnable;
}
}

#endregion
}


I am sure that someone could come up with a better way of implementing this concept. I'm too new at this.

Rudedog
"Fooling computers since 1971." Stuck in a procedural world.







Re: Visual C# Language Disabling a method in a derived class

J Buckman

Unless I've misunderstood your sample, this would work at run time. I'm looking for a compile time solution.





Re: Visual C# Language Disabling a method in a derived class

Rudedog2

My sample compiles................Place it in a class, not a namespace.