在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
.