J
Joel Reinford
I would like to build a class that has properties which can be accessed by
string names or index numbers in the form of MyClass.Item("LastName"). The
string names or item index values would be populated by a data-driven loop.
I need a few pointers to get me started in the right direction. I'm pretty
sure that I need a default Item property but I'm not sure how to create that
or index the other properties. A short code example is below
Joel Reinford
Assume a DataTable with four columns: FirstName, LastName, Birthdate, Gender
Function CreatePerson(dt as DataTable) as Person
Dim p as New Person
Dim dc As DataColumn
Dim drw As DataRow = dt.Rows(0)
For Each dc In dt.Columns
'this is what I want to achieve, it would translate into
p.FirstName, p.LastName, etc.
p.Item(dc.ColumnName) = drw(dc.ColumnName)
Next
End Function
Here is what a sample class looks like now
Public Class Person
Private _FirstName As String
Private _LastName As String
Private _LastName As String
Private _Gender As String
Private _Birthdate As Date
Property FirstName() As String
Get
Return _FirstName
End Get
Set(ByVal Value As String)
_FirstName = Value
End Set
End Property
Property LastName() As String
Get
Return _LastName
End Get
Set(ByVal Value As String)
_LastName = Value
End Set
End Property
Property Gender() As String
Get
Return _Gender
End Get
Set(ByVal Value As String)
_Gender = Value
End Set
End Property
Property Birthdate() As Date
Get
Return _Birthdate
End Get
Set(ByVal Value As String)
_Birthdate = Value
End Set
End Property
End Class
string names or index numbers in the form of MyClass.Item("LastName"). The
string names or item index values would be populated by a data-driven loop.
I need a few pointers to get me started in the right direction. I'm pretty
sure that I need a default Item property but I'm not sure how to create that
or index the other properties. A short code example is below
Joel Reinford
Assume a DataTable with four columns: FirstName, LastName, Birthdate, Gender
Function CreatePerson(dt as DataTable) as Person
Dim p as New Person
Dim dc As DataColumn
Dim drw As DataRow = dt.Rows(0)
For Each dc In dt.Columns
'this is what I want to achieve, it would translate into
p.FirstName, p.LastName, etc.
p.Item(dc.ColumnName) = drw(dc.ColumnName)
Next
End Function
Here is what a sample class looks like now
Public Class Person
Private _FirstName As String
Private _LastName As String
Private _LastName As String
Private _Gender As String
Private _Birthdate As Date
Property FirstName() As String
Get
Return _FirstName
End Get
Set(ByVal Value As String)
_FirstName = Value
End Set
End Property
Property LastName() As String
Get
Return _LastName
End Get
Set(ByVal Value As String)
_LastName = Value
End Set
End Property
Property Gender() As String
Get
Return _Gender
End Get
Set(ByVal Value As String)
_Gender = Value
End Set
End Property
Property Birthdate() As Date
Get
Return _Birthdate
End Get
Set(ByVal Value As String)
_Birthdate = Value
End Set
End Property
End Class