473,396 Members | 2,147 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,396 software developers and data experts.

Creating objects dynamically in a loop

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
Jan 12 '07 #1
6 5435
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jan 13 '07 #2
Mattias,

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

Private oDTs(2) As DataTable

Cor

"Mattias Sjögren" <ma********************@mvps.orgschreef in bericht
news:uE****************@TK2MSFTNGP03.phx.gbl...
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Jan 13 '07 #3
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

"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:uE****************@TK2MSFTNGP03.phx.gbl...
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Jan 13 '07 #4
>I would use the private keyword to show this instead of the ancient Dim in
>this place.
I assumed they were local variables.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jan 13 '07 #5
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jan 13 '07 #6
Mattias,

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

Cor

"Mattias Sjögren" <ma********************@mvps.orgschreef in bericht
news:u3***************@TK2MSFTNGP03.phx.gbl...
I would use the private keyword to show this instead of the ancient Dim
in
this place.

I assumed they were local variables.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Jan 13 '07 #7

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

Similar topics

6
by: Bhavin | last post by:
Anybody quickly replies it, would be a great help! I try to create dynamic array of textboxes control in ASP.NET(C#). I got the following error while I tried to assign value to ID property of...
6
by: NotGiven | last post by:
I want to learn moer of what I saw in a recent example. They create a page that created new fields/element. It's not like they were hidden and they displayed them, they were not there, then the...
2
by: JJ L. | last post by:
Hello. I have a project that consists of nine different objects, each serving their own purpose. In the past I have just created a form for each one, and then whenever you call, say,...
9
by: Patrick.O.Ige | last post by:
I have a code below and its a PIE & BAR CHART. The values now are all static but I want to be able to pull the values from a database. Can you guys give me some ideas to do this? Thanks ...
3
by: simon | last post by:
Hello, i'm looking to create an aspx page that is basically a FAQ page i'm not sure if this can be done, but would love some help with suggestions of how to do it another way if this is not...
2
by: RSH | last post by:
How do I go about creating an object name in a loop? I have a class named objSample Dim j As Integer For j = 1 To 10 Dim inpFile As System.Web.UI.HtmlControls.HtmlInputFile =
5
by: fireball | last post by:
please help newbie I need to create a lot of objects the same type (let's say: schemas) I wish to use paramerized block in loop to do so. - how to put names of my objects to such control-flow? ...
3
by: Bhavsan | last post by:
Here is what I am trying to do. Kindly, help me. 1. I'm creating a query dynamically based on User input using VBA (strSQL and DotSQL in the code below) 2. Executing the created query with in VBA...
1
by: joe_doufu | last post by:
I'm creating a page with multiple "widgets", each with its own XMLHttpRequest object, so the user can play with the widgets in parallel. The widgets are enclosed in divs with class "widget" and a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.