Shaun Luttin
Shaun Luttin

Reputation: 141522

What does it mean to open a network interface driver?

To work with GHI network interface drivers, we first need to call Open(). What does it mean to open a network interface? The documentation says nothing other than that the method call is required.

E.g.

var wiFiRs9110 = new WiFiRS9110(
    SPI.SPI_module.SPI2,
    GHI.Pins.G120.P1_10,
    GHI.Pins.G120.P2_11,
    GHI.Pins.G120.P1_9, 4000);

wiFiRs9110.Open(); // What does this do?

wiFiRs9110.EnableStaticIP(IpAddress, SubnetMask, GatewayAddress);

Upvotes: 0

Views: 76

Answers (1)

C.Evenhuis
C.Evenhuis

Reputation: 26446

You can imagine that a piece of hardware would require some initialization before it can be used.

They could have chosen to integrate the code from Open() in the constructor and Close() in the Dispose() method, but then:

  • All parameters that affect how the hardware/driver/class is initialized must then be passed in the constructor (event handlers are a good example here, you don't want to miss the first event)
  • You cannot "release" (or Close()) the network interface without disposing the object, so if you'd want to access it again you'd have to pass the same parameters to a new instance again
  • If anything goes wrong, the constructor would have to throw an exception. It makes more sense to have the constructor throw when you pass invalid parameters, and to have an Open() method that throws when the hardware does not respond.

There are also some less obvious issues with "starting" something from within a constructor, which may not apply to these drivers:

  • Interfaces cannot define constructors, so the interface would not completely represent what you're trying to build
  • When subclassing a class that "starts" from the constructor, if you want to "start differently", there is no parent constructor to call

Upvotes: 1

Related Questions