C# foreach loop for true boolen

elfenliedtopfan5 121 Reputation points
2021-01-31T15:09:06.733+00:00

Good Afternoon,

i have created a program that a class will check if bool is true or false but the issue i have is a simple,

if(elfbool1 == true )
{
then do something
}

this works fine but i want to check each bool at once as i want each checkbox to be checked before anything happens,

and dont really want to do multiple if statements to do this,

ideally i would like a for each loop to accomplish this,
as each one that is true will end up setting a file for download so a foreach one would work correctly

as it will que the download link its,

just im not quite sure how to get all ones by name as i have vlc, winrar, ect and if both those are selected it will setup a download link for them,

but i have like 50 plus bools and still growing so it would like to find out what values are true,

if they are true i can gen a link for them,

my bools are public static bool vlc {get; set;)
public static bool winrar {get; set;)

any help would be much appreciated

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,842 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,401 Reputation points
    2021-01-31T17:32:47.207+00:00

    Hello,

    The basic idea for checking many bool values is with the .Any extension. The following code samples were done with VS2019, C# 9, .NET 5.

    • Button one is against a bool array
    • Button two is against a property in a list of a class named Item
    • Button three is against CheckBox controls directly on a form
    • BooleanExtensions is for demo purposes only
    • I used three items in each demo but the number can be any number greater than 1 to work.

    Code

    For a real app, Item class and BooleanExtensions class would be in their own class files.

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace BoolCheck
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                bool[] values1 = {true, false, true};
    
                Debug.WriteLine(values1.Any(booItem=> booItem is false) ? 
                    "Not all true" : 
                    "All true");
    
                values1 = new[] { true, true, true };
                
                Debug.WriteLine(values1.Any(boolItem => boolItem is false) ? 
                    "Not all true" : 
                    "All true");
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                var items = new List<Item>
                {
                    new() {Id = 1, Vlc = true}, 
                    new() {Id = 2, Vlc = false}, 
                    new() {Id = 3, Vlc = true}
                };
    
                Debug.WriteLine(items.Any(item => item.Vlc is false) ? 
                    "Not all true" : 
                    "All true");
    
                var notTrueItems = items
                    .Select((item, index) => new {Item = item, Index = index})
                    .Where(data => data.Item.Vlc is false)
                    .ToList();
                
                for (int index = 0; index < notTrueItems.Count; index++)
                {
                    notTrueItems[index].Item.Vlc = true;
                }
    
                Debug.WriteLine(items.Any(item => item.Vlc is false) ? 
                    "Not all true" : 
                    "All true");
                
                
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                var checkBoxes = !Controls.OfType<CheckBox>().Any(cb => cb.Checked is false);
                Debug.WriteLine($"CheckBoxes all checked?: {checkBoxes.ToYesNoString()}");
            }
        }
    
        public class Item
        {
            public int Id { get; set; }
            public bool Vlc { get; set; }
        }
        public static class BooleanExtensions
        {
            public static string ToYesNoString(this bool value) => value ? "Yes" : "No";
        }
    }
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.