Need some help with multiple variables.. which depend on if..then

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 2256 Views - Last Post: 12 October 2010 - 06:26 AM Rate Topic: -----

#1 pinnacle2009  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 06-October 10

Need some help with multiple variables.. which depend on if..then

Posted 06 October 2010 - 06:04 PM

So I am working on a project for class. I first hit a bump when I could not figure out how to get this to count the number of times the button was clicked. Now I have hit a road block. Here is what I have so far (I stopped where I couldn't get it to work):

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim count As Integer
        Dim price As Double
        Dim price2 As Double
        Dim price3 As Double
        Dim price4 As Double
        Dim price5 As Double
        Label1.Text = Val(Label1.Text) + 1
        count = Label1.Text
        Label2.Text = String.Format("You have entered {0:N0} item(s).", count)
        If count = 1 Then
            price = TextBox1.Text
            Label3.Text = String.Format("The Largest Price was {0:C}.", price)
            Label4.Text = String.Format("The Lowest Price was {0:C}.", price)
            Label5.Text = String.Format("The Average Price was {0:C}.", price)
        End If
        If count = 2 Then
            price2 = TextBox1.Text
            If price > price2 Then
                Label3.Text = String.Format("The Largest Price was {0:C}.", price)
            Else
                Label3.Text = String.Format("The Largest Price was {0:C}.", price2)
            End If
        End If
    End Sub



How can I get this to remember variables which were completely dependent upon the count?

The project is asking for 5 prices entered 1 at a time, clicking the button ("Include Price"). After each number is enter, I am supposed to be getting the largest, lowest, and average of the numbers.

MOD EDIT: This is VB.NET code, not VB6. Moved there.

This post has been edited by JackOfAllTrades: 06 October 2010 - 08:23 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Need some help with multiple variables.. which depend on if..then

#2 guyfromri  Icon User is offline

  • D.I.C Addict

Reputation: 46
  • View blog
  • Posts: 819
  • Joined: 16-September 09

Re: Need some help with multiple variables.. which depend on if..then

Posted 06 October 2010 - 06:26 PM

ok...lets start from the bottom and work up


ubound=highest value
lbound=lowest value

To get an average, I don't know of a function so unless there is one I am unaware of, you'll have to add the 5 together than divide by 5. To take it a step further, you could create a new count of variables then use that as the number to divide by so in the case of more or less prices, the average will remain accurate.

QUESTION: As far as remembering variables....do you want to remember the name of the variable or the value? And do you want it remembered permenantely or do you want it remembered until the program is closed? I'm half awake so I'm not quite sure I follow the question but it may be me......


Hope I helped a little bit!!!!

This post has been edited by guyfromri: 06 October 2010 - 06:27 PM

Was This Post Helpful? 0
  • +
  • -

#3 pinnacle2009  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 06-October 10

Re: Need some help with multiple variables.. which depend on if..then

Posted 06 October 2010 - 06:30 PM

I'd like it to remember the name and value if possible. (Unless I only need the value to find largest, lowest, and average)

And thanks for the functions, did not know about them.
Was This Post Helpful? 0
  • +
  • -

#4 pinnacle2009  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 06-October 10

Re: Need some help with multiple variables.. which depend on if..then

Posted 07 October 2010 - 05:16 AM

Anyone? We have not gotten to arrays yet, so I dont think we can use them (not to mention i don't know how to use them.)

Is there another way to save variables and values across multiple If..Thens? Or am I just going about this wrong?

edit: Would Select..Case work?

This post has been edited by pinnacle2009: 07 October 2010 - 05:21 AM

Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: Need some help with multiple variables.. which depend on if..then

Posted 07 October 2010 - 06:40 AM

If you are struggling with some of the basic concepts in vb.net, may I suggest my blog aimed at beginners; VB Peasy Lemon Squeezy
Was This Post Helpful? 0
  • +
  • -

#6 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 181
  • View blog
  • Posts: 659
  • Joined: 03-February 10

Re: Need some help with multiple variables.. which depend on if..then

Posted 07 October 2010 - 06:41 AM

So you need to enter 5 numbers and report the current average, current largest, and current lowest? Since you state that you can't use arrays since you haven't learned about them, then we will use class variables. Also then UBound and LBound won't work either.


First make these class level variables, notice they
Public Class Form1
     Dim buttonCount As Integer
     Dim currentHighestPrice As Double
     Dim currentLowestPrice As Double
     Dim currentTotalPrice As Double
     Dim price1 As Double
     Dim price2 As Double
     Dim price3 As Double
     Dim price4 As Double
     Dim price5 As Double

End Class




Next thing, to get the current lowest value. We reference a class level variable for the buttonCount and for the currentLowestPrice. The way this operates is that on each successive button click we only need compare to the current lowest price and the current value that was entered, because we already have the lowest price of all the values we just need to know if it still will be or if the current entered value is lower. If the current entered value is lower then that becomes the new currentLowestPrice.

Public Function GetCurrentLowestValue() As Double
     Select Case buttonCount
          Case 1:
               Return price1

          Case 2:
               If price2 < currentLowestPrice Then
                    Return price2
               End If

          Case 3:
               If price3 < currentLowestPrice Then
                    Return price3
               End If

          Case 4:
               If price4 < currentLowestPrice Then
                    Return price4
               End If
          Case 5:
               If price5 < currentLowestPrice Then
                    Return price5
               End If
     End Case
End Function




We can do the same thing for the currentHighestPrice as we did for the lowest price.
Public Function GetCurrentHighestValue() As Double
     Select Case buttonCount
          Case 1:
               Return price1

          Case 2:
               If price2 > currentHighestPrice Then
                    Return price2
               End If

          Case 3:
               If price3 > currentHighestPrice Then
                    Return price3
               End If

          Case 4:
               If price4 > currentHighestPrice Then
                    Return price4
               End If
          Case 5:
               If price5 > currentHighestPrice Then
                    Return price5
               End If
     End Case
End Function



The average is merely the Total of the prices divided by the number of prices or currentTotalPrice / buttonCount



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

     'To keep track of our number of clicks we need to increment the count with each button click
     buttonCount += 1

     'We are also going to track the Total of all the prices
     currentTotalPrice += Convert.ToDouble(TextBox1.Text)
     
     'Set the price values
     Select Case buttonCount
          Case 1: price1 = Convert.ToDouble(TextBox1.Text)
          Case 2: price2 = Convert.ToDouble(TextBox1.Text)
          Case 3: price3 = Convert.ToDouble(TextBox1.Text)
          Case 4: price4 = Convert.ToDouble(TextBox1.Text)
          Case 5: price5 = Convert.ToDouble(TextBox1.Text)          
     End Select

     'Disable the button if we have gotten to the 5th and final value
     Button1.Enabled = (buttonclick < 5)

     'Report the number of items 
     Label2.Text = String.Format("You have entered {0:N0} item(s).", buttonCount)

     'Report the Highest value
     'First we need to determine the highest value
     currentHighestPrice = GetCurrentHighestValue()
     Label3.Text = String.Format("The Largest Price was {0:C}.", currentHighestPrice)

     'Report the Lowest value
     'First we need to determine the lowest value
     currentLowestPrice = GetCurrentLowestValue()
     Label4.Text = String.Format("The Lowest Price was {0:C}.", currentLowestPrice)

     'Report the average
     'The average is the total of the prices / number of times the button was clicked
     Label5.Text = String.Format("The Average Price was {0:C}.",  (currentTotalPrices/buttonCount))
       
End Sub



There are many other things that should be added/improved: a List or array would vastly improve things, error handling, Double.TryParse just to name a few.
Was This Post Helpful? 0
  • +
  • -

#7 pinnacle2009  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 06-October 10

Re: Need some help with multiple variables.. which depend on if..then

Posted 07 October 2010 - 12:08 PM

Thank you very much. I emailed the professor and apparently there was something he forgot to go over before giving us this assignment. Hopefully its arrays; after looking at them in our book, it would make things a little better. The assignment was also pushed back to be due later, so now I have more time to look over what you have posted.

Thanks again for the help! :)
Was This Post Helpful? 0
  • +
  • -

#8 pinnacle2009  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 06-October 10

Re: Need some help with multiple variables.. which depend on if..then

Posted 10 October 2010 - 05:35 PM

Okay, here is what I have. I am having problems with the output of the high and low price. I am not sure exactly what I am doing, but when some unknown number is entered, they will output "0"


Public Class Form1
    Dim price1, price2, price3, price4, price5, total, lowprice, highprice As Double
    Dim count As Integer
    Public Function lowpricevalue() As Double
        Select Case count
            Case 1
                Return price1
            Case 2
                If price2 < lowprice Then
                    Return price2
                End If
            Case 3
                If price3 < lowprice Then
                    Return price3
                End If
            Case 4
                If price4 < lowprice Then
                    Return price4
                End If
            Case 5
                If price5 < lowprice Then
                    Return price5
                End If
        End Select
    End Function
    Public Function highpricevalue() As Double
        Select Case count
            Case 1
                Return price1
            Case 2
                If price2 > highprice Then
                    Return price2
                End If
            Case 3
                If price3 > highprice Then
                    Return price3
                End If
            Case 4
                If price4 > highprice Then
                    Return price4
                End If
            Case 5
                If price5 > highprice Then
                    Return price5
                End If
        End Select
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        count += 1
        total += TextBox1.Text
        Select Case count
            Case 1
                price1 = TextBox1.Text
            Case 2
                price2 = TextBox1.Text
            Case 3
                price3 = TextBox1.Text
            Case 4
                price4 = TextBox1.Text
            Case 5
                price5 = TextBox1.Text
        End Select
        Label2.Text = String.Format("You have entered {0:N0} item(s).", count)
        highprice = highpricevalue()
        Label3.Text = String.Format("The Largest Price was {0:C}.", highprice)
        lowprice = lowpricevalue()
        Label4.Text = String.Format("The Lowest Price was {0:C}.", lowprice)
        Label5.Text = String.Format("The Average Price was {0:C}.", (total / count))
    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#9 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 181
  • View blog
  • Posts: 659
  • Joined: 03-February 10

Re: Need some help with multiple variables.. which depend on if..then

Posted 10 October 2010 - 07:05 PM

In the button click you need to perform error checking before allowing any other code to run.


If Not String.IsNullOrEmpty(TextBox1.Text) Then
     'check for number
     Dim enteredValue As Double
     If Double.TryParse(TextBox1.Text, enteredValue)
          'enteredValue will contain the value parsed into a double value already     
          'Place all the code from the click event here

     Else
           MessageBox.Show("Entered value is not a number")
     End If
Else
     MessageBox.Show("You need to enter a value.")
End If


Was This Post Helpful? 0
  • +
  • -

#10 pinnacle2009  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 06-October 10

Re: Need some help with multiple variables.. which depend on if..then

Posted 10 October 2010 - 07:23 PM

View Postdemausdauth, on 10 October 2010 - 06:05 PM, said:

In the button click you need to perform error checking before allowing any other code to run.


If Not String.IsNullOrEmpty(TextBox1.Text) Then
     'check for number
     Dim enteredValue As Double
     If Double.TryParse(TextBox1.Text, enteredValue)
          'enteredValue will contain the value parsed into a double value already     
          'Place all the code from the click event here

     Else
           MessageBox.Show("Entered value is not a number")
     End If
Else
     MessageBox.Show("You need to enter a value.")
End If



is there a way without the TryParse? I'd rather not be including stuff in here that we have not gone over in class.
Was This Post Helpful? 0
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: Need some help with multiple variables.. which depend on if..then

Posted 10 October 2010 - 07:42 PM

There nothing wrong with doing independent research. How do know it will be cover in class?

This post has been edited by AdamSpeight2008: 10 October 2010 - 07:51 PM

Was This Post Helpful? 0
  • +
  • -

#12 pinnacle2009  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 06-October 10

Re: Need some help with multiple variables.. which depend on if..then

Posted 10 October 2010 - 08:44 PM

Thats true. Wouldnt hurt to go ahead and learn what I can about that. Thanks
Was This Post Helpful? 0
  • +
  • -

#13 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: Need some help with multiple variables.. which depend on if..then

Posted 10 October 2010 - 09:37 PM

Plus there is benefit of when it is covered you can ask questions about the things you didn't understand in class.
Was This Post Helpful? 0
  • +
  • -

#14 pinnacle2009  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 06-October 10

Re: Need some help with multiple variables.. which depend on if..then

Posted 11 October 2010 - 12:34 PM

I am still getting the "0" problem. At the end of the two functions, the "End Function" part is underlined in green. The error, it says, is: "Function 'lowpricevalue' doesn't return a value on all code paths. Are you missing a 'Return' statement?" (And the same for 'highpricevalue')
Was This Post Helpful? 0
  • +
  • -

#15 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: Need some help with multiple variables.. which depend on if..then

Posted 11 October 2010 - 12:44 PM

It probably caused by a Select...Case statement missing a default option should none of the cases match. Add an appropriate Case Else the end of the cases.
A Double is a value type which means it always has value, and generally speaking this Zero.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2