Re: Beginner question re data structure

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



gc wrote:
Hello all,

I'm working on a console app that takes as input a list of domain names
then pings them. The data I want to work with is:

long rtt (ping round trip time in ms)
IPAddress ip
string hostName

Right now the app just prints the info in the order it's placed on the
command line.

What I want to do is store this information, then sort all the results
by rtt for display. (Lowest rtt first.)

I thought of adding an object that I could do something like add each
result (rtt, ip, hostName). The object could have a method to sort
itself and a tostring.

You're there already!


My problem is, how to represent the data structure rtt, ip, hostName?

Well, we could *almost* use System.Net.PingReply, except that you want to store the host *name* as well. I would say:

Put each result in a class:
class PingResult
{
// of course wrap these in properties in the real world...
public long RoundTripTime;
public IPAddress Address;
public string Hostname;
}

Now we want somewhere to put these. Your later post clarified that you are using C# 2.0, so we have the lovely generic containers available. One of these is List<>, which has a Sort method. But it will need to know how to sort the things we put in it. An easy way to do this is to have PingResult implement IComparable<>, so we add that to the definition:

class PingResult : IComparable<PingResult>
{
long RoundTripTime;
IPAddress Address;
string Hostname;

public int CompareTo(PingResult other)
{
if (other != null)
return this.RoundTripTime.CompareTo(other.RoundTripTime);
else
return 1;
}
}

Our comparison routine just defers to long's comparison.

Now when we obtain each ping result, we put a new PingResult in our collection:

List<PingResult> results = new List<PingResult>();

for (int i = 0; i < 10; ++i)
{
// get a result somehow
PingResult result = GetAPingResult();

// put it in the list
results.Add(result);
}

// sort the list
results.Sort();

// now the list is in order of RoundTripTime

The Sort method will see that PingResult implements IComparable<>, and so it will use that for sorting on.


--
Larry Lard
larrylard@xxxxxxxxxxxxxx
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
.



Relevant Pages

  • Re: Problem setting up broadband from Tiscali
    ... Sorry I can't help with a "One Click" Answer to the prob. ... screwed up by the failed Installation)? ... But that won't stop him (or you/us or anybody0 from being able to sort ... Which is why I included the way for him to find out if he *can* Ping ...
    (uk.people.silversurfers)
  • Re: Crashed Exchange 2003 server
    ... Ryan, I do not get an OS stop code of any sort. ... cannot ping. ... > the properties for the server in System Manager, ...
    (microsoft.public.exchange2000.admin)
  • Re: "is alive" command?
    ... > Looking for some sort of "is alive" command similiar to ping, ... I can ping the machine and output ... > then I have to sort through that file to get the result. ... torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway ...
    (microsoft.public.windowsxp.security_admin)
  • Re: Typhoons and raptors
    ... IOW, you don't ping down an IR beam with ... some sort of magical IR pulse to time a range return. ...
    (rec.aviation.military)