Question Validate Items in a list

shawnrye3

New member
Joined
Apr 14, 2022
Messages
2
Programming Experience
5-10
So does anyone know how I would validate the following lists to make sure that they do not overlap one another I do have the validation working to check first char and last char also
I have validation to make sure it doesn't already exist in the DB example if they enter a-g and try to submit they will get an error because it already exists, however in this scenario
if they enter b-f how would I validate it because a-g already covers that scenario?
I have this fixed for two groups, by passing in the alphaid for example a is 1 and b is 2 and so on, however I can only get this to work for 2 groups but not if you have 3 or more.

a-g
h-k
l-p
q-z
 
Part of the issue here is that your data is "stringly-typed". You have strings, but you seem to have assigned semantic value to the strings where there is significance to what the first and last characters are within the string. If you take steps to break out of being stringly-typed and actually be strongly-typed things become much easier.

Consider a class that looks like:
C#:
class AlphaRange
{
    public char Start { get; set; }
    public char End { get; set; }
}

Your list can then be:
C#:
var list = new List<AlphaRange>()
{
    new AlphaRange { Start = 'a', End = 'g' },
    new AlphaRange { Start = 'h', End = 'k' },
    new AlphaRange { Start = 'l', End = 'p' },
    new AlphaRange { Start = 'q', End = 'z' },
};

You can then add a method to AlphaRange that looks something like:
C#:
public bool IsBetween(char ch)
{
    return Start <= ch && ch <= End;
}

You can then go through the list and check to see if there is any overlap. Something like this pseudo-code:
C#:
foreach(var needle in list)
{
    foreach(var haystack in list)
    {
        if (needle != haystack)
        {
            if (haystack.IsBetween(needle.Start) || haystack.IsBetween(needle.End))
                we have a collision
        }
    }
}
 
Last edited:
Part of the issue here is that your data is "stringly-typed". You have strings, but you seem to have assigned semantic value to the strings where there is significance to what the first and last characters are within the string. If you take steps to break out of being stringly-typed and actually be strongly-typed things become much easier.

Consider a class that looks like:
C#:
class AlphaRange
{
    public char Start { get; set; }
    public char End { get; set; }
}

Your list can then be:
C#:
var list = new List<AlphaRange>()
{
    new AlphaRange { Start = 'a', End = 'g' },
    new AlphaRange { Start = 'h', End = 'k' },
    new AlphaRange { Start = 'l', End = 'p' },
    new AlphaRange { Start = 'q', End = 'z' },
};

You can then add a method to AlphaRange that looks something like:
C#:
public bool IsBetween(char ch)
{
    return Start <= ch && ch <= End;
}

You can then go through the list and check to see if there is any overlap. Something like this pseudo-code:
C#:
foreach(var needle in list)
{
    foreach(var haystack in list)
    {
        if (needle != haystack)
        {
            if (haystack.IsBetween(needle.Start) || haystack.IsBetween(needle.End))
                we have a collision
        }
    }
}
I tried this but it always returns true
 
Last edited by a moderator:
Post your code.
 
Back
Top Bottom