Results 1 to 15 of 15

Thread: [RESOLVED] How to Compare 9 Digits

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2017
    Posts
    37

    Resolved [RESOLVED] How to Compare 9 Digits

    Hi friends,

    I have 9 labels which contain positive or negative numbers

    I can't compare the 9 values ​​between them in order to define the maximum value and the minimum value (Lbl1.text to Lbl9.text)
    Do you have an idea ? is there a function for that?
    Thank you for your ideas

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,047

    Re: How to Compare 9 Digits

    Where did the numbers in the labels come from? That's what you should be comparing. If you compare strings, you get string comparisons, which won't do what you want. You COULD take the label text, convert them all to numbers, and compare that, but since the labels had to have been filled from something, it would be more efficient to work with the numbers before they were put into the labels. If they were strings before, then it barely matters, of course, but most likely, they were NOT strings before, in which case you save some time and hassle to do the work before they are put in the labels, or at the same time.

    So, where did the values in the labels come from?
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2017
    Posts
    37

    Re: How to Compare 9 Digits

    Are variables of type String

    Code:
    Dim value1 As String 
    Lbl1.text = value1

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: How to Compare 9 Digits

    What shaggy was asking was how did the numbers get into the labels. Can you show the
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2017
    Posts
    37

    Re: How to Compare 9 Digits

    Quote Originally Posted by dbasnett View Post
    What shaggy was asking was how did the numbers get into the labels. Can you show the
    sorry :

    Code:
    Dim value1 As String = Split(Txt_line1.Text, ";")(0)
    Lbl1.text = value1

  6. #6

    Thread Starter
    Member
    Join Date
    Jul 2017
    Posts
    37

    Re: How to Compare 9 Digits

    sorry :

    Code:
    Dim value1 As String = Split(Txt_line1.Text, ";")(0)
    Lbl1.text = value1

  7. #7
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: How to Compare 9 Digits

    hello,

    So your values are strings, so you need to convert them : use Cint() or Cdbl() or Cdec(), depending of what they are, to convert them. Then put all your values in a list(of ).

    After that, to have the min :

    Algo Code:
    1. Min=0
    2. for each val in the list
    3. if val <=Min then Min = val
    4. next

    I let you guess for the Max

    regards
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: How to Compare 9 Digits

    Quote Originally Posted by Delaney View Post
    Then put all your values in a list(of ).
    I don't know why everyone is obsessed with using collections when they are not required. The OP already has a String array courtesy of Split. There's no reason to create a List(Of Integer) when the point of such a collection is the ability to add and remove items and that is not needed here. A List(Of Integer) contains an Integer array internally anyway, so you're not saving anything. An array is still an IEnumerable(Of Integer) so you can still use it as a source for LINQ queries and the like. Firstly, the OP should use the correct Split method, then convert the String array to an Integer array, then (unless this is homework and a loop is required) use LINQ to get max and min:
    vb.net Code:
    1. Dim values = Txt_line1.Text.Split(";"c)
    2. Dim numbers = Array.ConvertAll(values, Function(s) Convert.ToInt32(s))
    3. 'or:
    4. 'Dim numbers = values.Select(Function(s) Convert.ToInt32(s)).ToArray()
    5. Dim min = numbers.Min()
    6. Dim max = numbers.Max()
    Of course, this question has been asked and answered many times, so anyone who is asking it again hasn't really looked for an answer already.

  9. #9
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,621

    Re: How to Compare 9 Digits

    I did just recently come across an issue where i had to convert an integer array into a list because .contains wouldn't work in a linq query with it. Good info. I would have said something similar if you hadn't.
    My light show youtube page (it's made the news) www.youtube.com/@artnet2twinkly
    Contact me on the socials www.facebook.com/lordorwell

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: How to Compare 9 Digits

    Quote Originally Posted by Lord Orwell View Post
    I did just recently come across an issue where i had to convert an integer array into a list because .contains wouldn't work in a linq query with it.
    No you didn't. I'm not sure what you thought the issue was but it wasn't actually the issue you thought it was. The List(Of T) class has it's own Contains member method but the LINQ Contains extension method extends the IEnumerable(Of T) interface and an array implements that interface the same as a List(Of T) does. I'd be interested to know the context so I can see exactly what you thought the issue was and what the actual issue was. Maybe I'm wrong but I can't see how, not because I'm infallible but because of what I understand about those Contains methods.

    E.g.
    vb.net Code:
    1. Module Module1
    2.  
    3.     Sub Main()
    4.         Dim arr = {1, 2, 3, 4, 5}
    5.         Dim lst As New List(Of Integer) From {1, 2, 3, 4, 5}
    6.  
    7.         Console.WriteLine(arr.Contains(3))
    8.         Console.WriteLine(lst.Contains(3))
    9.         Console.WriteLine(arr.Contains(6))
    10.         Console.WriteLine(lst.Contains(6))
    11.  
    12.         Console.ReadLine()
    13.     End Sub
    14.  
    15. End Module
    In that case, the calls on the List are it's member method while the calls on the array are the extension method, which you can confirm by mousing over them. All results are as expected though.

  11. #11
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: How to Compare 9 Digits

    Quote Originally Posted by jmcilhinney View Post
    I don't know why everyone is obsessed with using collections when they are not required.
    In fact, I always never use Array as in most of my applications the data are dynamic and I never know in advance how much points or values I will have.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: How to Compare 9 Digits

    Quote Originally Posted by Delaney View Post
    In fact, I always never use Array as in most of my applications the data are dynamic and I never know in advance how much points or values I will have.
    You should always use the right tool for the job. Collections when you need them and arrays when you don't. Given that you suggested a collection in this case, where an array was more appropriate, I suspect that you need collections less often than you think you do. I would think that I'd probably use arrays and collection pretty much equally but it could be a bit either way.

  13. #13
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: How to Compare 9 Digits

    I must confess that I didn't know the max and min method for the array which explain the solution I proposed . (that tells you my skills level...).
    At least I learned a new thing.

    Regards
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: How to Compare 9 Digits

    Quote Originally Posted by Delaney View Post
    I must confess that I didn't know the max and min method for the array which explain the solution I proposed . (that tells you my skills level...).
    At least I learned a new thing.

    Regards
    There's always more to learn. We can't use stuff that we don't know about and I've posted plenty of suboptimal solutions in my time.

  15. #15

    Thread Starter
    Member
    Join Date
    Jul 2017
    Posts
    37

    Re: How to Compare 9 Digits

    Thank you for your help you helped me to adjust the pb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width