1

I would like to create a custom datatable class in which the datarow class property "Item" is overloaded so that it returns a trimmed value.

How do I start? Maybe an example?

Class MyDatatable
Inherits DataTable

Public Overloads Property Item(ByVal columnIndex As Integer) As Object
Get

End Get
Set(value As Object)

End Set
End Property

End Class

Thx!

3
  • Create a new class that inherits DataTable and contains an Item property. Commented Jul 19, 2012 at 6:53
  • This is what I have so far... (not much!) See Q Commented Jul 19, 2012 at 10:03
  • I don't know enought about DataTable to help you directly. But maybe you'll have to loop all items and trim them, or have the database query return a trimmed version of the string. Commented Jul 19, 2012 at 15:19

1 Answer 1

1

There isn't much to overload since the DataTable doesn't have an Item property. What you really want to do is inherit from DataRow, but that doesn't work because it has a required initializer (DataRowBuilder) that is inaccessible.

Maybe something like this:

Public Class MyDataTable
  Inherits DataTable

  Public Function Item(ByVal rowIndex As Integer, _
                       ByVal columnIndex As Integer) As Object
    Return MyBase.Rows(rowIndex)(columnIndex).ToString().Trim
  End Function

End Class

It's missing some obvious error checking. Not very practical either, since a cell could hold a lot more types than just strings.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi LarsTech, thanks for this. The datatype in the cells will always be returned as string (IBM iSeries AS400) so not very error prone. Because the DataRowBuilder is inaccessable preventing to initialize DataRow class I think I'll have to mark your answer as definitive. =) grtz

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.