Simple class here – but I needed just some random data in a datatable and I didn’t want to have to type it all in by hand.
I like this simple little class because one could easily change it to take the type and number of columns they need and then fill them randomly (creating random names could be done but was not implemented here– I have an idea at the bottom).
Anyway I create a class that will contain a table :
Public Class RandomDataTable
Public dt As New DataTable(“random”)
Public name As New DataColumn(“Name”, System.Type.GetType(“System.String”))
Public value As New DataColumn(“Value”, GetType(Integer))
Dim rndm As New Random
Dim dr As DataRow
Now we have a datatable that will have two columns. Next I create the constructor to take in the number of needed rows (howManyResults)… as well as a minimum range for the data and a maximum range for the data.
‘Make random data set of 2 items and numbers
Public Sub New(ByVal howManyResults As Integer, ByVal rangeMin As Integer, ByVal rangeMax As Integer)
Now I add the columns, loop through the number of results and fill in the two columns with Entity# and the Random data number ( I used a random object and called its min max function):
dt.Columns.Add(name)
dt.Columns.Add(value)
For i As Integer = 1 To howManyResults
Dim nameString = “Entity ” + i.ToString
Dim rowValue = rndm.Next(rangeMin, rangeMax)
dr = dt.NewRow()
dr.Item(name) = nameString
dr.Item(value) = rowValue
dt.Rows.Add(dr)
Next
And that is it! I create the class like this DIM MyRandomTable as dataTable = new RandomDataTable(50, 2, 100) and I have a nice filled in table of random data that I can then work with!
** ON THE NAME THING — I am right now planning on extending this by making me first names and last names… I plan on making a firstName array of about 5-10 names and a lastName array of 5-10 names and then in my loop – call a random function to grab random firsts and match them with random lasts… should be interesting.
Any thoughts or ideas on how to extend this???? Let me know!