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 8 1642
Joel,
The "easiest" way is to add an indexed Item property with a Select Case for
each field
You can overload the Item Property for both Name & Index.
Something like: 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
Default Public Property Item(ByVal name As String) As Object
Get
Select Case name
Case "FirstName"
Return _FirstName
Case "LastName"
Return _LastName
Case "Gender"
Return _Gender
Case "Birthdate"
Return _Birthdate
Case Else
Throw New ArgumentOutOfRangeException("name", name,
"Invalid property name")
End Select
End Get
Set(ByVal value As Object)
Select Case name
Case "FirstName"
_FirstName = DirectCast(value, String)
Case "LastName"
_LastName = DirectCast(value, String)
Case "Gender"
_Gender = DirectCast(value, String)
Case "Birthdate"
_Birthdate = DirectCast(value, Date)
Case Else
Throw New ArgumentOutOfRangeException("name", name,
"Invalid property name")
End Select
End Set
End Property
Default Public Property Item(ByVal index As Integer) As Object
Get
' similar select case as above
End Get
Set(ByVal value As Object)
' similar select case as above
End Set
End Property
End Class
Including Default on the Item property allows you to drop Item
Dim name As string
name = MyClass.Item("LastName")
name = MyClass("LastName")
Using Reflection you can get ride of the Select Case, post if you want to
know how to do it with Reflection.
Hope this helps
Jay
"Joel Reinford" <jr********@earthlink.net> wrote in message
news:Ku********************@comcast.com... 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
Jay:
Yes, I'd like to avoid the Select Case. My reason for doing this is to save
some code in other places and it seem like I'd just be swapping where the
lines of code are. I'd like some input on how to use Reflection. Also, any
thoughts on the late binding in Reflection and performance hit.
Joel
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:Oj**************@TK2MSFTNGP09.phx.gbl... Joel, The "easiest" way is to add an indexed Item property with a Select Case
for each field
You can overload the Item Property for both Name & Index.
Something like:
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
Default Public Property Item(ByVal name As String) As Object Get Select Case name Case "FirstName" Return _FirstName Case "LastName" Return _LastName Case "Gender" Return _Gender Case "Birthdate" Return _Birthdate Case Else Throw New ArgumentOutOfRangeException("name", name, "Invalid property name") End Select End Get Set(ByVal value As Object) Select Case name Case "FirstName" _FirstName = DirectCast(value, String) Case "LastName" _LastName = DirectCast(value, String) Case "Gender" _Gender = DirectCast(value, String) Case "Birthdate" _Birthdate = DirectCast(value, Date) Case Else Throw New ArgumentOutOfRangeException("name", name, "Invalid property name") End Select End Set End Property
Default Public Property Item(ByVal index As Integer) As Object Get ' similar select case as above End Get Set(ByVal value As Object) ' similar select case as above End Set End Property
End Class
Including Default on the Item property allows you to drop Item
Dim name As string name = MyClass.Item("LastName") name = MyClass("LastName")
Using Reflection you can get ride of the Select Case, post if you want to know how to do it with Reflection.
Hope this helps Jay
"Joel Reinford" <jr********@earthlink.net> wrote in message news:Ku********************@comcast.com... 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
Joel,
Here's a version that uses Reflections (the System.ComponentModel really,
which is based on Reflection). 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
Private Shared ReadOnly m_properties As
System.ComponentModel.PropertyDescriptorCollection
Shared Sub New()
m_properties =
System.ComponentModel.TypeDescriptor.GetProperties (GetType(Person))
End Sub
Default Public Property Item(ByVal name As String) As Object
Get
Return m_properties(name).GetValue(Me)
End Get
Set(ByVal value As Object)
m_properties(name).SetValue(Me, value)
End Set
End Property
Default Public Property Item(ByVal index As Integer) As Object
Get
Return m_properties(index).GetValue(Me)
End Get
Set(ByVal value As Object)
m_properties(index).SetValue(Me, value)
End Set
End Property
End Class
The Item(string) property should really throw an ArgumentOutOfRangeException
or similar when passed an invalid property name, I will leave that as an
exercise for you.
Hope this helps
Jay
"Joel Reinford" <jr********@earthlink.net> wrote in message
news:Ku********************@comcast.com... 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
Jay:
That's what I was looking for. This is also much more maintainable than the
Select Case method. I can add/subtract properties without needing to change
anything else. Thanks very much.
Joel
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ev**************@tk2msftngp13.phx.gbl... Joel, Here's a version that uses Reflections (the System.ComponentModel really, which is based on Reflection).
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 Private Shared ReadOnly m_properties As System.ComponentModel.PropertyDescriptorCollection
Shared Sub New() m_properties = System.ComponentModel.TypeDescriptor.GetProperties (GetType(Person)) End Sub
Default Public Property Item(ByVal name As String) As Object Get Return m_properties(name).GetValue(Me) End Get Set(ByVal value As Object) m_properties(name).SetValue(Me, value) End Set End Property
Default Public Property Item(ByVal index As Integer) As Object Get Return m_properties(index).GetValue(Me) End Get Set(ByVal value As Object) m_properties(index).SetValue(Me, value) End Set End Property
> End Class
The Item(string) property should really throw an
ArgumentOutOfRangeException or similar when passed an invalid property name, I will leave that as an exercise for you.
Hope this helps Jay
"Joel Reinford" <jr********@earthlink.net> wrote in message news:Ku********************@comcast.com... 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
Joel, Also, any thoughts on the late binding in Reflection and performance hit.
I use reflection where it actually makes sense to use reflection.
I use late binding (Option Strict Off) where it makes sense to use late
binding. (such as some COM interop).
I rarely code for performance first, rather I code for "Correctness" first
(primarily OOP), then will code for performance when a routine is proven to
have a performance problem. I find most OOP designs lend themselves to being
rather performant from the start. (such as using polymorphism in favor of
conditionals (select case)).
Hope this helps
Jay
"Joel Reinford" <jr********@earthlink.net> wrote in message
news:Ep********************@comcast.com... Jay:
Yes, I'd like to avoid the Select Case. My reason for doing this is to
save some code in other places and it seem like I'd just be swapping where the lines of code are. I'd like some input on how to use Reflection. Also, any thoughts on the late binding in Reflection and performance hit.
Joel
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:Oj**************@TK2MSFTNGP09.phx.gbl... Joel, The "easiest" way is to add an indexed Item property with a Select Case for each field
You can overload the Item Property for both Name & Index.
Something like:
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
Default Public Property Item(ByVal name As String) As Object Get Select Case name Case "FirstName" Return _FirstName Case "LastName" Return _LastName Case "Gender" Return _Gender Case "Birthdate" Return _Birthdate Case Else Throw New ArgumentOutOfRangeException("name", name, "Invalid property name") End Select End Get Set(ByVal value As Object) Select Case name Case "FirstName" _FirstName = DirectCast(value, String) Case "LastName" _LastName = DirectCast(value, String) Case "Gender" _Gender = DirectCast(value, String) Case "Birthdate" _Birthdate = DirectCast(value, Date) Case Else Throw New ArgumentOutOfRangeException("name", name, "Invalid property name") End Select End Set End Property
Default Public Property Item(ByVal index As Integer) As Object Get ' similar select case as above End Get Set(ByVal value As Object) ' similar select case as above End Set End Property
End Class
Including Default on the Item property allows you to drop Item
Dim name As string name = MyClass.Item("LastName") name = MyClass("LastName")
Using Reflection you can get ride of the Select Case, post if you want
to know how to do it with Reflection.
Hope this helps Jay
"Joel Reinford" <jr********@earthlink.net> wrote in message news:Ku********************@comcast.com... 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
Joel,
In addition to my other post, thinking about this. It appears that you
really want to use a Typed DataSet! Especially if you already have your data
in a DataTable! And you domain objects don't really have any logic (they are
only properties).
As a Typed DataSet gives you type safe properties, plus allows you to index
those properties by Name or Index.
Then you can "avoid" "coding" the Person class itself & all those
properties...
Remember that Typed DataSets are still OO!
Martin Fowler's book "Patterns of Enterprise Application Architecture" from
Addison Wesley http://www.martinfowler.com/books.html#eaa explains when you
may want to use a traditional Domain Model & Data Mapper pattern: http://www.martinfowler.com/eaaCatalog/domainModel.html http://www.martinfowler.com/eaaCatalog/dataMapper.html
verses a Table Module & Data Gateway patterns: http://www.martinfowler.com/eaaCatalog/tableModule.html http://www.martinfowler.com/eaaCatal...taGateway.html
Martin also offers a couple of other useful patterns that can be used
instead of or in conjunction with the above patterns.
The System.Data.DataTable is an implementation of a Record Set pattern: http://www.martinfowler.com/eaaCatalog/recordSet.html
Rockford Lhotka's book "Expert One-on-One Visual Basic .NET Business
Objects" from A! Press provides a pre-implemented variation of Fowler's
Domain Model & Data Mapper patterns. http://www.lhotka.net/
Generally if there is no real logic behind my domain objects, I would use
the DataSet OOM coupled with a Table Module & Data Gateway patterns. As the
classes themselves are not really living up to their potential! :-) The
Table Module & Data Gateway patterns may be implemented in a single class or
two classes. Again I would consider using a Typed DataSet.
However if there is significant logic behind my domain objects, I would then
favor the Domain Model & Data Mapper patterns.
Depending on the needs of the project I would consider Fowler's other
patterns...
Hope this helps
Jay
"Joel Reinford" <jr********@earthlink.net> wrote in message
news:Ku********************@comcast.com... 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
Jay:
Thanks for the information. I used the datatable example only because I
thought it would make my question easy to understand. My real usage for this
at the moment is matching SQL output parameters to a business entity class
object. I'm using the DAAB SqlHelper method to get the parameters from the
stored procedure and I want to match the values in the output parameters to
the properties in the business entity class. Right now, that involves
declaring the parameter,
using the parameter, and retrieving the parameter value. If I can match the
parameter name to the property name, I should be able to do that with a
loop, rather than the tedious job of writing several lines of code for each
parameter.
Having said all that, I will definitely check out the links. There is always
more to learn and better ways to do things. Again, I appreciate your help
Joel
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ud**************@TK2MSFTNGP11.phx.gbl... Joel, In addition to my other post, thinking about this. It appears that you really want to use a Typed DataSet! Especially if you already have your
data in a DataTable! And you domain objects don't really have any logic (they
are only properties).
As a Typed DataSet gives you type safe properties, plus allows you to
index those properties by Name or Index.
Then you can "avoid" "coding" the Person class itself & all those properties...
Remember that Typed DataSets are still OO!
Martin Fowler's book "Patterns of Enterprise Application Architecture"
from Addison Wesley http://www.martinfowler.com/books.html#eaa explains when
you may want to use a traditional Domain Model & Data Mapper pattern: http://www.martinfowler.com/eaaCatalog/domainModel.html http://www.martinfowler.com/eaaCatalog/dataMapper.html
verses a Table Module & Data Gateway patterns: http://www.martinfowler.com/eaaCatalog/tableModule.html http://www.martinfowler.com/eaaCatal...taGateway.html
Martin also offers a couple of other useful patterns that can be used instead of or in conjunction with the above patterns.
The System.Data.DataTable is an implementation of a Record Set pattern: http://www.martinfowler.com/eaaCatalog/recordSet.html
Rockford Lhotka's book "Expert One-on-One Visual Basic .NET Business Objects" from A! Press provides a pre-implemented variation of Fowler's Domain Model & Data Mapper patterns. http://www.lhotka.net/ Generally if there is no real logic behind my domain objects, I would use the DataSet OOM coupled with a Table Module & Data Gateway patterns. As
the classes themselves are not really living up to their potential! :-) The Table Module & Data Gateway patterns may be implemented in a single class
or two classes. Again I would consider using a Typed DataSet.
However if there is significant logic behind my domain objects, I would
then favor the Domain Model & Data Mapper patterns.
Depending on the needs of the project I would consider Fowler's other patterns...
Hope this helps Jay "Joel Reinford" <jr********@earthlink.net> wrote in message news:Ku********************@comcast.com... 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
Joel,
Another alternative would be to take my code and create your own
Adapter/DataMapper that populated the domain object from a
DataReader/DataCommand. Without adding an Item property to each domain
object. For me: the Item property feels like a "back door" to the object
that should not be part of the domain object, the logic that I gave for the
Item Property feels more correct in the Data Mapper...
I prefer the Data Mapper model where the Data Mapper returns a wholly
constructed domain object, by calling the constructor of the Domain object
with each value it needs, this unfortunately can lead to duplication in
validation code, which needs to be mitigated...
Hope this helps
Jay
"Joel Reinford" <jr********@earthlink.net> wrote in message
news:Of********************@comcast.com... Jay:
Thanks for the information. I used the datatable example only because I thought it would make my question easy to understand. My real usage for
this at the moment is matching SQL output parameters to a business entity class object. I'm using the DAAB SqlHelper method to get the parameters from the stored procedure and I want to match the values in the output parameters
to the properties in the business entity class. Right now, that involves declaring the parameter, using the parameter, and retrieving the parameter value. If I can match
the parameter name to the property name, I should be able to do that with a loop, rather than the tedious job of writing several lines of code for
each parameter.
Having said all that, I will definitely check out the links. There is
always more to learn and better ways to do things. Again, I appreciate your help
Joel
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:ud**************@TK2MSFTNGP11.phx.gbl... Joel, In addition to my other post, thinking about this. It appears that you really want to use a Typed DataSet! Especially if you already have your data in a DataTable! And you domain objects don't really have any logic (they are only properties).
As a Typed DataSet gives you type safe properties, plus allows you to index those properties by Name or Index.
Then you can "avoid" "coding" the Person class itself & all those properties...
Remember that Typed DataSets are still OO!
Martin Fowler's book "Patterns of Enterprise Application Architecture" from Addison Wesley http://www.martinfowler.com/books.html#eaa explains when you may want to use a traditional Domain Model & Data Mapper pattern: http://www.martinfowler.com/eaaCatalog/domainModel.html http://www.martinfowler.com/eaaCatalog/dataMapper.html
verses a Table Module & Data Gateway patterns: http://www.martinfowler.com/eaaCatalog/tableModule.html http://www.martinfowler.com/eaaCatal...taGateway.html
Martin also offers a couple of other useful patterns that can be used instead of or in conjunction with the above patterns.
The System.Data.DataTable is an implementation of a Record Set pattern: http://www.martinfowler.com/eaaCatalog/recordSet.html
Rockford Lhotka's book "Expert One-on-One Visual Basic .NET Business Objects" from A! Press provides a pre-implemented variation of Fowler's Domain Model & Data Mapper patterns. http://www.lhotka.net/ Generally if there is no real logic behind my domain objects, I would
use the DataSet OOM coupled with a Table Module & Data Gateway patterns. As the classes themselves are not really living up to their potential! :-) The Table Module & Data Gateway patterns may be implemented in a single
class or two classes. Again I would consider using a Typed DataSet.
However if there is significant logic behind my domain objects, I would then favor the Domain Model & Data Mapper patterns.
Depending on the needs of the project I would consider Fowler's other patterns...
Hope this helps Jay "Joel Reinford" <jr********@earthlink.net> wrote in message news:Ku********************@comcast.com... 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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Hal Vaughan |
last post by:
I want to have a config file for my program, which means I need to know
where the config file is. If I type:
java myclass
and it runs myclass.class, is there any way to obtain the location of...
|
by: Peter Frost |
last post by:
Please help
I don't know if this is possible but what I would really like to do is
to use On Error Goto to capture the code that is being executed when
an error occurs.
Any help would be much...
|
by: downwitch |
last post by:
Hi,
I know this has been covered here and in the .public groups, but it
seems like it's been a while, especially around here, so I just thought
I'd ask again to see if anyone has figured out a...
|
by: Slavyan |
last post by:
(I just started to learn C#.NET)
What's the syntax in c# for a class to inherit more than one class.
I know following syntax:
public class MyClass : MyOtherClass
{
}
but I need to inherit...
|
by: Lei Jiang |
last post by:
I have a C# class :
public class MyClass
{
public object this
{
}
}
In my C++ code, how to access this index method?
|
by: Andrew J. Marshall |
last post by:
It was SO easy in VB6. Add a few methods, set a few funky properties and
*BAM*, type-safe collection class that supports the For Each...Next syntax.
I've been trying to do this in VB.NET with...
|
by: Rose winsle |
last post by:
Hi guys ...
I just wanted to create virtual directory using VB.net . i can manage that
... when i create the virtual directory how can i set anonymous access off.
following is my code...
|
by: budy_ludy |
last post by:
Hi All,
I am new to vb .net,
I have an ArrayList and i store class objects in it,
and later i want to retrieve each ArrayList items and type cast to the
class,
How can it be done ?
I used...
|
by: berrylthird |
last post by:
This question was inspired by scripting languages such as JavaScript. In JavaScript, I can access members of a class using array syntax as in the following example:
var myInstance:myClass = new...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
| |