473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Class variables to dataset

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

Nov 21 '05 #1
4 1570
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
Nov 21 '05 #2
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?

Nov 21 '05 #3
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
Nov 21 '05 #4
Thanks Cor

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Andrew Jaffe | last post by:
Hi, I have a class with various class-level variables which are used to store global state information for all instances of a class. These are set by a classmethod as in the following (in...
2
by: David Adams | last post by:
Hi, I am afraid I am asking a very basic question, or just something that is not possible. I have a WinForm app that contains a Form (Form A), and then 20-30 UserControls/WinForms that are...
2
by: KavvY | last post by:
I'm confused about how to declare class variables and accessor pairs. The following code is my class and the error is below. If someone could point me in the right direction I'd really appreciate...
0
by: Ed West | last post by:
Hello, I am wondering about best practices for class hierarchies and using ADO.Net, especially the DataSet update/delete commands and the data relations... this needs to package/unpackage to xml...
7
by: fisab | last post by:
I apologise in advance for such a basic question, but I'm hoping someone will take the time to answer my question. In my code I define a Dataset : Partial Class Default_aspx Dim dsExcel As...
9
by: Brian Henry | last post by:
If i inherite a queue class into my class, and do an override of the enqueue member, how would i then go about actually doing an enqueue of an item? I am a little confused on this one... does over...
9
by: Geoffrey | last post by:
Hello, Form my software, I create a class to manage configuration. Configuration parameters are retrieved from wml file, ini file, registry, .... For this, I have a constructor that create a...
7
by: mathieu | last post by:
Hello, I did read the FAQ on template(*), since I could not find an answer to my current issue I am posting here. I have tried to summarize my issue in the following code (**). Basically I am...
2
by: Andy B | last post by:
I need to make a class and not quite sure how to go about doing this part. I want the class to take user input, build a dataset based on that input and then return it from the class so it can be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.