Vishal
Vishal

Reputation: 3296

Basic OOPs related query

I have a basic doubt, I guess something which might be very common while programming in C#.

Basically here is a depiction of how my current code is structured:

File: api.cs

namespace API
{
    class OutputClass{
        private int var1;
        private int var2;
        int getVar1()
        {
            return var1;
        }
        int getVar2()
        {
            return var2;
        }
    }

    class APICore {
        public event Action<OutputClass> FrameDecodeComplete;
        void someFunction() {
            OutputClass myOutput;

            myOutput.var1 = 1;    
            myOutput.var2 = 2;

            FrameDecodeComplete(myOutput);
        }
    }
}

My requirement is that the user of this API creates an object of type APICore, and receives the output in the form of OutputClass object.

Obviously the lines myOutput.var1 = 1; and myOutput.var2 = 2; will throw an error because of it's protection level.

But I also need to ensure that user of the API is unable to set values for var1, var2. So I do not want to change the access level of var1 and var2 to public. And neither do I want to define functions such as setVar1(), setVar2().

Kindly comment.

Upvotes: 0

Views: 97

Answers (5)

Matt Burland
Matt Burland

Reputation: 45145

Give OutputClass a protected constructor that takes parameters to set var1 and var2 then give APICore a private nested class that derives from OutputClass and passes parameters to the protected base constructor.

Something like:

class OutputClass{
    private int var1;
    private int var2;
    int getVar1()
    {
        return var1;
    }
    int getVar2()
    {
        return var2;
    }

    protected OutputClass(int _var1, int _var2)
    {
        var1 = _var1;
        var2 = _var2;
    }
}

class APICore {
    public event Action<OutputClass> FrameDecodeComplete;
    void someFunction() {
        SubOutputClass myOutput = new SubOutputClass(1,2);

        FrameDecodeComplete(myOutput);
    }

    private SubOutputClass : OutputClass
    {
        public SubOutputClass(int var1, int var2) : base(var1, var2)
        {
        }
    }
}

With internal any class in the same assembly will be able to access it. Which may or may not be a problem for you.

Upvotes: 0

If you don't want external assemblies to be able to access your variable, than you can use the internal keyword.

class OutputClass
{
    public int Var1 { get; internal set; }
}


That syntax is a property, which is a syntax that you can use to make getters and setters for your field.

The syntax i showed you is a shorthand for the following

class OutputClass
{    
    private int _var1;
    public int Var1 
    {
        get
        {
            return _var1;
        }
        internal set
        {
            _var1 = value;
        }
    }
}

Upvotes: 3

Tom Bowers
Tom Bowers

Reputation: 5140

If you want users to have access to get properties, but not to set them, then you can create accessor properties like this.

public int var1 { get; private set; }

That way you can modify the variable yourself from within your class, and consumers of the class can access the value, but can't change it.

Upvotes: 1

axblount
axblount

Reputation: 2662

Sounds like their protection level should be internal. That way, var1 and var2 will only be visible inside the assembly they are declared in.

http://msdn.microsoft.com/en-us/library/7c5ka91b%28v=vs.90%29.aspx

Upvotes: 1

Iłya Bursov
Iłya Bursov

Reputation: 24145

check internal keyword, looks like it solve you problem

but you can also add internal constructor method to you OutputClass with 2 arguments, so someFunction will call constructor and return object, in your constructor you will be able to init private variables

Upvotes: 1

Related Questions