0

I seem to have an nested boolean which seem to cause trouble for me...

The purpose of the boolean is suppose to check whether an attribute specification it is set for type a or type b?

examaple:

public Enum DateType 
{
    Integer, 
    Boolean
}

public class Specification 
{
    public int? maxInt {get; set;}
    public int? minInt {get; set;}
    public string? trueText {get; set;}
    public string? falseText {get; set;}
}

public class Attribute 
{
    public DataType Type {get; set;}
    public Specification TypeSpecification {get; set;}
}

I then have nested validation for this a validation for this

public static bool IsSpecificationValid(this Attribute newAttribute, DataType type)
{
    switch(type)
    {
        case DateType.Boolean:
            var isSpecifationValid =
            newAttribute.isBoolean() is true &&
            newAttribute.isInteger() is false &&
            return isSpecifationValid; 

        case DateType.Integer:
            var isSpecifationValid =
            newAttribute.isBoolean() is false &&
            newAttribute.isInteger() is true &&
            return isSpecifationValid; 
    }
}

public static bool IsNumber(this Specification newAttribute)
{
    return newAttribute.maxInteger != null && newAttribute.minInt != null && newAttribute.maxInt> newAttribute.minInt;
}

public static bool IsBoolean(this Specification newAttribute)
{
    return newAttribute.falseText != null && newAttribute.TrueOptionText != null;
}

So problem is when I have this

Attribute a = new Attribute 
{
    Type = DataType.Integer,
    TypeSpecification = new Specification 
    {
            maxInt = 10,
            minInt  = 2,
            trueText = "Incodrre"
    }
}

And execute a.IsSpecificationValid(a.Type) it return as true... which I do get, but cant semantically understand how i make it return false? what am I doing wrong? since trueText is part of a boolean but isBoolean should have been givien false?

Removing the condition that I want it give false is also incorrect, sine nothing is avoiding me to set the values, so Incase that both trueText and falseText has been set the IsBoolean condition will return true and then trigger a false statement.

I want to ensure that when Attribute is created that the specification for that type is only filled.

kafka
  • 573
  • 1
  • 11
  • 28
  • Without a false option text the boolean check will return false, which is what you're expecting for an integer type. – Jonathon Chase Mar 06 '20 at 18:13
  • I am expecting the isBoolean to fail, and isNumber to return true and false && true = false ? @JonathonChase – kafka Mar 06 '20 at 18:15
  • @JonathonChase I want in the switch case specify what it can be, and what it definitly cannot be – kafka Mar 06 '20 at 18:16
  • @JonathonChase since I am expecting false , the statement `newAttribute.isBoolean() is false` becomes true, and the setting boolean variabel becomes true? – kafka Mar 06 '20 at 18:21
  • correct, because you have only populated one of the strings it is checking. If you populate both in your example, you will get back false. – Jonathon Chase Mar 06 '20 at 18:23
  • ok? So how do I make it work for Partial conditions? – kafka Mar 06 '20 at 18:24
  • 1
    use a boolean OR instead of AND `falseText != null || trueText != null` – Jonathon Chase Mar 06 '20 at 18:26

0 Answers0