0

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

Lance Roberts
  • 22,383
  • 32
  • 112
  • 130

2 Answers2

1

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.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Yeh, I'm trying to help someone with [GemBox.Spreadsheet](http://www.gemboxsoftware.com/Spreadsheet/Help/index.aspx), you can see it in the `ExcelWorksheet.Cells Property` page, the line says `Gets CellRange with all the cells (ExcelCell) in the worksheet.` I was just calling it a collection because that's what it seemed like. – Lance Roberts Aug 03 '11 at 06:17
  • @Lance - I see. I would say that if they defined access to the parent collection from each cell, you would access it and call a method on the reference. – Oded Aug 03 '11 at 06:19
  • See [question here](http://stackoverflow.com/questions/6920677/gembox-get-address-of-excelcell). – Lance Roberts Aug 03 '11 at 06:19
  • yeh, didn't see that. I was just hoping there was some tricky C# way to make it happen. – Lance Roberts Aug 03 '11 at 06:20
  • @Lance - Not without a reference to the collection (which you have, since you are using indexer access on it). – Oded Aug 03 '11 at 06:20
  • hmmm, so what would be the syntax to reference the collection property, using the indexed object? – Lance Roberts Aug 03 '11 at 06:22
  • @Lance - You are using `Cells[1,1]` - you can access `Cells` methods directly. – Oded Aug 03 '11 at 06:23
  • Having not much C# experience, my best guess was the first one in my question. I guess it's even more complicated because I want to do it after the member object has been defined, off of that member object. I'm guessing there is no mechanism for this, so I'll just accept and let the other gentleman know. – Lance Roberts Aug 03 '11 at 06:29
0

You could either wrap that in a propert of the Cell or what you are returning. So you'd add this to the Cell class:

public int StartPosition { 
    get { return this.ParentCollection.StartPosition; } 
}

If you can't change the class you can add an extension method, e.g.:

public static class CellExtensions {
 public static int GetStartPosition(this Cell cell) {
   return cell.ParentCollection.StartPosition;
 }
}
Anže Vodovnik
  • 2,325
  • 16
  • 25
  • Where is `ParentCollection` defined? It is not something that will appear automatically. – Oded Aug 03 '11 at 06:06
  • Again, it depends on the collection and what you control. If you "own" the collection implementation then it's something you can set for the Cell classes, that you should also own. If they are not your code, I'm afraid there isn't much you can do (out of the box). I'd set the ParentCollection when .Add is called in my collection. – Anže Vodovnik Aug 03 '11 at 06:09
  • Thanks, but my problem is in a library, so no mods. – Lance Roberts Aug 03 '11 at 06:15
  • In that case, like I mentioned, Extension methods are one possible answer. If you haven't heard of them, read http://msdn.microsoft.com/en-us/library/bb383977.aspx . It should give you an idea of what I meant with the second part of my answer. – Anže Vodovnik Aug 03 '11 at 06:28