RichardLee
Member
- Joined
- Dec 29, 2008
- Messages
- 7
- Programming Experience
- 5-10
I have a custom object which I have created lets call it MyObject
The idea behind it is that this object can contain any number of attributes, and I don't know what those attributes may be untill I read them from a database.
So you could have two MyObjects setup to represent User Accounts.
Imagine that the users may have the following details.
User1
UniqueID = 0
Attributes
(0) FirstName = "Bob"
(1) LastName = "Jones"
(2) Sex = "Male"
User2
UniqueID = 1
Attributes
(0) FirstName = "Sarah"
(1) MiddleInitial = "K"
(2) Team = "Unit 1"
(3) DOB = "1/1/1954"
If I where to bind a collection of MyObjects against a DataGridView all that
I would see would be a single column showing the UniqueID's.
But how should I go about exposing each of the attributes as properties that could be bound against the gridview?
so i'd get something like the following appearing in the gridview
[UniqueID], [FirstName], [LastName], [MiddleInitial], [Team], [Sex]
0, Bob, Jones, null, null, Male
1, Sarah, null, K, Unit 1, null
I have seen many examples on the net Like This on how to create a wrapper class where you identify your properties and then you just load the wrapper from the objects you are using.
But in each of the examples I have found, they require you already know the exact number of properties and hardcode this into your wrapper class. But in my system that is not something that I can know before I come to pulling the data out of the database, the data is always subject to change between records, where some objects may have the same attributes, and others may have totally different attributes.
Is there some way of dynamicaly creating a class with any number of properties at runtime? i'm not really sure how best to proceed with this, if anyone has any ideas i'd like to hear them. I'm not event sure this is possible with the gridview so even being told "it can't be done" would be helpful to know.
Here is a rough cut-down example of my classes (not checked to see if this code is working, but should give you an idea what i'm working with).
The idea behind it is that this object can contain any number of attributes, and I don't know what those attributes may be untill I read them from a database.
So you could have two MyObjects setup to represent User Accounts.
Imagine that the users may have the following details.
User1
UniqueID = 0
Attributes
(0) FirstName = "Bob"
(1) LastName = "Jones"
(2) Sex = "Male"
User2
UniqueID = 1
Attributes
(0) FirstName = "Sarah"
(1) MiddleInitial = "K"
(2) Team = "Unit 1"
(3) DOB = "1/1/1954"
If I where to bind a collection of MyObjects against a DataGridView all that
I would see would be a single column showing the UniqueID's.
But how should I go about exposing each of the attributes as properties that could be bound against the gridview?
so i'd get something like the following appearing in the gridview
[UniqueID], [FirstName], [LastName], [MiddleInitial], [Team], [Sex]
0, Bob, Jones, null, null, Male
1, Sarah, null, K, Unit 1, null
I have seen many examples on the net Like This on how to create a wrapper class where you identify your properties and then you just load the wrapper from the objects you are using.
But in each of the examples I have found, they require you already know the exact number of properties and hardcode this into your wrapper class. But in my system that is not something that I can know before I come to pulling the data out of the database, the data is always subject to change between records, where some objects may have the same attributes, and others may have totally different attributes.
Is there some way of dynamicaly creating a class with any number of properties at runtime? i'm not really sure how best to proceed with this, if anyone has any ideas i'd like to hear them. I'm not event sure this is possible with the gridview so even being told "it can't be done" would be helpful to know.
Here is a rough cut-down example of my classes (not checked to see if this code is working, but should give you an idea what i'm working with).
VB.NET:
class MyObject
private _ID as integer
Private _Attributes As New AttributeCollection
Public Property UniqueID() As Integer
Get
Return _ID
End Get
Set(ByVal value As Integer)
_ID = value
End Set
End Property
Public Property Attributes() As AttributeCollection
Get
Return _Attributes
End Get
Set(ByVal value As AttributeCollection)
_Attributes = value
End Set
End Property
Public Sub SetAttribute( _
ByVal Key As String, _
Optional ByVal Value As Object = Nothing, _
Optional ByVal RecordID As Integer = -1 _
)
If Attributes.Contains(Key) Then
Attributes(Key).SetValue = Value
Else
Attributes.Add(New Attribute(Key, Value, RecordID))
End If
End Sub
end class
VB.NET:
Public Class AttributeCollection
Inherits Collections.ObjectModel.KeyedCollection(Of String, Attribute)
Protected Overrides Function GetKeyForItem(ByVal item As Attribute) As String
Return item.Key
End Function
End Class
VB.NET:
Public Class Attribute
Private _RecordID As Integer
Private _Key As String
Private _Value As String
Public Property RecordID() As Integer
Get
Return _RecordID
End Get
Set(ByVal value As Integer)
_RecordID = value
End Set
End Property
Public Property Key() As String
Get
Return _Key
End Get
Set(ByVal value As String)
_Key = value
End Set
End Property
Public Property Value() As String
Get
Return _Value
End Get
Set(ByVal value As String)
Me.SetValue = value
End Set
End Property
Public WriteOnly Property SetValue() As Object
Set(ByVal value As Object)
If IsNothing(value) Then
_Value = Nothing
Exit Property
End If
If value.GetType.ToString.Contains("DBNull") Then
_Value = Nothing
Else
_Value = value
End If
End Set
End Property
Public Sub New( _
Optional ByVal Key As String = Nothing, _
Optional ByVal Value As Object = Nothing, _
Optional ByVal RecordID As Integer = -1 _
)
With Me
.RecordID = RecordID
.Key = Key
.SetValue = Value
End With
End Sub
End Class
Last edited: