Creating objects dynamically in a loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RSH

    #1

    Creating objects dynamically in a loop

    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).C lone

    For i = min To max

    oDT1.ImportRow( oDS.Tables(0).R ows(i))

    Next

    Dim oCompanyProcess 1 As New CompanyWorkerCl ass(oDT1, strServer, "TH1")

    AddHandler oCompanyProcess 1.LineProcessed , AddressOf ClickCounter

    AddHandler oCompanyProcess 1.Finished, AddressOf ThreadFinished

    Dim t1 = New Thread(AddressO f oCompanyProcess 1.ProcessDataRe port)

    t1.Start()



    ElseIf x = 2 Then

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

    oDT2 = New DataTable

    oDT2 = oDS.Tables(0).C lone

    For i = min To max

    oDT2.ImportRow( oDS.Tables(0).R ows(i))

    Next

    Dim oCompanyProcess 2 As New CompanyWorkerCl ass(oDT2, strServer, "TH2")

    AddHandler oCompanyProcess 2.LineProcessed , AddressOf ClickCounter

    AddHandler oCompanyProcess 2.Finished, AddressOf ThreadFinished

    Dim t2 = New Thread(AddressO f oCompanyProcess 2.ProcessDataRe port)

    t2.Start()



    ElseIf x = 3 Then

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

    oDT3 = New DataTable

    oDT3 = oDS.Tables(0).C lone

    For i = min To max

    oDT3.ImportRow( oDS.Tables(0).R ows(i))

    Next

    Dim oCompanyProcess 3 As New CompanyWorkerCl ass(oDT3, strServer, "TH3")

    AddHandler oCompanyProcess 3.LineProcessed , AddressOf ClickCounter

    AddHandler oCompanyProcess 3.Finished, AddressOf ThreadFinished

    Dim t3 = New Thread(AddressO f oCompanyProcess 3.ProcessDataRe port)

    t3.Start()

    next


  • Mattias Sjögren

    #2
    Re: Creating objects dynamically in a loop

    >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.

    Comment

    • Cor Ligthert [MVP]

      #3
      Re: Creating objects dynamically in a loop

      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" <mattias.dont.w ant.spam@mvps.o rgschreef in bericht
      news:uE6z%23WrN HHA.5000@TK2MSF TNGP03.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.

      Comment

      • Tom Leylan

        #4
        Re: Creating objects dynamically in a loop

        That may handle his creation and loading problems but that alone won't solve
        his Dim oCompanyProcess 1, 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" <mattias.dont.w ant.spam@mvps.o rgwrote in message
        news:uE6z%23WrN HHA.5000@TK2MSF TNGP03.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.

        Comment

        • Mattias Sjögren

          #5
          Re: Creating objects dynamically in a loop

          >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.

          Comment

          • Mattias Sjögren

            #6
            Re: Creating objects dynamically in a loop

            >That may handle his creation and loading problems but that alone won't solve
            >his Dim oCompanyProcess 1, 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 oCompanyProcess X. 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.

            Comment

            • Cor Ligthert [MVP]

              #7
              Re: Creating objects dynamically in a loop

              Mattias,

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

              Cor

              "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rgschreef in bericht
              news:u3r%23WJvN HHA.780@TK2MSFT NGP03.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.

              Comment

              Working...