Visual Basic number range - vb.net

I'm trying to complete an assignment but I'm a little lost in the logic here. I'm trying to create a range for a kWh rate to applied for but can't seem to come up with anything. I'm new to vb and programming.
Dim stateTax As Decimal = 3.5
Dim cityTax As Decimal = 1.5
Dim kWhUsed As Decimal = txtkWhUsed.Text
Dim kWhRate As Decimal
Select Case True
Case (kWhUsed < 1000)
kWhRate = 0.052
RunTotalPrice = (kWhRate * kWhUsed)
Case (kWhUsed >= 1000)
kWhRate = 0.041
RunTotalPrice = RunTotalPrice + (kWhRate * kWhUsed)
End Select
txtAmtDue.Text = FormatCurrency(RunTotalPrice.ToString, 2)
End Sub

Use a Select Case since it does top down logic testing for you.
Dim kWhRate As Double
Select Case kWhUsed
Case < 1000
kWhRate = 0.052
Case < 2000
kWhRate = 0.041
'etc.
End Select

Related

if statement to mathematical addition gives answer zero in vb.net

' total service till retirement
retirement = Me.DateTimePicker2.Value
appointment = Me.DateTimePicker1.Value
Dim workingTime As TimeSpan = retirement - appointment
Dim yearVal As Double = workingTime.TotalDays / 365
Dim years = CInt(Math.Floor(yearVal))
Dim monthVal = (workingTime.TotalDays - years * 365) / 30
Dim months As Int32 = CInt(Math.Floor(monthVal))
Dim dayVal = workingTime.TotalDays - (years * 365 + months * 30)
Dim days As Int32 = CInt(Math.Floor(dayVal))
Dim result = $" {years} years {months} months & {days} Days"
tservice.Clear()
tservice.Text = result
' Pay reckonable for pension.
reckonable = Val(lastpay.Text) + Val(increment.Text)
'17. Service on the date of retirement. rounded off
If months > 5 Then
totalyear = (Val(years) + 1)
End If
'1. PENSION:-17760x26x7/300=Rs. 10774.40
tpension.Clear()
pension = (Val(reckonable) * Val(totalyear) * 7 / 300)
tpension.Text = pension
i need that answer should not be zero but due to totalyear a integer variable inside if statement gives zero i want it to add a value 1 if num of months is greater than 5 kindly help me.

VB program to calculate monthly deposits plus interest

I am having issues trying to make a calculator that accurately calculates deposits plus the amount of interest that is added monthly. I dont know how to incorporate a loop that can add monthly deposits to total deposits, then adds the total deposits plus total interest times the interest rate / 100 / 12 to the total deposits. Here is what I have so far. Sorry am super new to VB !
Dim intMonthlyDeposit As Interger
Dim intMonthsTotal As Integer
Dim intAnnualRate As Interget
Dim decTotalDeposits As Decimal
Dim decTotalInterest As Decimal
Dim decTotalTotal As Decimal
Try
intMonthlyDeposit = CInt(txtMonthlyDeposits.Text)
intMonthsTotal = CInt(txtMonths.Text)
decAnnualRate = CDec(txtAnnualRate.Text)
decTotalDeposits = 0
decTotalInterest = 0
decTotalTotal = 0
decTotalDeposits = decMonthlyDeposit * intMonthsTotal
decTotalInterest = decTotalDeposits * (decAnnualRate / 100)
decTotalTotal = decTotalDeposits + decTotalInterest
lblTotDeposit.Text = decTotalDeposits.ToString("C")
lblTot.Text = decTotalTotal.ToString("C")
lblTotInterest.Text = decTotalInterest.ToString("C")
Catch ex As Exception
MsgBox("Enter a Valid Number")
End Try
End Sub
This is how I implemented the loop for calculating total interest and total deposits correctly.
For index As Integer = 1 To intMonthsTotal Step 1
decTotalDeposits += decMonthlyDeposit
decTotalInterest += (decTotalDeposits + decTotalInterest) * ((decAnnualRate / 100) / 12)

Rounding up to nearest higher integer in VBA

I'm trying to calculate how many layers a commodity will be stacked in. I have a variable quantity (iQty), a given width for the loadbed (dRTW), a width per unit for the commodity (dWidth) and a quantity per layer (iLayerQty).
The quantity per layer is calculated as iLayerQty = Int(dRTW/dWidth)
Now I need to divide the total quantity by the quantity per layer and round up. In an Excel formula it would be easy, but I'm trying to avoid WorksheetFunction calls to minimise A1/R1C1 confusion. At the moment I'm approximating it with this:
(Number of layers) = ((Int(iQty / iLayerQty) + 1)
And that works fine most of the time - except when the numbers give an integer (a cargo width of 0.5 m, for instance, fitting onto a 2.5 m rolltrailer). In those instances, of course, adding the one ruins the result.
Is there any handy way of tweaking that formula to get a better upward rounding?
I don't see any reason to avoid WorksheetFunction; I don't see any confusion here.
Number_of_layers = WorksheetFunction.RoundUp(iQty / iLayerQty, 0)
You could also roll your own function:
Function RoundUp(ByVal Value As Double)
If Int(Value) = Value Then
RoundUp = Value
Else
RoundUp = Int(Value) + 1
End If
End Function
Call it like this:
Number_of_layers = RoundUp(iQty / iLayerQty)
If using a WorksheetFunction object to access a ROUNDUP or CEILING function is off the table then the same can be accomplished with some maths.
Number of layers = Int(iQty / iLayerQty) - CBool(Int(iQty / iLayerQty) <> Round(iQty / iLayerQty, 14))
A VBA True is the equivalent of (-1) when used mathematically. The VBA Round is there to avoid 15 digit floating point errors.
I use -int(-x) to get the ceiling.
?-int(-1.1) ' get ceil(1.1)
2
?-int(1.1) ' get ceil(-1.1)
-1
?-int(-5) ' get ceil(5)
5
These are the functions I put together for this purpose.
Function RoundUp(ByVal value As Double) as Integer
Dim intVal As Integer
Dim delta As Double
intVal = CInt(value)
delta = intVal - value
If delta < 0 Then
RoundUp = intVal + 1
Else
RoundUp = intVal
End If
End Function
Function RoundDown(ByVal value As Double) as Integer
Dim intVal As Integer
Dim delta As Double
intVal = CInt(value)
delta = intVal - value
If delta <= 0 Then
RoundDown = intVal
ElseIf delta > 0 Then
RoundDown = intVal - 1
End If
End Function
This is my Ceiling in VBA.
Function Ceiling(ByVal Number As Double, ByVal Significance As Double) As Double
Dim intVal As Long
Dim delta As Double
Dim RoundValue As Double
Dim PreReturn As Double
If Significance = 0 Then
RoundValue = 1
Else
RoundValue = 1 / Significance
End If
Number = Number * RoundValue
intVal = CLng(Number)
delta = intVal - Number
If delta < 0 Then
PreReturn = intVal + 1
Else
PreReturn = intVal
End If
Ceiling = PreReturn / RoundValue
End Function

Working out a basic score from integer values

I'm trying to work out a basic % value by gathering values together:
For Each M As Match In matchFound
lamdaFix2 = M.Groups(1).Value
lamdaFix3 = M.Groups(2).Value
Application.DoEvents()
Dim lv As ListViewItem = formMozCheck.listViewMoz.Items.Add(lamdaFix1)
lv.UseItemStyleForSubItems = False
lv.SubItems.Add(Math.Round(Double.Parse(lamdaFix2)).ToString())
lv.SubItems.Add(Math.Round(Double.Parse(lamdaFix3)).ToString())
lv.SubItems.Add("-")
lv.SubItems.Add("-")
lv.SubItems.Add(itm.SubItems(8).Text)
Dim srVal As Integer
If (itm.SubItems(9).Text = "") Then
srVal = 0
Else
srVal = CInt(itm.SubItems(9).Text)
End If
lv.SubItems.Add(srVal.ToString())
' work out a score
Dim overAllScore As Integer
' TODO: basic score
overAllScore = CInt(CInt(CDbl(Math.Round(Double.Parse(lamdaFix2)).ToString()) & CDbl(Math.Round(Double.Parse(lamdaFix3)).ToString()) & CDbl(itm.SubItems(8).Text) & Val(srVal)) * 100)
lv.SubItems.Add(CLng(overAllScore) & "%").ForeColor = Color.DarkGreen
itm.Checked = False
Next
These values:
lv.SubItems.Add(Math.Round(Double.Parse(lamdaFix2)).ToString())
lv.SubItems.Add(Math.Round(Double.Parse(lamdaFix3)).ToString())
Are values between 0 - 100
And these ones:
itm.SubItems(8).Text
itm.SubItems(9).Text
Are values between 1 - 10
I have been racking the brains to see what the best way to add these values up and give a basic percentage, my attempt:
overAllScore = CInt(CInt(CDbl(Math.Round(Double.Parse(lamdaFix2)).ToString()) & CDbl(Math.Round(Double.Parse(lamdaFix3)).ToString()) & CDbl(itm.SubItems(8).Text) & Val(srVal)) * 100)
Just adds all values like 234524 rather than the total value of 23+45+2+4
i'm probably over complicating this lol
i tagged it as both vb and c# as i though the logic would be the same ;)
thanks for any help guys!
Graham
You should use "+" instead of "&":
overAllScore = CInt(CInt(CDbl(Math.Round(Double.Parse(lamdaFix2)).ToString()) + CDbl(Math.Round(Double.Parse(lamdaFix3)).ToString()) + CDbl(itm.SubItems(8).Text) + Val(srVal)) * 100)

Can't understand why my variable doesn't change

I'm building a little time > pay conversion program in VB. I'm really new to VB and don't understand why my variable pay doesn't calculate like it should. I plug in 5 5's as a test and get $0.
Dim total As Double = 0.0
Dim timeCounter As Integer = 0
Dim time As Integer = 0
Dim pay As Double = 0.0
While timeList.Items.Count < 5
time = timeList.Items(timeCounter)
total += time
timeCounter += 1
End While
If total >= 0 And total <= 40 Then
If total >= 0 And total <= 20 Then
pay = total * 10
ElseIf total >= 21 And total <= 30 Then
pay = total * 12
ElseIf total >= 31 And total <= 40 Then
pay = total * 15
Else
PayLabel.Text = "Error"
End If
End If
PayLabel.Text = "$" & pay
Your syntax should be something like this:
For intCount = 0 To timeList.Items.Count
time = timeList.Items(intCount)
total += time
Next intCount
This will avoid an infinite loop.
To fix your 40+ issue:
If total >= 0 And total <= 40 Then
If total >= 0 And total <= 20 Then
pay = total * 10
ElseIf total >= 21 And total <= 30 Then
pay = total * 12
ElseIf total >= 31 And total <= 40 Then
pay = total * 15
End If
Else
PayLabel.Text = "Error"
End If
this would be my fix into a console apps
for process will return $0, second $100
Module Module1
Sub Main()
Dim timeList As New List(Of Integer)
timeList.AddRange(New Integer() {1, 2, 3, 4, 5, 6})
process(timeList)
timeList.Clear()
timeList.AddRange(New Integer() {1, 2, 3, 4})
process(timeList)
Console.Read()
End Sub
Private Sub process(timeList As List(Of Integer))
Dim total As Double = 0.0
Dim timeCounter As Integer = 0
Dim time As Integer = 0
Dim pay As Double = 0.0
While timeList.Count < 5 AndAlso timeCounter < timeList.Count
time = timeList(timeCounter)
total += time
timeCounter += 1
End While
If total >= 0 And total <= 40 Then
If total >= 0 And total <= 20 Then
pay = total * 10
ElseIf total >= 21 And total <= 30 Then
pay = total * 12
ElseIf total >= 31 And total <= 40 Then
pay = total * 15
Else
Console.WriteLine("error")
End If
End If
Console.WriteLine("$" & pay)
End Sub
End Module
This could be better solved with a functional approach. To get the sum of the list of integers do the following:
Dim totalTime = timeList.Sum()
Then you can follow the logic you laid out. I would highly recommend learning to use Linq Set Functions to make your code your readable and easier to understand. Good Luck.