-
May 19th, 2020, 02:52 PM
#1
Thread Starter
Member
[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
-
May 19th, 2020, 03:06 PM
#2
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
-
May 19th, 2020, 03:40 PM
#3
Thread Starter
Member
Re: How to Compare 9 Digits
Are variables of type String
Code:
Dim value1 As String
Lbl1.text = value1
-
May 19th, 2020, 03:45 PM
#4
Re: How to Compare 9 Digits
What shaggy was asking was how did the numbers get into the labels. Can you show the
-
May 19th, 2020, 03:52 PM
#5
Thread Starter
Member
Re: How to Compare 9 Digits
Originally Posted by dbasnett
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
-
May 19th, 2020, 03:52 PM
#6
Thread Starter
Member
Re: How to Compare 9 Digits
sorry :
Code:
Dim value1 As String = Split(Txt_line1.Text, ";")(0)
Lbl1.text = value1
-
May 19th, 2020, 05:02 PM
#7
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:
Min=0
for each val in the list
if val <=Min then Min = val
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)
-
May 19th, 2020, 07:48 PM
#8
Re: How to Compare 9 Digits
Originally Posted by Delaney
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:
Dim values = Txt_line1.Text.Split(";"c) Dim numbers = Array.ConvertAll(values, Function(s) Convert.ToInt32(s)) 'or: 'Dim numbers = values.Select(Function(s) Convert.ToInt32(s)).ToArray() Dim min = numbers.Min() 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.
-
May 19th, 2020, 09:17 PM
#9
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.
-
May 19th, 2020, 09:33 PM
#10
Re: How to Compare 9 Digits
Originally Posted by Lord Orwell
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:
Module Module1 Sub Main() Dim arr = {1, 2, 3, 4, 5} Dim lst As New List(Of Integer) From {1, 2, 3, 4, 5} Console.WriteLine(arr.Contains(3)) Console.WriteLine(lst.Contains(3)) Console.WriteLine(arr.Contains(6)) Console.WriteLine(lst.Contains(6)) Console.ReadLine() End Sub 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.
Last edited by jmcilhinney; May 19th, 2020 at 09:38 PM.
-
May 20th, 2020, 05:26 AM
#11
Re: How to Compare 9 Digits
Originally Posted by jmcilhinney
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)
-
May 20th, 2020, 05:41 AM
#12
Re: How to Compare 9 Digits
Originally Posted by Delaney
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.
-
May 20th, 2020, 06:14 AM
#13
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)
-
May 20th, 2020, 06:25 AM
#14
Re: How to Compare 9 Digits
Originally Posted by Delaney
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.
-
May 20th, 2020, 11:39 AM
#15
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|