Message Box's, Try/Catch Blocks & IF/Else

Message Box's, Try/Catch Blocks & IF/Els

Page 1 of 1

6 Replies - 12094 Views - Last Post: 10 December 2005 - 10:50 PM Rate Topic: -----

#1 Journey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 27-November 05

Message Box's, Try/Catch Blocks & IF/Else

Posted 09 December 2005 - 10:05 PM

I have more trouble with Visual Basic than I have ever had with any other class in my life -- however, my Professor feels that since I loathe it with a passion, I could become a pretty good programmer, I am not sure I agree with his assessment.

In this exercise of higher education, I am to create a code that will calculate and display the gallons of water used and the water charge. It should also verify the meter reading to be correct and if the current reading is not greater than or equal to the previous reading a message box is supposed to pop up.

Now, the following code works (but I do get "C2" in the Water Charge Label text and I get 0 gallons used no matter what I put in) ... so I'm on the right track. But somewhere in the IF/Else, I have gotten lost.

So here I am, asking for help. Gosh you guys are great and I hate to keep asking but I have always been taught, you will never learn anything if you don't ask.

Here is the part that semi works:

Private Sub uiCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uiCalcButton.Click
Dim Current, Previous, GallonsUsed As Integer
Dim WaterCharge As Double
'assign input variables
Current = Integer.Parse(Me.uiCurrentTextBox.Text)
Previous = Integer.Parse(Me.uiPreviousTextBox.Text)

'verify the meter readings
If Current >= Previous Then
'calculate the water bill
WaterCharge = Convert.ToDouble(GallonsUsed) * 0.00175
Me.uiGalUsedLabel.Text = Convert.ToString(GallonsUsed)
Me.uiChargeLabel.Text = Convert.ToString("C2")
End If

***********

Now here is where it really gets messed up and I'm literally confused, I haven't even added the Try/Catch statement yet, I'm becoming afraid too .. LOL.

If I remove the ending "End IF" from above and place the following below, I get errors up the ying yang.

Else
'If meter reading is incorrect
MessageBox.Show("The meter reading must be greater than or equal to the previous reading.", _ messageboxbuttons.OK, MessageBoxIcon.Information)
End If

So what have I done to this poor code?

Thanks to anyone who can help.

Warmest regards,

Journey

Is This A Good Question/Topic? 0
  • +

Replies To: Message Box's, Try/Catch Blocks & IF/Else

#2 Journey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 27-November 05

Re: Message Box's, Try/Catch Blocks & IF/Else

Posted 09 December 2005 - 10:30 PM

I have added the remainder of the code, still get those funny blue lines here and there and I can't figure it out!!

Here is my code in it's final completion.

Private Sub uiCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uiCalcButton.Click
Dim Current, Previous, GallonsUsed As Integer
Dim WaterCharge As Double
Try
'assign input variables
Current = Integer.Parse(Me.uiCurrentTextBox.Text)
Previous = Integer.Parse(Me.uiPreviousTextBox.Text)
WaterCharge = Double.Parse(Me.uiChargeLabel.Text)

'verify the meter readings
If Current >= Previous Then
'calculate the water bill
WaterCharge = Convert.ToDouble(GallonsUsed) * 0.00175
Me.uiGalUsedLabel.Text = Convert.ToString(GallonsUsed)
Me.uiChargeLabel.Text = WaterCharge.ToString("C2")
Else
'when meter reading is incorrect
MessageBox.Show("The meter reading must be greater than or equal to the previous reading.", _
MessageBoxButton.Ok, MessageBoxIcon.Information)
End If

Catch exFormat As FormatException
MessageBox.Show {"The Current, Previous, GallonsUsed and WaterCharge must be numbers.", _
"WaterCharge", MessageBoxButtons.OK,MessageBoxIcon.Information)
Catch Ex As Exception
MessageBox.Show(Ex.Message, "WaterCharge", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub
End Class
Was This Post Helpful? 0
  • +
  • -

#3 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Message Box's, Try/Catch Blocks & IF/Else

Posted 10 December 2005 - 10:38 AM

Could you specify what you mean by 'funny blue lines'? Off the top of my head, I wwould note that you are not assigning a value to GallonsUsed anywhere, but you are using it in calculations...this will mean your result is not valid.
Was This Post Helpful? 0
  • +
  • -

#4 Journey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 27-November 05

Re: Message Box's, Try/Catch Blocks & IF/Else

Posted 10 December 2005 - 02:50 PM

When I write the code in VB.net, it gives you a blue squiggle line under a "string" of commands that have a problem. I get that blue squiggle under the following:

Else
'when meter reading is incorrect
MessageBox.Show("The meter reading must be greater than or equal to the previous reading.", _
MessageBoxButton.Ok, MessageBoxIcon.Information)
End If


Opps on the not placing "readings numeric" in the code. I told you Amadeus, this is killing me ..and I have found the book VB.net for Dummies, but Borders is out of stock on it. **sigh** so will have to order it off the internet. I refuse to let this programming course get the best of me. I will beat it yet. But am afraid this isn't the right time. LOL
Was This Post Helpful? 0
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Message Box's, Try/Catch Blocks & IF/Else

Posted 10 December 2005 - 03:42 PM

Ah, sorry for the misunderstanding...the blue squiggle line appears in Visual Studio...I don't often use Visual Studio as an IDE. The line indicates an error...if you hover above the squiggle line, it should display the error...can you post that?
Was This Post Helpful? 0
  • +
  • -

#6 AdmSteck  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 60
  • Joined: 26-November 05

Re: Message Box's, Try/Catch Blocks & IF/Else

Posted 10 December 2005 - 10:07 PM

First thing I noticed is in the catch portion of your code you have a { instead of a ( on the messagebox command.

Also, it seems as though you have a couple of logic errors. I'm not sure why you assign the value of uiChargeLabel.Text to WaterCharge and then re-assign a calculated value to it without using the original value for anything. Probably an extra line of code that could be removed. As Amadeus pointed out, you use GallonsUsed in calculations without assigning it a value. I assume you would want to add a line of code something like the following right after your IF statement:

GallonsUsed = Current - Previous


Lastly, to fix the message box in the ELSE statement... It seemed that the number of parameters you specified for the show method was off slightly. All I had to do was add the Title parameter before the buttons like so...

MessageBox.Show("The meter reading must be greater than or equal to the previous reading.", _
                "Your title here.", MessageBoxButtons.OK, MessageBoxIcon.Information)


Also had to add an 's' to the end of MessageBoxButton to make it MessageBoxButtons.OK. You had the 's' in your first post, but not the next two. Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#7 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Message Box's, Try/Catch Blocks & IF/Else

Posted 10 December 2005 - 10:50 PM

Unless I'm mistaken, the show() method is overloaded, and can accept (depending on usage) from one to four parameters...at least it can in C#, and the show() method is shared between vb.net and c#.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1