Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Any way to use constructor parameters within "dim as new"?

Any way to use constructor parameters within "dim as new"?

Scheduled Pinned Locked Moved Visual Basic
databasehardwaretutorialquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    supercat9
    wrote on last edited by
    #1

    I have a class which is supposed to simulate part of a piece of hardware. When the object is created, the constructor is given a reference to the underlying CPU-control object (an object of the class has no meaning without such a base object). The class needs a substantial number of "WithEvents" other objects which need to attach themselves to the underlying CPU control object. For example, "Dim MotorEnable as New IoBit(baseCPU, 4, 6)" will cause any writes to MotorEnable.pin to be visible on bit 6 of port 4 in the base CPU, and will cause any CPU writes to bit 6 of port 4 to trigger a "MotorEnable.PinChanged" event in the hardware-simulation object. The "Dim XX as New YY(params)" format of declaration is much more appealing than having a separate declaration and initialization. Unfortunately, even though the identity of the new master CPU object is known before all the "Dim XX as New YY()" statements start firing (they fire in response to calling New() with the master object as one of its parameters), as far as I can tell it only exists as a local variable which is not accessible to them. Is there any clean way to handle such constructs? Possible approaches: -1- Have the code for the piece of hardware Inherit the base CPU object, and create the new hardware object instead of the base CPU. That would work, but only for the case where there was only one hardware class trying to extend the CPU. -2- Have the code for the sub-feature objects accept a pointer to an iWillSupplyBase object, which will fire an event that will supply the real base CPU object. If the hardware simulation object implements iWillSupplyBase, it can fire the event within its New() procedure; the sub-feature objects can then attach themselves to the base object at that time. -3- Use a shared factory method which sets a ThreadStatic reference to the base CPU object and then calls the constructor. The "Dim As New" statements could then pass the ThreadStatic reference to the object constructors. This would probably work fine, but it seems really icky. -4- Have the simulation object create its own set of port handlers attached to the base CPU, and attach the port pins to those rather than the base CPU. That might be workable, but it seems rather icky, and would add a couple more levels of indirection to all I/O operations. -5- Give up on the Dim As New syntax, even though it makes much of the code nicer and easier to read. Is there any other brilliant approach I'm missing?

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups