Reputation: 8336
Found an example and I can't understand what it means.
How can I read from a DataRow or a DataReader using the same code?
I have no idea how to convert the interface and two classes in answer to C#?
Edit: Here is the code that I want to convert to C#:
Interface IIndexer
Default ReadOnly Property Item(ByVal index As String)
End Interface
Class DataReaderWrapper
Implements IIndexer
Private ReadOnly _reader As IDataReader
Public Sub New(reader As IDataReader)
_reader = reader
End Sub
Public ReadOnly Property Item(index As String) As Object Implements IIndexer.Item
Get
Return _reader(index)
End Get
End Property
End Class
Class DataRowWrapper
Implements IIndexer
Private ReadOnly _row As DataRow
Public Sub New(row As DataRow)
_row = row
End Sub
Public ReadOnly Property Item(index As String) As Object Implements IIndexer.Item
Get
Return _row(index)
End Get
End Property
End Class
Upvotes: 0
Views: 1044
Reputation: 3538
There is, as far as I know, no equivalent. More information about the Default keyword can be found at MSDN.
What you are seeing here is an Indexer in an Interface.
The converted code would be something like the following:
interface IIndexer
{
object this[string index] { get; }
}
class DataReaderWrapper : IIndexer
{
private readonly IDataReader _reader;
public DataReaderWrapper(IDataReader reader)
{
_reader = reader;
}
public object this[string index]
{
get { return _reader[index]; }
}
}
class DataRowWrapper : IIndexer
{
private readonly DataRow _row;
public DataRowWrapper(DataRow row)
{
_row = row;
}
public object this[string index]
{
get { return _row[index]; }
}
}
Upvotes: 1
Reputation: 10264
Something like this would work, it's unclear what you mean by 'default property':
var loadedItems = new List<Item>();
SqlDataReader dr = GetItemsDataReader();
while(dr.Read()){
Item item = GetItemFromData(dr);
loadedItems.Add(item);
}
private Item GetItemFromData(SqlDataReader dr){
var loadedItem = new Item();
loadedItem.ID = dr["ID"];
loadedItem.Name = dr["Name"];
'etc., etc.'
Return loadedItem
}
Upvotes: 0