1

I'm trying to assign multiple abbreviations from a text box to a string variable but I can't figure out how. This is what I have so far.

string abbreviation = | "S" | "HC" | "HNC";
if (txtEmployeeType.Text != abbreviation)

It gives me an error under the first Or operator. What I'm trying to do is say that if someone doesn't type an S, HC, or HNC into the Employee Type text box, an error message box will show. But I'm just trying to figure out how to type the string variable.

1

2 Answers 2

1

Use an array and .Contains

string[] abbreviatons = new string[] { "S", "HC", "HNC" };
if  (!abbreviations.Contains(txtEmployeeType.Text)) { /*do something*/ };
Sign up to request clarification or add additional context in comments.

Comments

0

To check that a string equals a string in a collection of string, do the following with LINQ:

using System;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var input = "abcd";
            string[] arguments = {"S", "HC", "HNC"};
            var isValid = IsValid(input, arguments);
        }

        private static bool IsValid(string text, params string[] arguments)
        {
            return arguments.Any(s => s == text);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.