Tuesday, May 22, 2007

Creating Connections and Networks

To create a new connection instance, call the constructor of the appropriate concrete class and specify the name and the price for this connection.
// Create a new NicConnection instance.
NicConnection conn = new NicConnection("NicConnection", 6);
Use the Add method of the ConnectionCollection class
// Add it to the ConnectionMonitor Connections collection.
sampleMonitor.Connections.Add(conn);

You can also use the other methods of the KeyedCollection and Collection classes (from which the ConnectionCollection class inherits) to check for the presence of a specific connection, get a reference to it, and remove it from the ConnectionCollection.

// See if a connection named Internet exists.
if (sampleMonitor.Connections.Contains("Internet"))
{
// Check the connection status.
Connection conn = sampleMonitor.Connections["Internet"];
if (!conn.IsConnected)
{
// Display the price and remove it from the Connections collection.
MessageBox.Show("Removing connection with price "
+ conn.Price.ToString());
sampleMonitor.Connections.Remove(conn);
}
}
The same principles apply to the Network and NetworkCollection classes. You create a new Network instance by specifying the name in a call to the constructor.
Network network = new Network("Intranet", "http://intranet");
Then you add it to the NetworkCollection using the Add method.
sampleMonitor.Networks.Add(network);
You also use the same methods and properties of the underlying KeyedCollection and Collection classes to manipulate the collection of Network instances.
// See if a network named Internet exists.
if (sampleMonitor.Networks.Contains("Intranet"))
{
// Check connection status.
Network netwk = sampleMonitor.Networks["Intranet"];
if (!netwk.Connected)
{
// Remove the network from the Networks collection.
sampleMonitor.Networks.Remove(netwk);
}
}

No comments: