Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

174
Visualizações
Can you create a list based on the output of bools from another class?

Edited for (hopefully clarity):

What I am looking to do (and not sure if it can be done) is in this order:

  1. Determine if variables are/are not present (eg, "Do you have a mouse? Do you have a keyboard? Do you have Speakers? etc.)
  2. Using the output from #1, create a list that incorporates the outputs from the are/are not statement (eg, Do you have a mouse? Yes? Okay Add mouse to the list. Do you have a keyboard? No? Okay, omit the keyboard from the same list as the mouse and do not add it to another list. Do you have speakers? Yes? Add speakers to the same list that the mouse is in.
  3. Be able to reference this list at a later point.

If the code below confuses you, please don't let it and instead look at #1-3 instead.

Edit2: This is for C# w/ Target Framework .NET Framework 4.7.2, Output type Class Library(.dll), in case that changes how the code needs to be written.

public class AreFooPresent
{
     public static bool foo0 = false
     public static bool foo1 = false
     public static bool foo2 = false
}

public class AreTheyThereList
    {
        List<FooList> myList = new List<FooList>
        {
            myList.Add(new FooList(if return foo0 != false));
            myList.Add(new FooList(if return foo1 != false));
            myList.Add(new FooList(if return foo2 != false));
        }
    }
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Another one using linq

using System.Linq;
...
var list = (new bool[] {AreFooPresent.foo0, AreFooPresent.foo1, AreFooPresent.foo2})
    .Where(b => b).ToList();

And I see you initializing some object FooList, in this case something like

using System.Linq;
...
var list = (new bool[] {AreFooPresent.foo0, AreFooPresent.foo1, AreFooPresent.foo2})
    .Where(b => b)
    .Select(b => new FooList(b)).ToList();
over 4 years ago · Santiago Trujillo Relatório

0

welcome to SO! Something simple like the code below does the job. It'll only add to the list IF it's true and as they're static members, you can reference the class then the static members.

Then at the end, it'll return the list of positive bools (if any) - if not, it'll be an empty list.

    public class AreFooPresent 
    {
      public static bool foo0 = false
      public static bool foo1 = false
      public static bool foo2 = false
    }
    
    public class AreTheyThereList 
    {
      public List<bool> MyPositiveFooCollectionMethod() 
      {
        List<bool>myList = new List<FooList>();
    
        if (AreFooPresent.foo0) myList.Add(AreFooPresent.foo0);
        if (AreFooPresent.foo1) myList.Add(AreFooPresent.foo1);
        if (AreFooPresent.foo2) myList.Add(AreFooPresent.foo2);
    
        return myList;
      }
    }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda