Class variables to dataset

N

Nikolay Petrov

I am searching for an idea how to optimize some of my code.
Let's say I have a class, which exposes some properties:


Class Dummy1
Private m_sValue1 As String
Private m_sValue2 As String
Public Property Value1() As String
Get
Value1 = Me.m_sValue1
End Get
Set(ByVal Value As String)
Me.m_sValue1 = Value
End Set
End Property
Public Property Value2() As String
Get
Value1 = Me.m_sValue2
End Get
Set(ByVal Value As String)
Me.m_sValue2 = Value
End Set
End Property
End Class


I have second class, one of who's methods returns an array of the
previus Class. Also I have second method, which returns the same array,
but this time in dataset. I create this dataset and it's datatable from
code:
Public Function GetDumies2() As System.Data.DataSet
Dim RetVal As New System.Data.DataSet("DummyDS")
Dim dT As New System.Data.DataTable("Dummies")
Dim dCol As System.Data.DataColumn
dCol = New System.Data.DataColumn("Value1") '0
dCol.DataType = System.Type.GetType("System.String")
dT.Columns.Add(dCol)
dCol = New System.Data.DataColumn("Value2") '0
dCol.DataType = System.Type.GetType("System.String")
dT.Columns.Add(dCol)
Dim tmpVal As Dummy1()
tmpVal = Me.GetDumies
Dim i As Integer
For i = 0 To tmpVal.GetUpperBound(0)
Dim dR As System.Data.DataRow
dR = dT.NewRow
dR.Item("Value1") = tmpVal(i).Value1
dR.Item("Value2") = tmpVal(i).Value2
dT.Rows.Add(dR)
Next
RetVal.Tables.Add(dT)
RetVal.AcceptChanges()
Return RetVal
End Function

My exact question is, how to optimize the generation of the DataSet and
DataTable? I do this in many classes, and providing its properties in
the generation of the dataset has many cons.

Thanks in advance
 
C

Cor Ligthert

Nikolay,

I hope that you can see it with this sample that I made for you.

Take a new form, add a datagrid to it, and than paste this code in.

\\\
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim dummies(2) As Dummy1
dummies(0) = New Dummy1("Cor", "Holland")
dummies(1) = New Dummy1("Ken", "Florida")
dummies(2) = New Dummy1("Herfried", "Austria")
DataGrid1.DataSource = GetDumies2(dummies).Tables("Dummies")
End Sub
Public Function GetDumies2(ByVal Dummies() As Object) As
System.Data.DataSet
Dim mDummies() As Dummy1 = DirectCast(Dummies, Dummy1())
Dim ds As New DataSet("DummyDS")
Dim dt As New DataTable("Dummies")
ds.Tables.Add(dt)
dt.Columns.Add("Value1", GetType(System.String))
dt.Columns.Add("Value2", GetType(System.String))
For i As Integer = 0 To Dummies.Length - 1
dt.LoadDataRow(New Object() _
{mDummies(i).Value1, mDummies(i).Value2}, True)
Next
Return ds
End Function
End Class
Class Dummy1
Private mValue1 As String
Private mValue2 As String
Public Sub New(ByVal a As String, ByVal b As String)
mValue1 = a
mValue2 = b
End Sub
Public Property Value1() As String
Get
Value1 = Me.mValue1
End Get
Set(ByVal Value As String)
Me.mValue1 = Value
End Set
End Property
Public Property Value2() As String
Get
Value2 = Me.mValue2
End Get
Set(ByVal Value As String)
Me.mValue2 = Value
End Set
End Property
///

I hope this helps,

Cor
 
N

Nikolay Petrov

mostly I want to skip the manual creation of the DataColumns
Can I somehow walk through the propertires of the class and create new
columns, based of the names and types of the properties?
 
C

Cor Ligthert

Nikolay,

This works, if it is the best that I don't know.


\\\\Needs a datagrid and an import to system.reflections.
Private Sub Form14_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim dummies(2) As Dummy1
dummies(0) = New Dummy1("Cor", "Holland")
dummies(1) = New Dummy1("Ken", "Florida")
dummies(2) = New Dummy1("Herfried", "Austria")
DataGrid1.DataSource = GetDumies2(dummies).Tables("Dummies")
End Sub
Public Function GetDumies2(ByVal Dummies() As Object) As
System.Data.DataSet
Dim mDummies() As Dummy1 = DirectCast(Dummies, Dummy1())
Dim ds As New DataSet("DummyDS")
Dim dt As New DataTable("Dummies")
ds.Tables.Add(dt)
Dim pi() As PropertyInfo = Dummies(0).GetType().GetProperties()
For Each p As PropertyInfo In pi
dt.Columns.Add(p.Name, p.GetType)
Next
For i As Integer = 0 To Dummies.Length - 1
Dim pis() As PropertyInfo = Dummies(i).GetType().GetProperties()
Dim ob(pi.Length - 1) As Object
For y As Integer = 0 To pis.Length - 1
Dim z As Object = y
ob(y) = pis(y).GetValue(Dummies(i), Nothing)
Next
dt.LoadDataRow(ob, True)
Next
Return ds
End Function
End Class
Class Dummy1
Private mValue1 As String
Private mValue2 As String
Public Sub New(ByVal a As String, ByVal b As String)
mValue1 = a
mValue2 = b
End Sub
Public Property Value1() As String
Get
Value1 = Me.mValue1
End Get
Set(ByVal Value As String)
Me.mValue1 = Value
End Set
End Property
Public Property Value2() As String
Get
Value2 = Me.mValue2
End Get
Set(ByVal Value As String)
Me.mValue2 = Value
End Set
End Property
End Class
///

I hope this helps,

Cor
 

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