Click here to Skip to main content
16,007,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
m wondering how I can call an Interface's Property (that is defined in a class that implements that interface) in the same Interface's method (in the same class of the previous Interface's property)
Here is the code:
C#
namespace interfacetest
{
  public interface IocExt
  {
    void LoadProperties();
    void UpdateProperties();
    string Properties
    { get; }
  }
  public class ocExt : IocExt
  {
    public string colNameGUID;
    void IocExt.LoadProperties()
    {
       colNameGUID = "GUID";
       Messagebox.Show("LoadProperties()- colNameGUID = " + colNameGUID);
    }
    string IocExt.Proeprties
    {
       get{ return colNameGUID; }
    }
    void IocExt.UpdateProperties()
    {
      IocExt ext = new ocExt();
      string s = "UpdateProperties()- colNameGUID = " +   ext.Properties; // here is the problem: 'colNameGUID' value
                                           could not be retrieved from LoadProeprties that is = "GUID" and it just has " "
      Messagebox.Show(s);
    }
  }
  public partial class ocExtImplement : Form
  {
    private void button3_click(object sender, EventArgs e)
    {
      IocExt ioc = new ocExt();
      ioc.LoadProperties();
      ioc.UpdateProperties();
    }
  }
}
Output:
LoadProperties()- colNameGUID = GUID
UpdateProperties() - colNameGUID = .
I'm expecting the second line should also display UpdateProperties() - colNameGUID = GUID. (instead of blank value)
Any ideas of how I can call string IocExt.Proeprties at IocExt.UpdateProperties()?
Thanks in advance!

[edit]Code block added to preserve formatting - OriginalGriff[/edit]
Posted
Updated 16-Sep-10 9:33am
v2

1 solution

See Interface cant be initiated. Its just a rule which defines an object defination.

So once you implement an interface, you will be using the methods and functions the same way as you do with a class without it.

so to get the value of original object use :
<br />
this.Properties


ext is actually a new object created and you didnt set any value for Properties inside UpdateProperties.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900