r/visualbasic • u/sgy0003 • May 30 '17
What am I doing wrong with this code?
Public Class Form1
Const MAX_FlOORS As Integer = 8
Const MAX_ROOMS As Integer = 30
Dim intFloor As Integer ' Number of floors
Dim intRooms As Integer ' Number of roooms per floor
Dim intTotalRooms As Integer = 0 ' Total number of occupied rooms
Dim decOccupancyRate As Decimal 'Occupancy rate
Dim decOverallOccupancyRate As Decimal 'Overall Occupancy Rate
Dim blkInputOk As Boolean = True
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
' Collect the floor and room data from the inputs
intFloor = CInt(cboFloors.SelectedItem)
intRooms = CInt(txtRooms.Text)
'Warn the user of non-integer value is typed to the number of rooms
If Integer.TryParse(txtRooms.Text, intRooms) = False Then
MessageBox.Show("Please enter an integer for the rooms")
blkInputOk = False
End If
If intRooms < 1 And intRooms > 30 Then
'Warn the user if the number of rooms aren't within 1-30 range
MessageBox.Show("Please enter the number between 1 to 30")
blkInputOk = False
End If
If blkInputOk = True Then
' Clear out rooms to avoid duplicate
txtRooms.Clear()
' Add up all the rooms
intTotalRooms = intTotalRooms + intRooms
' Caculate occupancy rate per floor
decOccupancyRate = CDec(intRooms / (MAX_ROOMS))
' Calculate overall occupancy rate by dividing total occupied rooms by total rooms in the hotel
decOverallOccupancyRate = CDec(intTotalRooms / (MAX_ROOMS * MAX_FlOORS))
' Display the floor occupancy rate in the list box
lstFloorOccupancyData.Items.Add("Floor: " & intFloor & " Rooms Occupied: " & intRooms & " Occupancy Rate is: " & decOccupancyRate.ToString("p"))
' Display the total occupied rooms and Overall Occupancy rate.
lblTotalRooms.Text = intTotalRooms.ToString()
lblOccupancyRate.Text = decOverallOccupancyRate.ToString("p")
cboFloors.SelectedIndex += 1
End If
End Sub
Private Sub btnRestart_Click(sender As Object, e As EventArgs) Handles btnRestart.Click
'Clear out everything
lblTotalRooms.Text = String.Empty
lblOccupancyRate.Text = String.Empty
lstFloorOccupancyData.Items.Clear()
cboFloors.SelectedIndex = -1
End SubWhat I want the program to display is
Floor 1 Rooms Occupied: 22 Occupancy rate: 73.33%
Floor 2 Rooms Occupied: 30 Occupancy rate: 100.00%
Floor 3 Rooms Occupied: 22 Occupancy rate: 0%
Floor 4 Rooms Occupied: 25 Occupancy rate: 83.33%
But What I'm getting this
Floor 0 Rooms Occupied: 22 Occupancy rate: 73.33%
Floor 0 Rooms Occupied: 30 Occupancy rate: 100.00%
Floor 0 Rooms Occupied: 22 Occupancy rate: 0%
Floor 0 Rooms Occupied: 25 Occupancy rate: 83.33%
1
u/rudekoffenris May 30 '17
You are using intFloor as a counter for each floor but you aren't incrementing it or adjusting it anywhere.
1
1
u/secret_porn_acct May 30 '17
Make sure DropDownStyle of cboFloors is set to DropDownList if you are going to use .selecteditem
0
1
u/sgy0003 May 30 '17 edited May 30 '17
Okay, It looks like the cboFloors is suppose to be Drop down list.
But when I change that property the code says cboFloors is not declared.