-1

I have a xml with many Ip addresses. Like this:

<settings>
  <ipaddresses>
    <ipaddress>1288</ipaddress>
    <ipaddress>1999</ipaddress>
    <ipaddress>555</ipaddress>
    <ipaddress>88888</ipaddress>
  </ipaddresses>
</settings>

And Now I want to check if one of the Ip Addresses match the Ip address of a pc. So that that user is allowed to see the product.

If the one of the Ipaddresses in the XML not match the Ipaddress of the pc, then a error message has to return. I try it like this:

 XDocument doc = XDocument.Parse(product.AuthenticationSettings);
            var IpAddress = doc.Descendants("ipaddress");
            foreach (var IpAddresses in IpAddress)
            {
                bool IppAddressMatch = false;

                if (GetClientIp() == IpAddresses.Value)
                {
                    IppAddressMatch = true;
                }

                if (GetClientIp() != IpAddresses.Value)
                {
                    // log message
                   return Content("<h1>403 Forbidden</h1>", "text/html");


                }
            }

But I get this error:

Error 6 Warning as Error: The variable 'IppAddressMatch' is assigned but its value is never used

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
InfinityGoesAround
  • 1,011
  • 4
  • 17
  • 31

2 Answers2

3

The code should probably be something like:

XDocument doc = XDocument.Parse(product.AuthenticationSettings);
var IpAddress = doc.Descendants("ipaddress");

bool IppAddressMatch = false;

foreach (var IpAddresses in IpAddress)
{
    if (GetClientIp() == IpAddresses.Value)
    {
        IppAddressMatch = true;
        break;
    }
}

if (!IppAddressMatch)
{
    // log message
    return Content("<h1>403 Forbidden</h1>", "text/html");
}

The point is: if the address of the PC is contained in the XML, then everything is OK, but if the address of the PC isn't contained in the XML, then error. Clearly when you find one matching address, you can stop looking for matching addresses (the break)

xanatos
  • 109,618
  • 12
  • 197
  • 280
-1

You have two option to solve this:

  1. Remove the IppAddressMatch variable, because you are not using it.
  2. Change the "treat warnings as errors" option. See Warning as error - How to rid these
Community
  • 1
  • 1
Ignacio
  • 806
  • 1
  • 10
  • 29
  • I don't think switching `treat warnings as errors` off is a good idea, this project was more than likely setup by a more senior developer, and he switched the option on for a reason – 3dd Aug 13 '15 at 08:25
  • And how do you know that? Maybe he do not know that this option was activated. When you are writing code sometimes is useful to have this disable, so you can debug the code without having it finished. – Ignacio Aug 13 '15 at 08:28