0

Hi all I am trying to learn VB and am having trouble with some code I am using. I would like my program to output a specific number based on if a check box is checked using case statements but my code is not working.

Public Class frmBTPW Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btncalc.Click Dim dblhdr As Double Dim dblfdr As Double Dim dbltdr As Double dblhdr = 24 dblfdr = 35 dbltdr = 50 Select Case "Power Wash Rental" Case "Half Day Rental" If chkhd.Checked = True Then txtrc.Text = "poop" End If Case "Full Day Rental" If chkFD.Checked = True Then txtrc.Text = dblfdr End If End Select End Sub Private Function Button1_Click() As CheckBox Throw New NotImplementedException End Function End Class 

Help would be greatly appreciated.My code isn't outputting anything in the text-box.

7
  • 2
    You need to read up on the case statement. Your test expression is a string literal, it won't match any of the cases. Commented Mar 16, 2015 at 15:10
  • 3
    what is "Power Wash Rental" supposed to represent? Commented Mar 16, 2015 at 15:10
  • I am supposed to make a program that calculates the cost of power wash rentals. A half day wash is $24 dollars Full Day is $35 and 2 day is $50. The "poop" thing was to see if it would even output text. It's just supposed to output the given values for the variables. nothing too fancy. Commented Mar 16, 2015 at 15:16
  • 1
    the widgets the user sees on the screen are Objects - Controls, to be exact. You may have a checkbox (or something) with the text "Power Wash Rental" but that is just a property of that Object or Control - hence the question. As is, "Power Wash Rental" is never going to match the "Full" or "Half" Text Commented Mar 16, 2015 at 15:17
  • 1
    the suggesion from @Plutonix to use a ComboBox was not for the output, but rather for the input. Commented Mar 16, 2015 at 16:28

4 Answers 4

1

Beyond case statements, respectfully I think you should read up on the distinction between a literal value and a variable. "Power Wash Rental" is nothing more than a series of characters, AKA a string: (In this case "P" followed by "o" etc.) Likewise, "Half Day Rental" is a series of characters, "H" followed by "a" etc.)

"Power Wash Rental" is a literal string. So is ""Half Day Rental" and of course they will never match.

Whereas:

Dim A as string A = TextBox1.text 

Now, A is a variable. It is a string which contains whatever series of characters (text) is typed into the textbox.

Sign up to request clarification or add additional context in comments.

Comments

1

This is a simple way to do it.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load chkhd.tag = 24 ' store values in the check boxes chkfd.tag = 35 ' using the tag property chktd.tag = 50 ' and later add up the values End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btncalc.Click dim total as double = 0 total += IF(chkhd.checked, cdbl(chkhd.tag), 0) total += IF(chkfd.checked, cdbl(chkfd.tag), 0) total += IF(chktd.checked, cdbl(chktd.tag), 0) msgbox(total) End Sub 

However, I think you might want radio buttons instead of checkboxes. Checkboxes can all be checked. Radio buttons can only have one at a time.

This solution allows you to keep your price with the checkbox -- you could do this in the form designer instead of form load.

Comments

0

I would recommend reading up on Case Statements. Currently you will never get anywhere as your using a string to what, nothing. You also do not need a case for this... Also if the first condition is true and the last one is as well, the last one win's for setting the text, didn't know if you had this there for a reason or not?

 If chkhd.Checked = True Then txtrc.Text = "poop" End If If chkFD.Checked = True Then txtrc.Text = dblfdr End If 

Comments

0

As others have stated your Case statement isn't working because you are using string literals to compare "Power Wash Rental" to "Half Day Rental" which will always be false. Plutonix was also correct in saying that a ComboBox for the rental duration should be used. The only reason not to be is if you were calculating cumulative rental days/amounts; however in that situation you should be using some sort of NumericUpDown for your multiplier against a time duration.

Here is an example that should help you get started. You could make the structure into a type of keyed collection or make it a wrapper class for a dictionary object which would make be easier to use in code. The following may not be exactly plug-and-play with your project, however it should help give you some ideas on how to handle the situation.

Option Strict On Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.ComboBox1.Items.AddRange({PowerWashRentals.halfDayText, PowerWashRentals.FullDayText, PowerWashRentals.TwoDayText}) AddHandler ComboBox1.SelectedValueChanged, AddressOf Me.ComboBox1_SelectedChanged End Sub Private Sub ComboBox1_SelectedChanged(sender As Object, e As EventArgs) Dim cBox As ComboBox = DirectCast(sender, ComboBox) Select Case cBox.SelectedItem.ToString Case PowerWashRentals.halfDayText Label1.Text = PowerWashRentals.HalfDayPrice.ToString Case PowerWashRentals.FullDayText Label1.Text = PowerWashRentals.FullDayPrice.ToString Case PowerWashRentals.TwoDayText Label1.Text = PowerWashRentals.TwoDayPrice.ToString End Select End Sub End Class Public Structure PowerWashRentals Public Const HalfDayPrice As Double = 24 Public Const FullDayPrice As Double = 35 Public Const TwoDayPrice As Double = 50 Public Const halfDayText As String = "Half Day Rental" Public Const FullDayText As String = "Full Day Rental" Public Const TwoDayText As String = "Two Day Rental" End Structure 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.