Results 1 to 13 of 13

Thread: Math functions in VB

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    67

    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.

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

    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:
    1. Dim textBoxes = Me.Controls.OfType(Of TextBox)()
    2. Dim numbers = textboxes.Where(Function(tb) Integer.TryParse(tb.Text, Nothing)).Select(Function(tb) Integer.Parse(tb.Text))
    3. Dim minNumber = numbers.Min()

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    67

    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?

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Math functions in VB

    Instead of
    Dim textBoxes = Me.Controls.OfType(Of TextBox)()

    Try
    Dim textBoxes() as TextBox = {tbAAH, tbCol, tbDE, tbLex, tbUni}

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    67

    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?

  6. #6
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Math functions in VB

    Quote Originally Posted by Pianoman23 View Post
    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

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    67

    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

  8. #8
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    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

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    67

    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".

  10. #10

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

    Re: Math functions in VB

    Quote Originally Posted by boops boops View Post
    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.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    67

    Re: Math functions in VB

    Apologies for my ignorance.

  13. #13
    Frenzied Member jdc20181's Avatar
    Join Date
    Oct 2015
    Location
    Indiana
    Posts
    1,168

    Re: Math functions in VB

    Quote Originally Posted by jmcilhinney View Post
    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
  •  



Click Here to Expand Forum to Full Width