Results 1 to 10 of 10

Thread: Looking for a workaround

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 1999
    Location
    Atlanta, GA
    Posts
    75

    Looking for a workaround

    Here's the problem.

    I have a program that is creating multiple instances of an ActiveX exe object. Since the user is given the option of choosing how many instances will be created, an array of these objects is the best bet. Unfortunately, arrays do do not work with the "WithEvents" statement (which is necessary for my program to interact with the sessions).

    Is there a workaround for this? Creating and working with 10 variables is bulky and annoying.

    Thanks.

    Jay

  2. #2
    jim mcnamara
    Guest
    You can reference the objects as members of a collection:

    Code:
    Dim MyColl as Collection
    Dim MyObj as Object
    
    For i = 1 to 10
         Set MyObj = New  whatever
         MyColl.Add MyObj
    Next i
    
    'Then reference each one 
    For i = 1 to MyColl.Count
      print Mycoll.Item(i).property1
    next i

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 1999
    Location
    Atlanta, GA
    Posts
    75
    Thanks for the idea of collections. It solves my initial problem beautifully, but another question has risen out of it.

    How do I reference an event being raised by one of the instances in the collection?

    Right now the code to take care of a single instance named GobjTimer is:

    Private Sub GobjTimer_PeveNotify(ByVal LintEventCode As Integer)
    ' Do things
    End Sub

    Thanks again for the help

  4. #4
    New Member
    Join Date
    Aug 2001
    Location
    Winnipeg
    Posts
    10

    Collection woes

    Couldn't you wrap up the collection as a class module with an event that passes back the id or instance of the ctl ??

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 1999
    Location
    Atlanta, GA
    Posts
    75
    I am a little confused about what you are suggesting. This is the scenario of what I need.

    I am using an ActiveX exe to carry out a series of internet related functions. To decrease the amount of time it takes to carry out these functions, I have created the ActiveX exe to handle the events asynchronously. X number of these sessions are created, and raise events to the calling program indicating various degrees of progress.

    To track these "progress reports", the main program needs to accept the events and calculate the passed results.

    What I need to do is find a way to spawn numerous instances of the ActiveX component, and wait for results as they are continuously sent back.

    Using the suggestion of collections, I am able to create a dynamic number of these instances without having to create multiple variables. The problem is that there is no way to reference them. Normally, one would say variablename_event.
    With the collection, however, collectionname.Item(index)_event does not work. I need a way to check for multiple occurrences of an event that are coming from multiple instances of the control.

    Suggestions, anyone?

  6. #6
    New Member
    Join Date
    Feb 2005
    Posts
    1

    Re: Looking for a workaround

    I have the same problem and will do the following, if anyone has a better idea please let me know, i would like to have collections like controls collections in VB6 where an eventhandler includes the index of the object firing the event.

    public class MyDll
    private Owner as MyDllCollection
    public id as Integer
    public sub SetOwner(Value as MyDllCollection)
    Owner=Value
    end sub
    public sub DoReport
    'Do your report here
    Owner.ChildComplete(Me)
    end sub
    end class

    public class MyDllCollection
    Inherits System.Collections.CollectionBase
    public event Completed(index as integer)
    Default Property Item(ByVal index As Integer) As MyDll
    Get
    Return CType(innerlist.Item(index), MyDll)
    End Get
    Set(ByVal Value As qServer)
    innerlist.Add(Value)
    End Set
    End Property
    public Function Add(ByVal Value As MyDll) As Integer
    InnerList.Add(Value)
    Value.SetOwner(Me)
    Value.id=innerlist.IndexOf(Value)
    Return innerlist.IndexOf(Value)
    End Function
    public sub ChildComplete(ByRef Value as MyDll)
    raisevent Completed(Value.id)
    end sub
    end class

    This requires som synchronisation as well but should do what you are asking for. I haven't tried it yet but it should work.

  7. #7
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: Looking for a workaround

    I believe this is technically outside the realm of what VB6 can handle. In order for a Parent object to listen for Events on a Child Object, you need to do a
    VB Code:
    1. Dim WithEvents Object as ObjectType
    which implies a design-time restriction unmallable at run-time.

    What you are asking for is something like:
    VB Code:
    1. Dim WithEvents CollectionOfObjects As (ObjectType or Collection)
    which is not something VB6 was designed for.

    This does not imply that you cannot reach your end goal, just that you will have to find another means of doing it.
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  8. #8
    Lively Member
    Join Date
    Sep 2004
    Posts
    74

    Re: Looking for a workaround

    Zeke is pointing to the right direction.

    Why not wrap the ActiveX in an class module and define the event handlers in that class mod, and instead of creating an new activex object you could create an new class which in turn creates the activex and handles the events
    and put these clas objects in an collection.

  9. #9
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: Looking for a workaround

    I think the issue at hand is changing the number of Objects that a Parent Object can listen to their Events.

    At design time, you use the
    VB Code:
    1. Dim WithEvents Object As ObjectType
    to tell the Parent Object that it should listen to the Events Raised by that Object.

    He wants to change the number of Objects listened to at run time from 1 to say 3 or 4, and be able to know exactly which Object raised the Event.

    You could make a single ActiveX Object to manage dynamically created aggregate objects, if you gave them all different names or something, but the original proposal is outside the bounds of VB6 functionality.
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  10. #10
    Fanatic Member namrekka's Avatar
    Join Date
    Feb 2005
    Location
    Netherlands
    Posts
    639

    Re: Looking for a workaround

    Once I had the same and rebuild it to an OCX.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width