使用有关扩展项目的重新定义列表中缺少对象引用
大家好,
我正在尝试开设一个项目管理课程。 为了查看数据中的内容是否发生变化,我想在编程结构的较低级别上实现事件。我有一些扩展 ProjectComponent 类的类。基类有一个事件和事件抛出方法,子组件可以使用它们。 现在我在项目对象中有几个自定义列表(nameley eList)。 因为所有子组件都有一个共同的父组件 ProjectComponent,所以我希望自定义列表对象 (eList) 在添加对象时订阅该事件,并在删除对象时取消订阅。
但是,当尝试对此进行编程时,我收到以下错误:
“ProjectComponent”不包含 'itemChanged' 的定义,没有 扩展方法“itemChanged” 接受类型的第一个参数 '项目组件'
这有点奇怪,因为该类显然具有公共字段。
下面是代码:
public class ProjectComponent
{
public event ItemChanged itemChanged;
public void throwItemChangedEvent(ItemChangedEventArgs Arguments)
{
if (itemChanged != null)
itemChanged(new Object(), Arguments);
}
}
public class eList<ProjectComponent> : IList<ProjectComponent>
{
List<ProjectComponent> internalList = new List<ProjectComponent>();
public override void Add(ProjectComponent Item)
{
this.internalList.Add(Item);
Item.itemChanged += new ItemChanged(ItemChanged_Handler);
}
private void ItemChanged_Handler(object sender, ItemChangedEventArgs eventArgs)
{
//do stuff here
}
}
调用方式的示例如下:
public eList<ChildClass> Children = new eList<ChildClass>();
这个想法是,当编辑列表中的对象时,列表对象会收到如下所示的对象:
Children.childstring = "anything";
当 Children 对象内的字段发生更改时,事件可以被接待。
我的问题只是我做错了什么,为什么我不能订阅 eList 类中的 ProjectComponent 事件? 或者有谁知道更好的方法来达到相同的结果?
提前致谢,
Harry
编辑:ItemChanged delagate 的定义:
public delegate void ItemChanged(object sender, ItemChangedEventArgs eventArgs);
public class ItemChangedEventArgs : EventArgs
{
private String p_CallStack;
public String CallStack
{
get { return this.p_CallStack; }
set { this.p_CallStack = value; }
}
public ItemChangedEventArgs()
{
p_CallStack = "";
}
public ItemChangedEventArgs(String StackStart)
{
p_CallStack = StackStart;
}
}
He everybody,
I'm trying to setup a project management class.
In order to see if somthing in the data changed i want to implement events on the lower level of the programming structure. I have some Classes extending the ProjectComponent Class. The base class has an event and event throwing methode, which the childcomponents can use.
Now I have a couple of custom list (nameley eList) in the project object.
Because all the child component have a common parent, ProjectComponent, i would like my custom list object (eList) to subscribe to the event when an object is added and unsubscribe when removed.
However when trying to prog this, i received the following error:
'ProjectComponent' does not contain a
definition for 'itemChanged' and no
extension method 'itemChanged'
accepting a first argument of type
'ProjectComponent'
Which is kind of wierd seeing as the class clearly has that public field.
Here is a the code:
public class ProjectComponent
{
public event ItemChanged itemChanged;
public void throwItemChangedEvent(ItemChangedEventArgs Arguments)
{
if (itemChanged != null)
itemChanged(new Object(), Arguments);
}
}
public class eList<ProjectComponent> : IList<ProjectComponent>
{
List<ProjectComponent> internalList = new List<ProjectComponent>();
public override void Add(ProjectComponent Item)
{
this.internalList.Add(Item);
Item.itemChanged += new ItemChanged(ItemChanged_Handler);
}
private void ItemChanged_Handler(object sender, ItemChangedEventArgs eventArgs)
{
//do stuff here
}
}
An example how it would be called is:
public eList<ChildClass> Children = new eList<ChildClass>();
The idea is that when an object in the list is edited the list object recieve an object like so:
Children.childstring = "anything";
At the moment the field inside the Children object is changed an event could be recieved.
My question is simply what am i doing wrong, why cant i suscribe to the ProjectComponent event inside the eList class?
Or does anyone know a better way to achive the same results?
Thanks in Advance,
Harry
Edit: Definition of ItemChanged delagate:
public delegate void ItemChanged(object sender, ItemChangedEventArgs eventArgs);
public class ItemChangedEventArgs : EventArgs
{
private String p_CallStack;
public String CallStack
{
get { return this.p_CallStack; }
set { this.p_CallStack = value; }
}
public ItemChangedEventArgs()
{
p_CallStack = "";
}
public ItemChangedEventArgs(String StackStart)
{
p_CallStack = StackStart;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有 2 个错误:
1.
在 通用类< /a> 定义时必须使用非现有类的变量:
public class eList: ...
-->
public class eList: ...
在您想要执行的情况下:
2.
new Item.itemChanged
没有意义,您必须使用事件的底层委托类型:注意:
您的代码根本不遵守 c# 的设计准则
更多信息请参见:命名指南
you have 2 errors:
1.
in generic class definition you must use variables not existing classes:
public class eList<ProjectComponent>: ...
-->
public class eList<T>: ...
in your case you want to do:
2.
new Item.itemChanged
has no meaning, you have to use the underlying delegate type of your event:N.B:
your code does not respect at all design guidelines for c#
More informations here:Naming Guidelines
难道不应该吗
Shouldn't it be