Creating objects dynamically in a loop

R

RSH

Hi,

i have a situation where I need to dynamically create objects in a loop. My
question surrounds intantiation naming in such a scenerio.

Below is a snippet that is basically hardcoding each object. My problem is
that I would like to create the objects dynamically but I can't figure out
how to do it
How would I go about dynamically creating the required objects so that I
could be using anywhere from 2 - 10 etc. ?



Thanks,

Ron



for x as integer = 1 to num

If x = 1 Then

' Create Object 1 ----------------------------

oDT1 = New DataTable

oDT1 = oDS.Tables(0).Clone

For i = min To max

oDT1.ImportRow(oDS.Tables(0).Rows(i))

Next

Dim oCompanyProcess1 As New CompanyWorkerClass(oDT1, strServer, "TH1")

AddHandler oCompanyProcess1.LineProcessed, AddressOf ClickCounter

AddHandler oCompanyProcess1.Finished, AddressOf ThreadFinished

Dim t1 = New Thread(AddressOf oCompanyProcess1.ProcessDataReport)

t1.Start()



ElseIf x = 2 Then

' Create Object 2 --------------------------

oDT2 = New DataTable

oDT2 = oDS.Tables(0).Clone

For i = min To max

oDT2.ImportRow(oDS.Tables(0).Rows(i))

Next

Dim oCompanyProcess2 As New CompanyWorkerClass(oDT2, strServer, "TH2")

AddHandler oCompanyProcess2.LineProcessed, AddressOf ClickCounter

AddHandler oCompanyProcess2.Finished, AddressOf ThreadFinished

Dim t2 = New Thread(AddressOf oCompanyProcess2.ProcessDataReport)

t2.Start()



ElseIf x = 3 Then

' Create Object 3 ----------------

oDT3 = New DataTable

oDT3 = oDS.Tables(0).Clone

For i = min To max

oDT3.ImportRow(oDS.Tables(0).Rows(i))

Next

Dim oCompanyProcess3 As New CompanyWorkerClass(oDT3, strServer, "TH3")

AddHandler oCompanyProcess3.LineProcessed, AddressOf ClickCounter

AddHandler oCompanyProcess3.Finished, AddressOf ThreadFinished

Dim t3 = New Thread(AddressOf oCompanyProcess3.ProcessDataReport)

t3.Start()

next
 
M

Mattias Sjögren

Below is a snippet that is basically hardcoding each object. My problem is
that I would like to create the objects dynamically but I can't figure out
how to do it
How would I go about dynamically creating the required objects so that I
could be using anywhere from 2 - 10 etc. ?

Use an array of DataTable, something like

Dim oDTs(2) As DataTable


Mattias
 
C

Cor Ligthert [MVP]

Mattias,

I would use the private keyword to show this instead of the ancient Dim in
this place.

Private oDTs(2) As DataTable

Cor
 
T

Tom Leylan

That may handle his creation and loading problems but that alone won't solve
his Dim oCompanyProcess1, 2, 3... and the multiple threads.

Frankly I believe you're going to have to create a class to encapsulate all
your knicknacks. The collection you create would be a collection of
whatever objects you call those things. The class would contain the
DataTable, your process, handler and thread. Now they don't have to be
named differently since there wrapped up in something more concrete.

It's time for folks to stop thinking "code" and started thinking "classes".
:)

Tom
 
M

Mattias Sjögren

That may handle his creation and loading problems but that alone won't solve
his Dim oCompanyProcess1, 2, 3... and the multiple threads.

Right, I didn't want to spell out all the changes. I hoped the
original poster would figure out the rest, since the principle is the
same for oCompanyProcessX. As for the threads, unless he needs to keep
references to each Thread object he could just reuse the same
variable.


Mattias
 
C

Cor Ligthert [MVP]

Mattias,

I thought that your idea around the sample was to place it global

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