indexer

Creating a setter method that takes extra arguments in Ruby

半城伤御伤魂 提交于 2019-12-18 05:34:50
问题 I'm trying to write a method that acts as a setter and takes some extra arguments besides the assigned value. Silly example: class WordGenerator def []=(letter, position, allowed) puts "#{letter}#{allowed ? ' now' : ' no longer'} allowed at #{position}" end def allow=(letter, position, allowed) # ... end end Writing it as an indexer works and I can call it like this: gen = WordGenerator.new gen['a', 1] = true # or explicitly: gen.[]=('a', 1, true) But when I try any of the following, the

Using Moq to set indexers in C#

ⅰ亾dé卋堺 提交于 2019-12-18 04:31:45
问题 I'm having trouble figuring out how to set indexers in C# with Moq. The Moq documentation is weak, and I've done a lot of searching... what I'd like to do is similar in the solution to How to Moq Setting an Indexed property: var someClass = new Mock<ISomeClass>(); someClass.SetupSet(o => o.SomeIndexedProperty[3] = 25); I want to modify the above to work for any index and any value so I can just do something like this: someClass.Object.SomeIndexedProperty[1] = 5; Currently I have the following

C# what is the point or benefit of an indexer?

孤街浪徒 提交于 2019-12-18 03:58:12
问题 Doing some code reading and stumbled upon this snippet that I haven't seen before: public SomeClass { public someInterface this[String strParameter] { get { return SomeInternalMethod(strParameter); } } } It looks like it is called as follows: SomeClass _someClass = new SomeClass(); SomeInterface returnedValue = _someClass["someString"]; I am interested in where this function would be appropriate or what the intent of writing in this style. For example why would this be preferred over simply

wpf bind to indexer

情到浓时终转凉″ 提交于 2019-12-17 16:30:26
问题 <TextBlock Text="{Binding Path=[0]} /> or <TextBlock Text="{Binding Path=[myKey]} /> works fine. But is there a way to pass a variable as indexer key? <TextBlock Text="{Binding Path=[{Binding Column.Index}]} /> 回答1: The quickest way to handle this is usually to use a MultiBinding with an IMultiValueConverter that accepts the collection and the index for its bindings: <TextBlock.Text> <MultiBinding Converter="{StaticResource ListIndexToValueConverter}"> <Binding /> <!-- assuming the collection

PropertyChanged for indexer property

瘦欲@ 提交于 2019-12-17 15:32:33
问题 I have a class with an indexer property, with a string key: public class IndexerProvider { public object this[string key] { get { return ... } set { ... } } ... } I bind to an instance of this class in WPF, using indexer notation: <TextBox Text="{Binding [IndexerKeyThingy]}"> That works fine, but I want to raise a PropertyChanged event when one of the indexer values changes. I tried raising it with a property name of "[keyname]" (i.e. including [] around the name of the key), but that doesn't

Using CDT parser out of Eclipse(How to make project?)

亡梦爱人 提交于 2019-12-13 16:45:21
问题 I am trying to parse C++ source by using CDT parser apart from eclipse. To get AST, I have to make, index, IncludeFileContentProvider. To make index, I need to make project. I think this project means eclipse project. But I am using CDT parser outside of eclipse. In this case how to make project. 回答1: Here is an example of CDT parser as you want. import java.util.HashMap; import java.util.Map; import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.IASTDeclaration;

C# Indexer Usage

我怕爱的太早我们不能终老 提交于 2019-12-13 05:08:04
问题 To have an indexer we use the following format: class ClassName { DataType[] ArrayName = new DataType[Length]; public DataType this[int i] { get { return ArrayName[i]; } } } For the sake of simplicity I used the format, even though we can go for a custom indexer also. According to my understanding, we are keeping a propery array that is indexed. My question is : Is it a templated property? When and where could we achieve high degree code optimization using this indexer? 回答1: It is not about

C# extend indexer?

狂风中的少年 提交于 2019-12-06 23:59:52
问题 Is it possible to use extension methods to extend an indexer? I want to be able to get cell's value from DataGridViewCell of given DataGridViewRow using header text like this: object o = row["HeaderText"]; I have this code public static class Ext { public static object Cells(this DataGridViewRow r, string header) { foreach (DataGridViewCell c in r.Cells) { if (c.OwningColumn.HeaderText == header) { return c.Value; } } return null; } } and I want similar indexer. Thanks. 回答1: Indexers are

Using an array's SetValue method vs. the [] indexers

守給你的承諾、 提交于 2019-12-06 20:19:01
问题 I noticed that arrays have the SetValue method, which seems a little out of place when you could just use the indexers. Is there some special purpose for SetValue? The MSDN article didn't seem to say what SetValue was for, just how to use it. Which method would be more efficient to use as far as speed goes? 回答1: Sometimes all you have of an array is that it's an Array . The Array class does not have indexers, so the best way to set/get element values on it is via the GetValue and SetValue

Is Ecplise CDT's indexer limited to the common filetypes for sources and headers?

浪子不回头ぞ 提交于 2019-12-06 11:14:02
问题 I'm working on a project while involves TOM files (.t extension), which get compiled into .c files. Now, I've told my Eclipse to treat them like C sources files, but the CDT indexer doesn't seem to want to touch them. Is it possible to tell it to consider additional filetypes? Note: TOM files look just like C files but with some additional syntax, which would just look like syntax errors on some lines to the indexer. 回答1: The easiest way to do this is define a new association. To do this on