none
Specifying a range for a list input relative to previous list inputs RRS feed

  • Question

  • I'm trying to validate user input to a range which compares an input to previous inputs in a list, I can seem to find the syntax. I've written something indicative of what i'm trying to do, but the if statement that declares the range is obviously all wrong. My code reads as follows:

    List<Opening> OpeningsList = new List<Opening>();
    
    int MinRange = openingFromSOP + openingWidth + (int)StudCount() / 2 * (int)TimberThickness;
               
                if (openingFromSOP > MinRange.CompareTo(List<Opening> OpeningsList))
                {
                    OpeningsList.Add(new Opening() { OpeningFromSOP = openingFromSOP - StudCount() / 2 * TimberThickness, DistanceOverOpening = openingFromSOP + openingWidth + StudCount() / 2 * TimberThickness, OpeningWidth = openingWidth + StudCount() * TimberThickness, OpeningHeadHeight = openingHeadHeight, OpeningCillHeight = openingCillHeight, LintelTicker = flag2 });
                }
                else
                {
                    ContentDialog CillTooHigh = new ContentDialog
                    {
                        Title = "Invalid input",
                        Content = "The opening is within the bounds of another opening.",
                        CloseButtonText = "Ok"
                    };
                }

    Any help would be appreciated, cheers, Matthew

    Thursday, February 14, 2019 9:11 PM

Answers


  •  the place that I'm trying to implement the validation is on lines 349 and 351 of the MainPage.xaml.cs file within 'Millenium'. I just can't seem to get the logic right:

    I haven't analyzed your code in detail, but looking at line 349 which I believe is this one:

    //int MinRange = openingFromSOP + openingWidth + (int)StudCount() / 2 * (int)TimberThickness;
    

    my first concern is whether or not you are familiar with (well-versed in) the
    concepts of C# Operator Precedence, Associativity, and Order of Evaluation.

    These are critical to understanding how the compiler will handle statements
    such as the above. The result of the statement you have will be the same as
    if it were written:

    int MinRange = openingFromSOP + openingWidth + (((int)StudCount() / 2) * (int)TimberThickness);
    

    If that is not what you intended then you should add parentheses to force
    the order of operations to occur as you intended. It is often a good idea
    to add parentheses anyway for readability and so that someone looking at 
    your code knows exactly what you intended.

    Operator Precedence and Associativity in C# Programming
    https://www.programtopia.net/csharp/docs/operator-precedence-and-associativity

    Operator Precedence and Associativity
    http://www.informit.com/articles/article.aspx?p=23210&seqNum=12

    Operators (C# Programming Guide)
    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/operators

    C# operators
    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/

    Note as well that order of expression evaluation in C# may be different than 
    in C and C++. See:

    What is the order of evaluation in C#?
    https://blogs.msdn.microsoft.com/oldnewthing/20070814-00/?p=25593

    Precedence vs Associativity vs Order
    https://blogs.msdn.microsoft.com/ericlippert/2008/05/23/precedence-vs-associativity-vs-order/

    - Wayne

    • Marked as answer by matty1248 Saturday, February 23, 2019 10:19 PM
    Friday, February 22, 2019 7:49 AM
  • Greetings Matty.

    I can't follow your code at all. Are you trying to compare the position of a new opening with existing openings to see if there is any overlap?

    If so, where is the list of existing openings? You will probably need to loop through it comparing each one to the new one in turn, although it might be possible to do it using linq. In psuedo-code the non-linq version would look something like this.

    int newLeftEdge = WorkOutTheLeftLocationOfTheNewOpening(); int newRightEdge = WorkOutTheRightLocationOfTheNewOpening(); bool newIsLegit = true; foreach(Opening op in ListOfExistingOpenings) { int leftEdge = WorkOutLeftLocationOf(op); int rightEdge = WorkOutRightLocationOf(op); if(newLeftEdge >= leftEdge && newLeftEdge <= rightEdge) newIsLegit = false; // The left edge is inside an existing opening. if(newRightEdge <= rightEdge && newRightEdge >= leftEdge) newIslegit = false; // The right edge is inside an existing opening.

    if(newLeftEdge < leftEdge && newRightEdge > rightEdge)

    newIslegit = false; // The new opening completely encloses an existing opening. } if(newIslegit) { // Add the new opening to the list. } else { // Display an error message. }


    P.S. Do you work in the building industry? I ask because this is the sort of thing I do in my job.

    • Edited by Ante Meridian Thursday, February 14, 2019 10:15 PM forgot about the encloses clash
    • Marked as answer by matty1248 Saturday, February 23, 2019 10:19 PM
    Thursday, February 14, 2019 10:09 PM

  •     the place that I'm trying to implement the validation is on lines 349 and 351 of the MainPage.xaml.cs file within

    //if (openingFromSOP > MinRange.CompareTo(List<Opening> OpeningsList))

    I'm not clear on what you wanted or expected with this line of code.

    MinRange.CompareTo will return 0 if the compared ints are equal.

    It will return a value that is less than 0 - usually -1 - if MinRange is less
    than the compared to int.

    It will return a value that is greater than 0 - usually 1 - if MinRange is
    greater than the compared to int.

    I don't see what point there is in comparing that return value to openingFromSOP.

    Int32.CompareTo Method
    https://docs.microsoft.com/en-us/dotnet/api/system.int32.compareto?view=netframework-4.7.2#System_Int32_CompareTo_System_Int32_

    - Wayne

    • Marked as answer by matty1248 Saturday, February 23, 2019 10:18 PM
    Saturday, February 23, 2019 12:13 PM

All replies

  • Greetings Matty.

    I can't follow your code at all. Are you trying to compare the position of a new opening with existing openings to see if there is any overlap?

    If so, where is the list of existing openings? You will probably need to loop through it comparing each one to the new one in turn, although it might be possible to do it using linq. In psuedo-code the non-linq version would look something like this.

    int newLeftEdge = WorkOutTheLeftLocationOfTheNewOpening(); int newRightEdge = WorkOutTheRightLocationOfTheNewOpening(); bool newIsLegit = true; foreach(Opening op in ListOfExistingOpenings) { int leftEdge = WorkOutLeftLocationOf(op); int rightEdge = WorkOutRightLocationOf(op); if(newLeftEdge >= leftEdge && newLeftEdge <= rightEdge) newIsLegit = false; // The left edge is inside an existing opening. if(newRightEdge <= rightEdge && newRightEdge >= leftEdge) newIslegit = false; // The right edge is inside an existing opening.

    if(newLeftEdge < leftEdge && newRightEdge > rightEdge)

    newIslegit = false; // The new opening completely encloses an existing opening. } if(newIslegit) { // Add the new opening to the list. } else { // Display an error message. }


    P.S. Do you work in the building industry? I ask because this is the sort of thing I do in my job.

    • Edited by Ante Meridian Thursday, February 14, 2019 10:15 PM forgot about the encloses clash
    • Marked as answer by matty1248 Saturday, February 23, 2019 10:19 PM
    Thursday, February 14, 2019 10:09 PM
  •    Hey Ante, actually i replied to you the other day but you must not have seen, I work for Mitek too! :) I'm working as a QA for Mitek UK, trying to learn the code as fast as I can so I can help out with Pamir. Just trying to work in a field that I'm familiar with as I try to build my understanding of the code.

       Thanks for your response, I'll have to try it out and respond to it in substance tomorrow cause I'd best get some sleep and my ability to think is going downhill.

    #OneMitek

    Thursday, February 14, 2019 10:47 PM
  • I'm trying to validate user input to a range which compares an input to previous inputs in a list, I can seem to find the syntax.

    There are many, many ways of comparing "input to previous inputs in a list"...

    Why don't you write down, the specific comparison you want to perform?

    Any help would be appreciated, cheers, Matthew

    You must help first, otherwise, you'll get a slugfest of guesses...


    • Edited by ritehere44 Thursday, February 14, 2019 11:12 PM
    Thursday, February 14, 2019 11:11 PM
  • Greetings again.

    I did miss that reply in the earlier thread. I'm not an enthusiastic fan of that OneMiTek stuff, but at least we get doughnuts after being forced to watch those 'town halls'.

    You're probably curious, so I'll tell you my real name is Andrew McLennan (so my username has the same initials, Ante sounds like Andy (which a lot of people call me), and it's ironic because I hate mornings). My avatar is about ten years out of date, so it doesn't show the salt and pepper my beard has collected in the meantime.

    Good luck with your efforts.

    Friday, February 15, 2019 3:10 AM
  • Hey Andrew,

      We have the same feelings about OneMitek in Europe in truth, there's a healthy degree of scepticism. Although in Europe we are collaborating daily all across europe to make the software we make, which tends to all building codes in europe and South Africa, so in principal it seems feasible. My scepticism grew after the directors came through a couple weeks back, the whole strategy to make radical new products and collaborate globally seems totally flawed. 

      I'm Matthew Fyfe, by the way, if you want to look me up. Would be great to get you on skype too if you're up for it, not sure if there's some way to PM on here. 

      I'm still trying to implement this suggestion you made, hopefully I can suss it out this morning. There's no list as such, it's just generated by a click event where the width, heights and distance from setting out point are validated on each click, as below:

            public async void CreateOpening_Click(object sender, RoutedEventArgs e)  //this one
            {
                if (!int.TryParse(InputOpening1Width.Text, out int openingWidth)) { InputOpening1Width.Text = "0"; }
                if (!int.TryParse(InputHeadHeight1.Text, out int openingHeadHeight)) { InputHeadHeight1.Text = "0"; }
                if (!int.TryParse(InputOpening1FromSOP.Text, out int openingFromSOP)) { InputOpening1FromSOP.Text = "0"; }
                if (!float.TryParse(InputThickness.Text, out float TimberThickness)) { InputThickness.Text = "0"; }
                bool flag2 = Lintelticker.IsChecked.Value;
                float StudCount()
                {
                    if (flag2 == true)
                    {
                        double openingWidthParse = (double)openingWidth;
                        double studCount = Math.Ceiling(openingWidthParse / 50);
                        float a = (float)studCount;
                        return a;
                    }
                    else
                    {
                        return 0;
                    }
                }
                if (openingWidth == 0)
                {
                    ContentDialog CillTooHigh = new ContentDialog
                    {
                        Title = "Invalid input",
                        Content = "You must enter a width for your opening.",
                        CloseButtonText = "Ok"
                    };
                    ContentDialogResult result = await CillTooHigh.ShowAsync();
                    return;
                }            
                if (openingHeadHeight == 0)
                {
                    ContentDialog CillTooHigh = new ContentDialog
                        {
                            Title = "Invalid input",
                            Content = "You must enter a head height for your opening.",
                            CloseButtonText = "Ok"
                        };
                    ContentDialogResult result = await CillTooHigh.ShowAsync();
                    return;
                }           
    
                PanelPress();
                OpeningPress();
            }

    I'll try to implement your suggestion in here. Feels like I'm so close to finishing, but my code has got pretty weighty now and with so much bad practice.

    Cheers,

    Matthew

    Saturday, February 16, 2019 9:36 AM

  • Hi     matty1248,

    >>I'll try to implement your suggestion in here. Feels like I'm so close to finishing, but my code has got pretty weighty now and with so much bad practice.

    When you implement a method, it won't be perfect at first. You can slowly improve it later. For example, more succinct syntax and high efficiency.

    So, I suggest you can implement the function first, then optimize.

    Besides, if you have some  If I have any misunderstanding, you can upload a runnable demo to us(Including your test material and remove all private information). We can download it and debugging.

    Best Regards,

    Yong Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 18, 2019 2:41 AM
    Moderator
  • Hello Yong, 

       thanks for your response. I have been trying to build and refine as you suggest, in the case of this issue I just can't seem to get off the ground. I even built my program again from scratch, with some significant refinements, but when I get to this bit I just come to a standstill.

       I've tried to upload the code but it seems like I don't have the privelages to do this, either that or i'm trying to do it in the wrong place. Can you point me in the right direction for how i might do that?

      Many thanks,

      Matthew


    • Edited by matty1248 Monday, February 18, 2019 7:49 PM typo
    Monday, February 18, 2019 7:49 PM

  •    I've tried to upload the code but it seems like I don't have the privelages to do this, either that or i'm trying to do it in the wrong place. Can you point me in the right direction for how i might do that?


    Usually people create a ZIP archive of all of the project/solution folders.
    Then they upload it to OneDrive, mark it as sharable and then post a link
    to the ZIP so we can download it.

    If you haven't used OneDrive before, you should be able to sign in using
    the same username and password that you use for these forums.

    onedrive.com

    or 

    https://onedrive.live.com/about/en-us/

    - Wayne

    Monday, February 18, 2019 11:45 PM
  • Hi Wayne,

       I've attached the file, though it's quite messy, the place that I'm trying to implement the validation is on lines 349 and 351 of the MainPage.xaml.cs file within 'Millenium'. I just can't seem to get the logic right:

    https://drive.google.com/file/d/157yokK4kutY0saPIYCm2n_Xm9-R-B8s2/view?usp=sharing
    https://drive.google.com/file/d/1uvMEI2-JAUic_5JfrfJGwbsoUqTIVvhX/view?usp=sharing

    If you find the time to take a look that would be great, I'm just trying to get to the end of this one then I'm going to try to start using interfaces, abstract classes etc to find neater ways of doing things.

    Cheers, 

    Mathew


    Thursday, February 21, 2019 9:03 PM

  •  the place that I'm trying to implement the validation is on lines 349 and 351 of the MainPage.xaml.cs file within 'Millenium'. I just can't seem to get the logic right:

    I haven't analyzed your code in detail, but looking at line 349 which I believe is this one:

    //int MinRange = openingFromSOP + openingWidth + (int)StudCount() / 2 * (int)TimberThickness;
    

    my first concern is whether or not you are familiar with (well-versed in) the
    concepts of C# Operator Precedence, Associativity, and Order of Evaluation.

    These are critical to understanding how the compiler will handle statements
    such as the above. The result of the statement you have will be the same as
    if it were written:

    int MinRange = openingFromSOP + openingWidth + (((int)StudCount() / 2) * (int)TimberThickness);
    

    If that is not what you intended then you should add parentheses to force
    the order of operations to occur as you intended. It is often a good idea
    to add parentheses anyway for readability and so that someone looking at 
    your code knows exactly what you intended.

    Operator Precedence and Associativity in C# Programming
    https://www.programtopia.net/csharp/docs/operator-precedence-and-associativity

    Operator Precedence and Associativity
    http://www.informit.com/articles/article.aspx?p=23210&seqNum=12

    Operators (C# Programming Guide)
    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/operators

    C# operators
    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/

    Note as well that order of expression evaluation in C# may be different than 
    in C and C++. See:

    What is the order of evaluation in C#?
    https://blogs.msdn.microsoft.com/oldnewthing/20070814-00/?p=25593

    Precedence vs Associativity vs Order
    https://blogs.msdn.microsoft.com/ericlippert/2008/05/23/precedence-vs-associativity-vs-order/

    - Wayne

    • Marked as answer by matty1248 Saturday, February 23, 2019 10:19 PM
    Friday, February 22, 2019 7:49 AM

  •     the place that I'm trying to implement the validation is on lines 349 and 351 of the MainPage.xaml.cs file within

    //if (openingFromSOP > MinRange.CompareTo(List<Opening> OpeningsList))

    I'm not clear on what you wanted or expected with this line of code.

    MinRange.CompareTo will return 0 if the compared ints are equal.

    It will return a value that is less than 0 - usually -1 - if MinRange is less
    than the compared to int.

    It will return a value that is greater than 0 - usually 1 - if MinRange is
    greater than the compared to int.

    I don't see what point there is in comparing that return value to openingFromSOP.

    Int32.CompareTo Method
    https://docs.microsoft.com/en-us/dotnet/api/system.int32.compareto?view=netframework-4.7.2#System_Int32_CompareTo_System_Int32_

    - Wayne

    • Marked as answer by matty1248 Saturday, February 23, 2019 10:18 PM
    Saturday, February 23, 2019 12:13 PM
  • Thanks Wayne,

       I think I have some understanding of these concepts, though limited, I think the code has just got so heavy and full of mistakes and bad practice that I've gotten a little lost with it.

      The actual line itself is really bad, i know, I guess I just wanted to get this program finished and make a fresh start and roll in a more refined understanding and some new concepts. I'm going to read through all those articles and try to tackle the problem from scratch in the other attachment.

      The actual problem itself was just to validate that there are no clashes, with the new list item, within the horizontal bounds of the preceding list items. I should be able to sort it myself, there's no great value in getting someone else to answer it I guess, it's all within the scope of what I already know. I'll close this as answered and try to reduce the clutter in my new attempt.

      Thanks for your help,

      Regards,

      Matthew

    Saturday, February 23, 2019 10:18 PM
  • I'm trying to validate user input to a range which compares an input to previous inputs in a list,...

    You haven't defined your comparison criteria, and without it, your question is still unanswered. 

    Added: anyone can compare anything; it's just a matter of defining a comparison criteria.

    • Edited by ritehere44 Sunday, February 24, 2019 10:20 PM anyone can compare anything
    Sunday, February 24, 2019 10:10 PM
  • Hi,

     well the criteria is that, for openings subsequent to the first, the openingWidth + openingFromSOP must either be less than the openingFromSOP of any other given entry or the openingFromSOP must be greater than the openingWidth + openingFromSOP of the same entry, otherwise the dialog box will generate. I can kind of get my head round the comparison, it's more how to syntax that into the list and to edit the list.

    Cheers,

    Matthew

    Sunday, February 24, 2019 10:24 PM
  • Hi,

     well the criteria is that, for openings subsequent to the first, the openingWidth + openingFromSOP must either be less than the openingFromSOP of any other given entry or the openingFromSOP must be greater than the openingWidth + openingFromSOP of the same entry, otherwise the dialog box will generate. I can kind of get my head round the comparison, it's more how to syntax that into the list and to edit the list.

    Cheers,

    Matthew

    No, this is not the comparison criteria you should've specified in your OP.

    Notice that, you wrote in your OP: "...compares an input to previous inputs in a list..."

    And the comparison criteria above is yet to be defined.

    Moreover, this new criteria, seems more a charade, because, firstly you say, with words, this inequation:

                openingWidth + openingFromSOP < openingFromSOP

    Right after, you say, with words, this another inequation:

                openingFromSOP > openingWidth + openingFromSOP

    Have you noticed that both inequations are identical?

    Also, it's important to notice that, probably, those variables above are not singular/scalar values, because their values depend on the input order index.

    But this we have to guess too, because you did not specify it, yet.

    In my opinion, this thread is another fake thread, posted to promote some moderator, who has not appeared...

    Tuesday, February 26, 2019 1:40 AM
  • Well sorry for that ritehere44, I guess I'm not the best communicator
    Sunday, March 3, 2019 10:00 PM