If...Then...Else question, beginning, newbie.

Sorry in advance if my question is inappropriate.

Page 1 of 1

5 Replies - 1438 Views - Last Post: 16 December 2009 - 02:31 PM Rate Topic: -----

#1 ritsybitsy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-December 09

If...Then...Else question, beginning, newbie.

Posted 14 December 2009 - 01:55 PM

Hi, I'm sorry in advance if this is inappropriate. I'm not asking for code, I actually have a question and it's probably going to be really dumb, but I'm kind of stuck.
I'm doing a, I guess simple if...then...else.
It's supposed to calculate and display the number of gallons of water used and the total charge for the water, which is .00175/gal. It should only make the calculations when the current meter reading is >= the previous meter reading, or else it should print an error message.

	Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click
		If (Val)currentTextBox < (val)previousTextBox 
		Then badlabel.text = "Error"
		Else
			totalgallonsLabel = Val(currentTextBox) + Val(previousTextBox)
			totalchargeLabel = totalgallonsLabel * 0.00175
		End If

	End Sub



But it's giving errors which I've attatched. Is there an/many obvious mistake/s that I'm making?
Thank you.

Attached image(s)

  • Attached Image

This post has been edited by ritsybitsy: 14 December 2009 - 01:57 PM


Is This A Good Question/Topic? 0
  • +

Replies To: If...Then...Else question, beginning, newbie.

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,548
  • Joined: 23-August 08

Re: If...Then...Else question, beginning, newbie.

Posted 14 December 2009 - 02:12 PM

First problem: you posted in VB6, but this code is VB.NET. I'll move it for you, and let the VB gurus answer for you.
Was This Post Helpful? 0
  • +
  • -

#3 ZRonZ  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 206
  • Joined: 09-January 09

Re: If...Then...Else question, beginning, newbie.

Posted 14 December 2009 - 03:02 PM

View Postritsybitsy, on 14 Dec, 2009 - 12:55 PM, said:

Hi, I'm sorry in advance if this is inappropriate. I'm not asking for code, I actually have a question and it's probably going to be really dumb, but I'm kind of stuck.
I'm doing a, I guess simple if...then...else.
It's supposed to calculate and display the number of gallons of water used and the total charge for the water, which is .00175/gal. It should only make the calculations when the current meter reading is >= the previous meter reading, or else it should print an error message.

	Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click
		If (Val)currentTextBox < (val)previousTextBox 
		Then badlabel.text = "Error"
		Else
			totalgallonsLabel = Val(currentTextBox) + Val(previousTextBox)
			totalchargeLabel = totalgallonsLabel * 0.00175
		End If

	End Sub



But it's giving errors which I've attatched. Is there an/many obvious mistake/s that I'm making?
Thank you.


First off, look up the proper use of the Val function. You had the right idea but you didn't execute it correctly in the If statement. Follow through with the Else code.

Second, totalgallonslabel isn't a value, it's a label. Labels have properties like value. You got it right with badlabel.text, do the same for the other labels.

Correct those and you'll at least have a good start at it and should be able to fix the math.
Was This Post Helpful? 0
  • +
  • -

#4 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: If...Then...Else question, beginning, newbie.

Posted 15 December 2009 - 02:48 PM

If you didīnt understand what said.. when this is that he meant

this code
 totalchargeLabel = totalgallonsLabel * 0.00175

returns a error because itīs a label. So thisīs a string unless you convert it first.
And you cant do string = string * dubble/decimal/singel or any other value holder.
itīs simple. what till you get if you do like this for example: Hello = Hello * 0.43. ^^
Well, you get error :)
Sure thatīs to much explanation, but that was just to make sure you understand :)
Was This Post Helpful? 0
  • +
  • -

#5 thalorre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-December 09

Re: If...Then...Else question, beginning, newbie.

Posted 16 December 2009 - 02:22 PM

So you need to say:

 Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click

dim totalcharge as Integer
dim totalgallons as Integer

totalgallonsLabel.text = totalgallons
totalchargeLabel.text = totalcharge

If (Val)currentTextBox < (val)previousTextBox 
		Then badlabel.text = "Error"
		Else
			totalgallons = Val(currentTextBox) + Val(previousTextBox)
			totalcharge = totalgallonsLabel * 0.00175
 End If
   End Sub



it's been a while that i used VB but i think that's it.

Grtz.
Was This Post Helpful? 0
  • +
  • -

#6 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: If...Then...Else question, beginning, newbie.

Posted 16 December 2009 - 02:31 PM

View Postthalorre, on 16 Dec, 2009 - 01:22 PM, said:

So you need to say:

 Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click

dim totalcharge as Integer
dim totalgallons as Integer

totalgallonsLabel.text = totalgallons
totalchargeLabel.text = totalcharge

If (Val)currentTextBox < (val)previousTextBox 
		Then badlabel.text = "Error"
		Else
			totalgallons = Val(currentTextBox) + Val(previousTextBox)
			totalcharge = totalgallonsLabel * 0.00175
 End If
   End Sub



it's been a while that i used VB but i think that's it.

Grtz.



Well. yes, that sould work because you convert it to a integer befor using it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1