-
Nov 23rd, 2016, 04:27 AM
#1
Thread Starter
Lively Member
Math functions in VB
I have two questions:
I'm aware that Math.Min() will only accept two values, but I need to get the minimum value from the contents of ten textboxes; so, is there a Math function that will allow me to do this or is there a better way to do it?
Also, how do I get round the problem of showing the minimum value as zero if any of the textboxes contain zeros or blanks?
Hope you can help.
-
Nov 23rd, 2016, 05:00 AM
#2
Re: Math functions in VB
You can put your values into an array or list and then use the Enumerable.Min extension method, e.g.
vb.net Code:
Dim textBoxes = Me.Controls.OfType(Of TextBox)() Dim numbers = textboxes.Where(Function(tb) Integer.TryParse(tb.Text, Nothing)).Select(Function(tb) Integer.Parse(tb.Text)) Dim minNumber = numbers.Min()
-
Nov 23rd, 2016, 08:54 AM
#3
Thread Starter
Lively Member
Re: Math functions in VB
Thanks but I'm still unsure. The textboxes from which I need to extract the minimum number are tbAAH, tbCol, tbDE, tbLex and tbUni; so where do I fit these into your code?
-
Nov 23rd, 2016, 11:51 AM
#4
Re: Math functions in VB
Instead of
Dim textBoxes = Me.Controls.OfType(Of TextBox)()
Try
Dim textBoxes() as TextBox = {tbAAH, tbCol, tbDE, tbLex, tbUni}
-
Nov 23rd, 2016, 03:05 PM
#5
Thread Starter
Lively Member
Re: Math functions in VB
Thanks. I now have the following:
CODE
Dim textBoxes() As TextBox = {tbAAH, tbCol, tbDE, tbLex, tbUni}
Dim numbers = textBoxes.Where(Function(tb) Integer.TryParse(tb.Text, Nothing)).Select(Function(tb) Integer.Parse(tb.Text))
Dim minNumber = numbers.Min()
CODE
I get the following message:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Core.dll
Additional information: Sequence contains no elements
So presumably something needs to be put in the brackets after Dim minNumber = numbers.Min(); but what should be in there?
-
Nov 23rd, 2016, 03:30 PM
#6
Re: Math functions in VB
Originally Posted by Pianoman23
Thanks. I now have the following:
CODE
Dim textBoxes() As TextBox = {tbAAH, tbCol, tbDE, tbLex, tbUni}
Dim numbers = textBoxes.Where(Function(tb) Integer.TryParse(tb.Text, Nothing)).Select(Function(tb) Integer.Parse(tb.Text))
Dim minNumber = numbers.Min()
CODE
I get the following message:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Core.dll
Additional information: Sequence contains no elements
So presumably something needs to be put in the brackets after Dim minNumber = numbers.Min(); but what should be in there?
You would get that error if none of the textboxes can be parsed as a number, because the numbers collection would be empty. You could prevent the exception by checking the numbers count, e.g.
Code:
If numbers.Count > 0 Then
Dim minNumber = numbers.Min()
Else
MessageBox.Show("no minimum number")
End If
but maybe the lambda expression could be improved to allow for this situation.
BB
Last edited by boops boops; Nov 23rd, 2016 at 03:36 PM.
-
Nov 23rd, 2016, 03:50 PM
#7
Thread Starter
Lively Member
Re: Math functions in VB
I really don't know what I'm doing here. I'm now up to the following code but still getting errors
Code:
Dim textBoxes() As TextBox = {tbAAH, tbCol, tbDE, tbLex, tbUni}
Dim numbers = textBoxes.Where(Function(tb) Integer.TryParse(tb.Text, Nothing)).Select(Function(tb) Integer.Parse(tb.Text))
Dim minNumber = numbers.Min()
If numbers.Count > 0 Then
Dim minNumber = numbers.Min()
Else
MessageBox.Show("no minimum number")
End If
MsgBox(minNumber)
Code
-
Nov 23rd, 2016, 05:22 PM
#8
Re: Math functions in VB
You can't write MsgBox(minNumber) because in your present code minNumber isn't declared outside the If block. I wrote it that way to avoid letting minNumber=0 if all your textboxes are empty; instead you get a message.
Here's an alternative way of writing the code which I hope you'll find easier to understand.
Code:
Dim textBoxes() As TextBox = {tbAAH, tbCol, tbDE, tbLex, tbUni} 'make an array of your textboxes
Dim numbers As New List(Of Integer)
Dim minNumber As Integer
For Each tb As TextBox In textBoxes
Dim n As Integer
If Integer.TryParse(tb.Text, n) Then
numbers.Add(n) 'if the textbox contains a number, add it to the list
End If
Next
If numbers.Count > 0 Then
'at least one textbox contains number
minNumber = numbers.Min
MessageBox.Show(minNumber.ToString)
Else
'none of the text boxes contains a number
minNumber = Integer.MinValue
MessageBox.Show("no numbers found in text boxes")
End If
This time minNumber gets the value of Integer.MinValue (something less than - 2 billion) instead of 0 when none of the textboxes contains a number. It has to have some value, but the idea is that you just ignore it.
By the way, please enclose your code in tags with square brackets like this: [code] ... [/code]. Or select a block of code in the forum message editor and hit the # button.
BB
Last edited by boops boops; Nov 23rd, 2016 at 05:25 PM.
-
Nov 23rd, 2016, 05:52 PM
#9
Thread Starter
Lively Member
Re: Math functions in VB
Thanks. I'll remember the tip about displaying code.
I've inputted the code. I'm not getting any error messages but even though tbAAH contains 14.67 and tbCol contains 14.07 (the others are empty) the messagebox says "no numbers found in text boxes".
-
Nov 24th, 2016, 01:21 AM
#10
Re: Math functions in VB
That's because everyone assumed the numbers would be integers. Just change Integer to Double everywhere it occurs in the above code.
BB
-
Nov 24th, 2016, 01:46 AM
#11
Re: Math functions in VB
Originally Posted by boops boops
That's because everyone assumed the numbers would be integers.
I didn't assume that. I just provided an example in which the numbers would be Integers. Unfortunately, too many people think that an example is something that you can copy and paste and expect to work as is, rather than a demonstration of a principle that you then supposed to implement in the code you write yourself. If you don't read and understand an example then you shouldn't be surprised if it doesn't work in your specific case. Too much copy and paste and not enough read and think.
-
Nov 24th, 2016, 03:51 AM
#12
Thread Starter
Lively Member
Re: Math functions in VB
Apologies for my ignorance.
-
Nov 25th, 2016, 06:54 PM
#13
Re: Math functions in VB
Originally Posted by jmcilhinney
something that you can copy and paste and expect to work as is, rather than a demonstration of a principle that you then supposed to implement in the code you write yourself. If you don't read and understand an example then you shouldn't be surprised if it doesn't work in your specific case. Too much copy and paste and not enough read and think.
Read my singnature
Disclaimer: When code is given for example - it is merely a example.
Add this to your singature so people know ahead a time its merely for example
A another useful tip is when putting code in your posts use the [code][/code] Brackets or hit the VB button on the toolbar.
Disclaimer: When code is given for example - it is merely a example. •
•
•
Unless said otherwise indicated - All Code snippets advice or otherwise that I post on this site, are expressly licensed under Creative Commons Attribution 4.0 International Please respect my copyrights.
Tags for this Thread
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
|