0

My English is not good. So i tired to write if condition with each item in array. What is solution for this. Thank you for you help This code make me tired , so how to do it like this

public bool AllowElement(Element element)// element is object by mouse houver on Revit program
    {
        BuiltInCategory[] BIC  = new BuiltInCategory[]{Frame, Column, Slab,Foundation, etc...}// BIC is 
        in enum, and this is declare with params key word
        if( ele ==  (int)BIC.Frame || ele ==  (int)BIC.Column ||
            ele ==  (int)BIC.Slab || ele ==  (int)BIC.Foundation ||
            ele == (int)BIC.etc....)
        {
            return true
        }
    }

end this code I writed but i feel it not true

public bool AllowElement(Element element)
    {
        int ele = element.Category.Id.IntegerValue;
        foreach (var item in BIC)
        {
            if (ele == (int)item)
                return true;

        }
        return false;
    }
6
  • Hard to tell hat your asking based on your sample, You could try Exists, IndexOf, for an Array or if it's an Enum than IsDefined Commented Mar 7, 2020 at 15:19
  • 1
    But how to nest a condition into an array?. Commented Mar 7, 2020 at 15:25
  • 3
    Does this answer your question? Value is in enum list Commented Mar 7, 2020 at 15:36
  • 2
    You could use .Any to return a bool if any entry in the list matches a condition you specify. Commented Mar 7, 2020 at 15:42
  • 1
    Change the second code you have posted. If BIC is an enum then try if (Enum.IsDefined(typeof(BIC), ele)) { return true; } Commented Mar 7, 2020 at 17:37

1 Answer 1

2

Have you tried using Linq for validating if the item exists?

    public bool AllowElement(Element element)
    {
        return BIC.Any(x => (int)x == element.Category.Id.IntegerValue);
    }

Any() method returns true if there is any element that matches the condition Microsoft docs on Any

This is with assumption that BIC is in scope of this method.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.