在C#中实现接口时,如何在同一接口的方法中调用接口的属性 [英] How can I call an Interface's property in the same interface's method while implementing the interface in C#

查看:526
本文介绍了在C#中实现接口时,如何在同一接口的方法中调用接口的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我想知道如何在同一接口的方法(在先前接口属性的同一类中)调用接口的属性(在实现该接口的类中定义)
这是代码:

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:

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();
    }
  }
}

输出:
LoadProperties()-colNameGUID = GUID
UpdateProperties()-colNameGUID =.
我期望第二行也应该显示UpdateProperties()-colNameGUID = GUID. (而不是空白值)
关于如何在IocExt.UpdateProperties()中调用字符串IocExt.Proeprties的任何想法?
提前谢谢!

[edit]添加了代码块以保留格式-OriginalGriff [/edit]

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]

推荐答案

请参见无法启动接口.它只是定义对象定义的规则.

因此,一旦实现了一个接口,您将使用与不使用它的类相同的方法和功能.

因此,要获取原始对象的值,请使用:
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 实际上是创建的新对象,并且您没有在UpdateProperties内部设置属性的任何值.



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


这篇关于在C#中实现接口时,如何在同一接口的方法中调用接口的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆