Indexer

D

DaveL

Hello
I want to Iherit DataTable

Can you show or point me to any aritcles
to Write the indexer as follows
example:

for (int x=0;x<odatatable.rows.Count;x++)
{
Console.WriteLine(oDataTable.Rows[x]["ItemName"]
}
i do not know how to write The above indexer

Thanks
DaveL
 
A

Anthony Jones

DaveL said:
Hello
I want to Iherit DataTable

Can you show or point me to any aritcles
to Write the indexer as follows
example:

for (int x=0;x<odatatable.rows.Count;x++)
{
Console.WriteLine(oDataTable.Rows[x]["ItemName"]
}
i do not know how to write The above indexer

Since DataTable already has that ability its not clear why you want to add
it.

To start with the Rows member on DataTable returns a DataRowCollection
which is a sealed class. So your new Rows method would have to return
something else. Lets call it MyRowCollection which delegates to
DataRowCollection:-

class MyRowCollection
{
DataRowCollection _drc
internal MyRowCollection(DataRowCollection drc) { _drc = drc; }
public DataRow this[int index]
{
get
{
//Your other stuff
return _drc[index];
}
}
}

Note that an indexer has the same basic syntax as a property except instead
of simple the property name there is the keyword 'this' followed by the
parameters that form the indexer e.g., this[string name] .

As with methods you can create various overloads for the indexer using
different types of and numbers of parameters.

That said I really think you should reconsider this. If you are using C#3
consider some Extension methods.
 
J

Jeff Johnson

I want to Iherit DataTable

Can you show or point me to any aritcles
to Write the indexer as follows
example:

for (int x=0;x<odatatable.rows.Count;x++)
{
Console.WriteLine(oDataTable.Rows[x]["ItemName"]
}
i do not know how to write The above indexer

There are two indexers involved. The Rows property returns a collection
which has an indexer. It returns a DataRow. The DataRow also has an indexer
which returns the value of one of its columns. What I don't understand is
this: doesn't this stuff just automatically work in your derived class?
 
D

DaveL

It Does Work in the Derived class
but as you see in my example

[irow][sColumnName]

i dont know how to implement the above
[iRow] indexer no problem

I need to work on the data b4 i return it back to the business logic code

public DataRow this[iRow]
{
get{}
set{}
}
What would the indexer for the column part code like
public object

Tks Dave

Jeff Johnson said:
I want to Iherit DataTable

Can you show or point me to any aritcles
to Write the indexer as follows
example:

for (int x=0;x<odatatable.rows.Count;x++)
{
Console.WriteLine(oDataTable.Rows[x]["ItemName"]
}
i do not know how to write The above indexer

There are two indexers involved. The Rows property returns a collection
which has an indexer. It returns a DataRow. The DataRow also has an
indexer which returns the value of one of its columns. What I don't
understand is this: doesn't this stuff just automatically work in your
derived class?
 
P

Pavel Minaev

It Does Work in the Derived class
but as you see in my example

[irow][sColumnName]

i dont know how to implement the above
[iRow] indexer no problem

I need to work on the data b4 i return it back to the business logic code

public DataRow  this[iRow]
{
       get{}
      set{}}

What would the indexer for the column part code like
public object

The column indexer is the indexer for the DataRow class, not for the
DataTable class. So you'd need to derive your own class from DataRow,
override its indexer to do what you want it to do, and return an
instance of that class from DataTable indexer.

Note that, since neither of those indexers is virtual, you cannot
properly override it, so the construct is extremely brittle - anyone
using your DataTable/DataRow through a reference of base type would
use the default implementations, circumventing any of your custom
logic. You might want to ask yourself whether it is a good idea at
all.
 
J

Jeff Johnson

It Does Work in the Derived class
but as you see in my example

[irow][sColumnName]

i dont know how to implement the above
[iRow] indexer no problem

I need to work on the data b4 i return it back to the business logic code

public DataRow this[iRow]
{
get{}
set{}
}
What would the indexer for the column part code like

I could be wrong, but I think you're missing the fact that the indexers
above are a sort of "shorthand" syntax and you don't realize that there's is
actually another object "between" the indexers.

Consider this statement:

string FName = MyDataTable[12]["FirstName"].ToString();

What's really happening is something like this:

string FName = MyDataTable.Item[12].Item["FirstName"].ToString();

The first indexer ( [12] ) is called against the DataTable and returns a
DataRow object. The second indexer ( ["FirstName"] ) is called against the
DataRow that was retrieved by the first indexer.
 
D

DaveL

Ive Decided its a Bad Idea....
i still want to write this code....just for my experience if anything else

What i basically wanted to to is deal with dbNulls...
then i decided....to Place that logic in My Custom Readers

that is a Much better Fit

thank you for all your help and information
Dave


It Does Work in the Derived class
but as you see in my example

[irow][sColumnName]

i dont know how to implement the above
[iRow] indexer no problem

I need to work on the data b4 i return it back to the business logic code

public DataRow this[iRow]
{
get{}
set{}}

What would the indexer for the column part code like
public object

The column indexer is the indexer for the DataRow class, not for the
DataTable class. So you'd need to derive your own class from DataRow,
override its indexer to do what you want it to do, and return an
instance of that class from DataTable indexer.

Note that, since neither of those indexers is virtual, you cannot
properly override it, so the construct is extremely brittle - anyone
using your DataTable/DataRow through a reference of base type would
use the default implementations, circumventing any of your custom
logic. You might want to ask yourself whether it is a good idea at
all.
 
D

DaveL

I Completly understand there is a reference from indexer 1 of datarow for
indexer 2

I just am trying to write it..and i am missing somthing somewhere

dave

Jeff Johnson said:
It Does Work in the Derived class
but as you see in my example

[irow][sColumnName]

i dont know how to implement the above
[iRow] indexer no problem

I need to work on the data b4 i return it back to the business logic code

public DataRow this[iRow]
{
get{}
set{}
}
What would the indexer for the column part code like

I could be wrong, but I think you're missing the fact that the indexers
above are a sort of "shorthand" syntax and you don't realize that there's
is actually another object "between" the indexers.

Consider this statement:

string FName = MyDataTable[12]["FirstName"].ToString();

What's really happening is something like this:

string FName = MyDataTable.Item[12].Item["FirstName"].ToString();

The first indexer ( [12] ) is called against the DataTable and returns a
DataRow object. The second indexer ( ["FirstName"] ) is called against the
DataRow that was retrieved by the first indexer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top