tags:

views:

125

answers:

4

i have interface 'IResourcePolicy' contain property Version i have to implemet this property which contain value ,,means in other page i did this type of coding

IResourcePolicy irp(instantiated interface)
irp.WrmVersion = "10.4";


Here how can i implement property 'version'

  public interface IResourcePolicy
   {
   string Version
      {
          get;
          set;
      }
}
+3  A: 

Interfaces can not contain any implementation (including default values). You need to switch to abstract class.

Vitaliy Liptchinsky
Thats what how can i implement this property somewhere else
peter
create a class @petr that implements that interface,than create instance to that class and assign to variable which have interface type
ArsenMkrt
means can you show me the code
peter
I would vote for the first sentence. But abstract base classes should always be avoided if an interface is enough. So before you know what problem should actually be solve, you shouldn't suggest to create a base class.
Stefan Steinegger
@Stefan, by second sentence I meant that if he really needs a default value to be introduced, then he should use abstract class.
Vitaliy Liptchinsky
+2  A: 

You mean like this?

class MyResourcePolicy : IResourcePolicy {
    private string version;

    public string Version {
        get {
            return this.version;
        }
        set {
            this.version = value;
        }
    }
}
J. Random Coder
this i knows ,,but i already assigned values such that irp.WrmVersion = "10.4"; see my question,,i dont need to lose that value ,,i have to pass it
peter
+7  A: 

In the interface, you specify the property:

public interface IResourcePolicy
{
   string Version { get; set; }
}

In the implementing class, you need to implement it:

public class ResourcePolicy : IResourcePolicy
{
   string Version { get; set; }
}

This looks similar, but it is something completely different. In the interface, there is no code, you just specify the there is a property with a getter and a setter, whatever they will do.

In the class, you actually implement them. The shortest way to do this is using this { get; set; } syntax, the compiler will create a field and generate the getter and setter implementation for it.

Stefan Steinegger
+1  A: 
  • but i already assigned values such that irp.WrmVersion = "10.4";

J.Random Coder's answer and initialize version field.


private string version = "10.4';
kazuk