Chuck
Chuck

Reputation: 139

C# method foreach

public string GetNetworkAdapterName()
    {
            foreach (ManagementObject queryObj in name.Get())
            {
                bool physicaladapter = Convert.ToBoolean(queryObj["PhysicalAdapter"]);
                if (physicaladapter == true)
                {
                    return Convert.ToString(queryObj["Name"]);
                }
            }
            return null;
    }

How can I get for example the first, second element of the foreach ? GetNetworkadapterName(1) is not working thanks

Upvotes: 1

Views: 667

Answers (4)

Dominating
Dominating

Reputation: 2930

you can use an integer like this:

   int i = 0;
    foreach (ManagementObject queryObj in name.Get())
    {
         i++;
         if(i < 3)
          {/*get element*/}            
          bool physicaladapter = Convert.ToBoolean(queryObj["PhysicalAdapter"]); 
            if (physicaladapter == true)
            {
                 return Convert.ToString(queryObj["Name"]);
            }  
    }
    return null; 

Upvotes: 1

nathan gonzalez
nathan gonzalez

Reputation: 11987

the current code will always return the first physical adapter. to modify it to allow you to tell it which to return, try something like this:

public string GetNetworkAdapterName(int Ordinal)
    {
            int i = 1;

            foreach (ManagementObject queryObj in name.Get())
            {
                bool physicaladapter = Convert.ToBoolean(queryObj["PhysicalAdapter"]);
                if (physicaladapter == true && i == Ordinal)
                {
                    return Convert.ToString(queryObj["Name"]);
                } else if (physicaladapter == true) { 
                    i++;
                }
            }
            return null;
    }

Upvotes: -1

Jon Skeet
Jon Skeet

Reputation: 1500065

It's very unclear what you're trying to do, but if the idea is to allow the caller to find all the network adapters, you can use LINQ (if you're using .NET 3.5 or higher):

public IEnumerable<string> GetNetworkAdapterNames()
{
    return name.Get()
               .Cast<ManagementObject>() // May not be necessary
               .Where(q => Convert.ToBoolean(q["PhysicalAdapter"]))
               .Select(q => Convert.ToString(q["Name"]));
}

If you're not using .NET 3.5, you could use an iterator block to do the same thing slightly more manually.

Then the caller can decide what to do with the sequence of adapter names - they could find the nth one, or print all of them out etc. This has the advantage that if they do want to access multiple elements, they can do so efficiently.

Getting to the nth value will always take iteration, but this can be hidden with a convenience method, such as LINQ's ElementAt method:

string secondAdapterName = GetNetworkAdapterNames().ElementAt(1);

I firmly believe that mixing the concern of "finding the nth value" with that of "finding all the network adapter names" is a bad idea.

Upvotes: 4

Pabuc
Pabuc

Reputation: 5638

public string GetNetworkAdapterName(int index)
{
    ManagementObject[] queryObjects = name.Get();
    if(index > queryObjects.Length)
        return String.Empty;
    ManagementObject queryObj = queryObjects[index];
    if(Convert.ToBoolean(queryObj["PhysicalAdapter"]))
        return queryObj["Name"].toString();
    else
        return String.Empty;      
}

Upvotes: 1

Related Questions