将集合属性与成员对象一起使用 [英] Use collection properties with member object

查看:97
本文介绍了将集合属性与成员对象一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个集合,说 Cells ,如果引用像 Cells [1,1] 给我一个该集合的对象,但成员对象没有该集合对象具有的某个属性。有没有办法从成员调用该属性?如下,假设 StartPosition 是集合的对象类的属性:


Cells [1,1] .StartPosition


或者



<

Cells [1,1] .ParentCollection.StartPosition



解决方案

您只能调用在您正在访问的对象上定义的属性。



也就是说,如果要调用集合上的方法,请在集合上调用,



您可以 可以添加对包含集合的引用到您放置的每个项目,如果您设计



您的符号是数组符号,用于2维数组。虽然数组是集合,但大多数.NET集合不被视为数组,即使它们有 indexers


If I have a collection, say Cells, and if referenced like so Cells[1,1] it gives me an object of that collection but the member object doesn't have a certain property that the collection object has. Is there a way to call that property from the member? Like as follows, assuming StartPosition is a property of the object class for the collection:

Cells[1,1].StartPosition

or maybe

Cells[1,1].ParentCollection.StartPosition

解决方案

You can only call properties that are defined on the object you are accessing.

That is, if you want to call a method on the collection, call it on the collection, not on the content of the collection.

You could add a reference to the containing collection to each item you put in it, if you design and construct your classes that way.

Note:

Your notation is array notation, for 2 dimensional arrays. Though arrays are collections, most .NET collections are not considered to be arrays, even if they do have indexers.

这篇关于将集合属性与成员对象一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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