tags:

views:

708

answers:

5

I built a simple vb.net winforms project that pings IP addresses and logs the results. It works fine on most machines I've tried it on. I log the status result of the ping (System.Net.NetworkInformation.IPStatus) by using the IPStatus.tostring method.

Normally this returns a text result such as "Success" or "TimedOut"

Yesterday, on one machine it returned "65" ...which is not one of the enum values. I have a feeling it might be a combination of values. I ran some test code:

Dim status As System.Net.NetworkInformation.IPStatus
status = Net.NetworkInformation.IPStatus.Success
MsgBox(status.ToString)

Which returns "Success"

And this:

status = Net.NetworkInformation.IPStatus.BadDestination Or Net.NetworkInformation.IPStatus.BadHeader
MsgBox(status.ToString)

Which returns "11050"

I suspect the "65" I saw was the result of some combination of enum values. Is there any way I can change the code in my second example to show the text names of both values? That is... any way I can see ALL values in this variable?

+2  A: 

IPStatus is NOT a Flags enum, therefore it is not appropriate to combine its member values in this way. This is its definition via Reflector:

Public Enum IPStatus
    ' Fields
    BadDestination = &H2B0A
    BadHeader = &H2B22
    BadOption = &H2AFF
    BadRoute = &H2B04
    DestinationHostUnreachable = &H2AFB
    DestinationNetworkUnreachable = &H2AFA
    DestinationPortUnreachable = &H2AFD
    DestinationProhibited = &H2AFC
    DestinationProtocolUnreachable = &H2AFC
    DestinationScopeMismatch = &H2B25
    DestinationUnreachable = &H2B20
    HardwareError = &H2B00
    IcmpError = &H2B24
    NoResources = &H2AFE
    PacketTooBig = &H2B01
    ParameterProblem = &H2B07
    SourceQuench = &H2B08
    Success = 0
    TimedOut = &H2B02
    TimeExceeded = &H2B21
    TtlExpired = &H2B05
    TtlReassemblyTimeExceeded = &H2B06
    Unknown = -1
    UnrecognizedNextHeader = &H2B23
End Enum

How you are getting an IPStatus value of 65 - now that's the real question :)

AakashM
So the mystery remains... I did a ping with System.Net.NetworkInformation.Ping.Send and the System.Net.NetworkInformation.PingReply.ToString returned "65" and I have no idea why
B2Ben
A: 

It looks like apart from Success (0) and Unknown (-1), the defined enum values range from 11002 to 11045, so 65 is not a combination of any of the defined enum values.

If you are getting 65 back, you will not be able to resolve this to a string.

Patrick McDonald
A: 

That enum is not marked with the FlagsAttribute and therefore should not be or'd together because the result could overlap. You are better off creating your own enum to contain the values you are looking for.

Nescio
A: 

You should be able to loop over the enum's range using something like [1], test whether the current enum bit is represented in the value and add it to a string builder.

I find it hard to write up a working sample in VB.NET in this little text box, but I'm sure someone else will oblige.

[1] http://damieng.com/blog/2008/04/10/using-linq-to-foreach-over-an-enum-in-c

Kim Gräsman
A: 

Try using System.Enum to get the name of the value.

In your example, use: MsgBox(System.Enum.GetName(GetType(Net.NetworkInformation.IPStatus), status))

Nathan