简体   繁体   中英

C# if hostname contains string then check array list

I am writing a program for work and when it initializes it goes through a series of if/else statements to check the computer name based on the building it is at, then when the statements find a match, it will print the building location onto a label in my form.

I am wanting to optimize this process by using an array list instead of 20+ if/else statements, and I am having an issue with it. Here is the code below:

string hostName = Environment.MachineName;
string[] buildingCode = {"B1","B2","B3","B4"};
string[] buildingName = {"Building 1", "Building 2", "Building 3", "Building 4"};

if (hostName.Contains(buildingCode))
{
     Console.Write(buildingName);
}
else
{
     Console.Write("Error!");
}

How I want it to work is that it will check the computer's hostname to see if it contains a string value from the buildingCode array. Then once it finds a match, it will use a string value from the buildingName array. But the Error List is displaying this error:

CS1503 Argument 1: cannot convert from 'string[]' to 'string'

I am still a slight noob at coding with C#, but I am learning a lot as I use this language and I try to avoid asking for help, but I am stumped. Any feedback/input would be greatly appreciated, thanks in advance.

You are probably using wrong data structures that's causing confusion. Event if your building codes contain host name, how will you decide which building name corresponds to that code!

You can instead use Dictionary that stores building code/name as key/value.

string hostName = "B1";

var buildings = new Dictionary<string, string> {
    { "B1", "Building 1" },
    { "B2", "Building 2" },
    { "B3", "Building 3" },
    { "B4", "Building 4" }
};

if (buildings.ContainsKey(hostName)) {
    Console.Write(buildings[hostName]);
}
else {
    Console.Write("Error!");
}

PS While this solves your problem, I can't imagine how MachineName is going to match a building in reality!

Update : Since OP is learning C#, here's fiddle for him to play with.

First of all you cant compare string Array to String

String[] = string Array

That's mean :

buildingCode[0] = B1;

buildingCode[1] = B2;

buildingCode[3] = B3;

buildingCode[4] = B4;

In your Code will be something like this :

        string hostName = Environment.MachineName;
            string[] buildingCode = {"B1","B2","B3","B4"};
            string[] buildingName = {"Building 1", "Building 2", "Building 3", "Building 4"};

            if (hostName.Contains(buildingCode[0]))
            {
                Console.Write(buildingName[0]);
            }
            else
            {
                Console.Write("Error!");
            }

You Might need to put into the Loop to check all the array

you cant write if(string contains array) You need a for-loop within a if. like this:

for (int i = 0; i <= stringname.length; i++)
        {
            if (string.Contains(stringname[i]))
            {
                Console.WriteLine(buildingName[i]);
                //Do something
            }
        }

Please check below. Array.Exists will check the hostName exist in buildingCode or not. Array.IndexOf will give you index of the match then you can get your buildingName based of the index.

Code:

using System;

public class Program
{
    public static void Main()
    {
        string hostName = "B1";
        string[] buildingCode = {"B1","B2","B3","B4"};
        string[] buildingName = {"Building 1", "Building 2", "Building 3", "Building 4"};

        if (Array.Exists(buildingCode, element => element == hostName))
        {
             Console.Write(buildingName[Array.IndexOf(buildingCode, hostName)]);
        }
        else
        {
             Console.Write("Error!");
        }
    }
}

Please check output in DotNetFiddle .

You are comparing two different types, Contains method get a single string and not the array of string. Your code should look like as

 string hostName = Environment.MachineName;
        string[] buildingCode = { "B1", "B2", "B3", "B4" };
        string[] buildingName = { "Building 1", "Building 2", "Building 3", "Building 4" };
     if (buildingCode.Where(t => t.Contains(hostname)).Any())
     {
          Console.Write(buildingName[
          Array.IndexOf(buildingCode,buildingCode.Where(t => t.Contains(hostname))
         .FirstOrDefault())]);
     }

find here: ActualCode

You can not check whole array with Contains function. if you wish to use Contains function you need to check each element of array

use if (buildingCode.Any(x => x.Contains(hostName))) instead of if (hostName.Contains(buildingCode))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM