Hi,

A bit of a tricky question, so I will try to explain everything in its fullest.

I am trying to replicate some of the Terrarium work done by the Microsoft guys, but I dont want to look at any of their code.
Anyway I have my base class which is an organism. This has a number of variables and methods. These methods/variables fall into a number of categories:
A) Only base class can access it (private)
B) Everything can access it (public)
C) (the interesting one), methods that only the "engine" can access and not any classes that inherit off the base.

The reason behind this is that the base class contains functionality for resolving fights, exchanging energy, scanning for enemies etc, aging the organism. The engine needs access to the aging method specifically, but I dont want any inherited classes to access the same function - as the "child" classes will be written by a third party.

Does that all make sense?

My question is, how can I define a method/variable in the base class such that it is available externally, but not to any classes that inherit from the base.

Thanks in advance

Stephen

Provided the base class and the engine are in the same library you can use a shared interface that is private to your code only.
When implementing the interface on the base class ensure that you only do this explicitly and not implicitly. You will need to cast base class instances to the interface to use it but it should keep the methods private.

In your library

namespace ClassLibrary1
{
    interface EngineInterface
    {
        void InterfaceMethod();
    }

    public class MyBaseClass : EngineInterface
    {
        private int myVar1;

        public int Prop1
        {
            get { return myVar1; }
            set { myVar1 = value; }
        }

        void EngineInterface.InterfaceMethod()
        {
            // do stuff
        }
    }
    
    public static class EngineClass
    {
        public static void EngineMethod(MyBaseClass c1)
        {
            ((EngineInterface)c1).InterfaceMethod();
        }
    }
}

When base class is used out side you library

namespace WindowsApplication1
{
    class Class6 : ClassLibrary1.MyBaseClass
    {
        public void TestMethod()
        {
            ClassLibrary1.EngineClass.EngineMethod(this); // OK
            ((ClassLibrary1.EngineInterface)this).InterfaceMethod(); // Error: ClassLibrary1.EngineInterface is inaccessible dut to its protection level
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.