2

Im kind of new to the whole C#, but basically im writing a plugin based architecture for an app im working on. Every plugin will need to have some basic things as such I have an interface as follows:

interface IPlugin
{
   string Username {get;}
   string Password {get;}
}

The problem is that the username and password will only be used within the class implementing the interface, as such there is no need to make it public.

So that means I cant use an interface since it is only allowed to be public. I was thinking i could use an abstract class but what is the correct access modifier I would need to put on a class member so that I can implement I can see it when I inherit from the class.

I tried the following but it never worked, and i know why it doesn't, i just don't know what the correct modifier is.

abstract class Plugin
{
  private string Username;
}

class Imp : Plugin
{
  this.Username = "Taylor";
}
Shoe
  • 74,840
  • 36
  • 166
  • 272

6 Answers6

2

Try to use protected modifier, so that fields can be accessible from subclases

abstract class Plugin
{
  protected string Username;
  protected string Password;
}

class Imp : Plugin
{
    public Imp()
    {
        base.Username = "Taylor";
        base.Password = "Pass";
    }
}

You can omit base accesor or use this instead, but I've used to explicitly state what I am changing. It make code a little bit more readable and less ambiguous.

Ilya Ivanov
  • 23,148
  • 4
  • 64
  • 90
  • yes, you can access without `this` and `base` at all, but I want to make explicit that I'm setting a field of a parent class. – Ilya Ivanov Jan 20 '13 at 13:28
  • would it make any difference whether you use this or base, for example if you cast back up to the abstract class, would it be beneficial to use base instead of the this keyword? –  Jan 20 '13 at 13:33
  • No, It doesn't make any difference. The only scenario is when you have another `Username` field in subclass, then when you add `base` modifier - you will access field of a base class, in other case - field of a current class. `if you cast back up to the abstract class` it doesn't matter what type of reference you accessing a class. – Ilya Ivanov Jan 20 '13 at 13:35
1

You are correct in that Interfaces only expose Public methods and properties. You cannot set access modifiers in interfaces.

Given your case, creating an abstract is probably a correct approach. To make a field or property visible only to classes which inherit from your abstract class, you should use the protected access modifier.

For more information: protected access modifier

In your example:

abstract class Plugin
{
   protected string Username;
}

class Imp : Plugin
{
  public Imp()
  {
      this.Username = "Taylor"; // No error here...
  }
}
Blachshma
  • 17,097
  • 4
  • 58
  • 72
1

You are looking for the protected modifier.

manojlds
  • 290,304
  • 63
  • 469
  • 417
1

I think you're looking for the protected keyword, like this:

abstract class Plugin
{
    protected string Username;
}
rankAmateur
  • 2,067
  • 2
  • 18
  • 22
1

The correct modifier is protected. You are right about using an abstract class and not interface in this case - interface is a contract so that the outside world knows some capabilities of the implementors, while abstract class may (and often does) contain some logic and protected members used by that logic.

Honza Brestan
  • 10,637
  • 2
  • 32
  • 43
0

As others said the right approach is to have an abstract class as a base class. This means only your Imp class will be able to access Username. But you can achieve close to that with interfaces, though not exactly.

interface IPlugin
{
    string Username { get; }
}

class Imp : IPlugin
{
    string IPlugin.Username
    {
        get { return "Taylor"; }
    }
}

The key is explicit implementation of interfaces. Now you wont be able to do:

new Imp().Username; //error 

But you will able to do:

((IPlugin)new Imp()).Username; //works

In explicit implementation, Username is public only to the interface instance, not the derived type instance.

As to why private is not allowed, see Non Public Members for C# Interfaces

Community
  • 1
  • 1
nawfal
  • 70,104
  • 56
  • 326
  • 368