Wrong Calculation in Visual basic - vb.net

Hi i have problem a wrong calculation in visual basic
when i input 1.5 all in the combo boxes then
the textbox4.textto textbox14.text are the units that has the values of 3,3,3,3,3,3,6,2,3 that is equal to 29.
I add all from textbox30.textto textbox22.text the divide them to the total units that is 29
the textbox31.text is equal to 1.51724137931034 but the correct value is 1.50
like this
TextBox30.Text = 3*1.5
TextBox29.Text = 3*1.5
TextBox28.Text = 3*1.5
TextBox27.Text = 3*1.5
TextBox26.Text = 3*1.5
TextBox25.Text = 3*1.5
TextBox24.Text = 6*1.5
TextBox23.Text = 2*1.5
TextBox22.Text = 3*1.5
a = 4.5 + 4.5 + 4.5 + 4.5 + 4.5 + 4.5 + 9 + 3 + 4.5
textbox31.text = a/29
heres the code
Dim a As Integer
TextBox30.Text = TextBox4.Text * ComboBox5.Text
TextBox29.Text = TextBox5.Text * ComboBox6.Text
TextBox28.Text = TextBox6.Text * ComboBox7.Text
TextBox27.Text = TextBox7.Text * ComboBox8.Text
TextBox26.Text = TextBox8.Text * ComboBox9.Text
TextBox25.Text = TextBox9.Text * ComboBox10.Text
TextBox24.Text = TextBox10.Text * ComboBox11.Text
TextBox23.Text = TextBox11.Text * ComboBox12.Text
TextBox22.Text = TextBox12.Text * ComboBox13.Text
a = TextBox30.Text + Val(TextBox29.Text) + Val(TextBox28.Text) + Val(TextBox27.Text) + Val(TextBox26.Text) + Val(TextBox25.Text) + Val(TextBox24.Text) + Val(TextBox23.Text) + Val(TextBox22.Text)
TextBox31.Text = (a / 29)

I don't know if it solve your problem, but it's always better to use a datatype like double for decimals. Integer is just for whole numbers.
So try Dim a As Double

a is an integer. a = 44. a/29 = 1.5172413793.
Can avoid some of these problems by ensuring option strict is always on.

Related

How to print out multiple If Statement in Visual Basic

I have been trying to display this if statement in my VB form application with no luck. Below is the code I have attempted.
Dim iNumber1 As Double
Dim iNumber2 As Double
Dim iNumber3 As Double
Dim iNumber4 As Double
Dim iNumber5 As Double
Dim iNumber6 As Double
Dim iNumber7 As Double
Dim iAns As Double
Dim overall As Double
iNumber1 = TextBox1.Text
iNumber2 = TextBox2.Text
iNumber3 = TextBox3.Text
iNumber4 = TextBox7.Text
iNumber5 = TextBox6.Text
iNumber6 = TextBox5.Text
iNumber7 = TextBox8.Text
iAns = iNumber1 + iNumber2 + iNumber3 + iNumber4 + iNumber5 + iNumber6 + iNumber7
overall = (Math.Round(Val(iAns / 7), 1))
If iNumber1 <= 1.9 Then
TextBox4.Text = $"Comprehension: {iNumber1} Entering"
ElseIf iNumber1 >= 2.9 And iNumber1 >= 2 Then
TextBox4.Text = $"Comprehension: {iNumber1} Emerging"
ElseIf iNumber1 >= 3.9 And iNumber1 >= 3 Then
TextBox4.Text = $"Comprehension: {iNumber1} Developing"
ElseIf iNumber1 >= 4.9 And iNumber1 >= 4 Then
TextBox4.Text = $"Comprehension: {iNumber1} Expanding"
ElseIf iNumber1 >= 5.9 And iNumber1 >= 5 Then
TextBox4.Text = $"Comprehension: {iNumber1} Bridging"
ElseIf iNumber1 >= 6.9 And iNumber1 >= 6 Then
TextBox4.Text = $"Comprehension: {iNumber1} Reaching"
End If
Any suggestions on how to get this to come out? I am totally stuck. Thanks.

VB ElseIf Statement Limited to 1

I was forced by college to do a application in Visual Studio (Basic) and I never had expierence with this language before so I'm confused. I did If statement which is working and then I've added EleseIf which is also working but after I've added Second ElseIf it doesn't work. It seems like only If statement and the first ElseIf is working for me but I need more than just 1 ElseIf statement.
Public Class receipt
Private Sub receipt_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim outputValue As Decimal = My.Settings.outputamount.Remove(4)
Dim calculation As Decimal = outputValue * My.Settings.inputamount
Dim totalwithoutcharge As String = calculation.ToString
customername.Text = "Name: " + My.Settings.Username
Label6.Text = "Entered Money: " + My.Settings.inputamount + " " + My.Settings.currency
Label7.Text = "Converted To: " + totalwithoutcharge + " " + My.Settings.outputcurrency
If calculation < 100 Then
Label8.Text = "Charge: 0%"
Label9.Text = "Total: " + totalwithoutcharge + " " + My.Settings.outputcurrency
ElseIf calculation > 100 Then
Label8.Text = "Charge: 1%"
Label9.Text = "Total: " + totalwithoutcharge + " " + My.Settings.outputcurrency
ElseIf calculation > 500 Then
Label8.Text = "Charge: 2%"
Label9.Text = "Total: " + totalwithoutcharge + " " + My.Settings.outputcurrency
ElseIf calculation > 1000 Then
Label8.Text = "Charge: 3%"
Label9.Text = "Total: " + totalwithoutcharge + " " + My.Settings.outputcurrency
Else
Label8.Text = "Something went wrong"
End If
End Sub
End Class
I've entered many values I've add over 600 and it was still showing 'Charge: 1%' where it should show 2% instead. The same happen with values higher than 1000 it will still show the 1%. But when the value is below 100 then is OK and it is showing 0%.
Over 100 works fine too
Over 500 doesn't work
I've also tried to do it with two conditions where it would look at range but it didn't work too.
I've tried 'ElseIf calculation > 100 And calculation > 500 Then' but there is no difference.
First of all use & instead of + when concentating strings.
Second if calculation is > 500 it is also greater than 100, 200 etc. So you need to implement a range. So think about the logic.
Based on my comment
If calculation >= 1000 Then
ElseIf calculation >= 500 Then
ElseIf calculation >= 100 Then
Else
'lass than 100
End If

ElseIf statement wont run the whole code VB

Ok so I've been working on a program I'm making and for some reason when I put a bit of my code into an else if statement, it won't run the statement at all not even the original If statement, However when I put it in a regular If and else statement it works perfectly.
This bit of code works:
If TextBox5.Text > TextBox1.Text & TextBox6.Text > TextBox2.Text Then 'NE
Bearing = Atan(X2 / Y2) * 57.3
Else
Bearing = Atan(Y2 / X2) * 57.3
Bearing = Bearing + -Bearing + -Bearing + 90
If BOFF > 0 Then
Bearing = Bearing - Math.Round(BOFF)
Else
Bearing = Bearing + Math.Round(BOFF)
End If
End If
And this bit of code doesnt work:
If TextBox5.Text > TextBox1.Text & TextBox6.Text > TextBox2.Text Then 'NE
Bearing = Atan(X2 / Y2) * 57.3
ElseIf TextBox5.Text > TextBox1.Text & TextBox6.Text < TextBox2.Text Then
Bearing = Atan(Y2 / X2) * 57.3
Bearing = Bearing + -Bearing + -Bearing + 90
If BOFF > 0 Then
Bearing = Bearing - Math.Round(BOFF)
Else
Bearing = Bearing + Math.Round(BOFF)
End If
End If
Could be a syntax error something, but it doesn't come up with an error at all, runs the code perfectly and all that. I don't know what I'm doing wrong here.
You should use AND instead of &
AND is the logical operator on the other hand & is for joining strings
It is the logical as well as bitwise AND operator. If both the
operands are true, then condition becomes true. This operator does not
perform short-circuiting, i.e., it evaluates both the expressions.
If TextBox5.Text > TextBox1.Text And TextBox6.Text > TextBox2.Text Then 'NE
'Code
ElseIf TextBox5.Text > TextBox1.Text And TextBox6.Text < TextBox2.Text Then
'Code
End If
Note:
If values of TextBox1.Text =4, TextBox6.Text=1 then TextBox1.Text & TextBox6.Text will yields 41
See this demo
& is used to concatenate two strings

The datasource doesn't contain a datamember with the "duration" name

By using Dev express chart control am populating some of charts which are based on today,yesterday,week ,months and year based query.But the Problem is that am getting this error .
The datasource doesn't contain a datamember with the "duration" name.
where getDataset is global function which am using from last 6 months which is perfectly alright.plz help. help is appreciated
Here is my complete method
Protected Sub populateLoginHistoryChart(ByVal ddlSelectedValue As String)
Dim sqlQuery As String = Nothing
sqlQuery = "SELECT LT.LogOnTime AS duration , COUNT(*) AS posts "
sqlQuery += " FROM ( "
sqlQuery += "SELECT CASE"
sqlQuery += " When DATEPART(hh,[LogOnTime]) >= 0 AND DATEPART(hh,[LogOnTime]) <= 4 AND DATEPART(mm,[LogOnTime]) <= 59 then '00:00-04:00'"
sqlQuery += " When DATEPART(hh,[LogOnTime]) >= 4 AND DATEPART(hh,[LogOnTime]) <= 8 AND DATEPART(mm,[LogOnTime]) <= 59 then '04:00-08:00' "
sqlQuery += " When DATEPART(hh,[LogOnTime]) >= 8 AND DATEPART(hh,[LogOnTime]) <= 12 AND DATEPART(mm,[LogOnTime]) <= 59 then '08:00-12:00' "
sqlQuery += " When DATEPART(hh,[LogOnTime]) >= 12 AND DATEPART(hh,[LogOnTime]) <= 16 AND DATEPART(mm,[LogOnTime]) <= 59 then '12:00-16:00' "
sqlQuery += " When DATEPART(hh,[LogOnTime]) >= 16 AND DATEPART(hh,[LogOnTime]) <= 20 AND DATEPART(mm,[LogOnTime]) <= 59 then '16:00-20:00' "
sqlQuery += " When DATEPART(hh,[LogOnTime]) >= 20 AND DATEPART(hh,[LogOnTime]) <= 23 AND DATEPART(mm,[LogOnTime]) <= 59 then '20:00-23:00' "
sqlQuery += " End as LogOnTime "
sqlQuery += " FROM StudentLoginHistory"
If ddlSelectedValue = "Today" Then
sqlQuery += " WHERE DATEADD(dd, 0, DATEDIFF(dd, 0, LogOnTime)) = DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))"
sqlQuery += " )LT "
ElseIf ddlSelectedValue = "Yesterday" Then
sqlQuery += " WHERE DATEADD(dd, 0, DATEDIFF(dd, 0, LogOnTime)) = DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))-1 "
sqlQuery += " )LT "
End If
sqlQuery += " GROUP BY LT.LogOnTime"
sqlQuery += " ORDER BY duration"
If ddlSelectedValue = "Week" Then
sqlQuery = "SELECT CONVERT(varchar(25),LogOnTime,107) AS duration, count(*) AS posts"
sqlQuery += " FROM StudentLoginHistory WHERE "
sqlQuery += " DateDiff(Week,LogOnTime,GetDate())=1"
sqlQuery += " GROUP BY CONVERT(varchar(25),LogOnTime,107)"
sqlQuery += " ORDER BY duration"
ElseIf ddlSelectedValue = "Month" Then
sqlQuery = "SELECT LT.LogOnTime AS duration, COUNT(*) AS posts"
sqlQuery += " FROM (SELECT CASE "
sqlQuery += " When DATEPART(dd,[LogOnTime]) >= 1 AND DATEPART(dd,[LogOnTime]) <= 7 then '2013-05-01 to 2013-05-07'"
sqlQuery += " When DATEPART(dd,[LogOnTime]) >= 8 AND DATEPART(dd,[LogOnTime]) <= 14 then '2013-05-08 to 2013-05-14'"
sqlQuery += " When DATEPART(dd,[LogOnTime]) >= 15 AND DATEPART(dd,[LogOnTime]) <= 21 then '2013-05-15 to 2013-05-21' "
sqlQuery += " When DATEPART(dd,[LogOnTime]) >= 22 AND DATEPART(dd,[LogOnTime]) <= 28 then '2013-05-22 to 2013-05-28'"
sqlQuery += " End as LogOnTime "
sqlQuery += " FROM StudentLoginHistory"
sqlQuery += " WHERE(DatePart(mm, LogOnTime) = DatePart(mm, DateAdd(mm, -1, getdate())) AND DatePart(yyyy, LogOnTime) = DatePart(yyyy, DateAdd(mm, -1, getdate())))"
sqlQuery += " )LT "
sqlQuery += " GROUP BY LT.LogOnTime"
ElseIf ddlSelectedValue = "Year" Then
sqlQuery = "SELECT CONVERT(varchar(4),LogOnTime , 100) + CONVERT(varchar(4), LogOnTime, 120) AS duration,COUNT(*) AS posts"
sqlQuery += " FROM StudentLoginHistory WHERE "
sqlQuery += " year(LogOnTime) = year(DATEADD(year,-1,getdate())) "
sqlQuery += "GROUP BY CONVERT(varchar(4),LogOnTime , 100) + CONVERT(varchar(4), LogOnTime, 120) "
sqlQuery += "ORDER BY month(CONVERT(varchar(4),LogOnTime , 100) + CONVERT(varchar(4), LogOnTime, 120))"
End If
Dim ds As DataSet = objglb.getDataset(sqlQuery, "tbl")
Dim Title As New DevExpress.XtraCharts.ChartTitle()
Dim Diagram As XYDiagram = CType(LoginHistoryChart.Diagram, XYDiagram)
LoginHistoryChart.Titles.Clear()
Dim Series As New Series("Series", ViewType.Bar)
LoginHistoryChart.Series.Add(Series)
Series.DataSource = ds
Series.ArgumentDataMember = "duration"
Series.ValueScaleType = ScaleType.Numerical
Series.ValueDataMembers.AddRange(New String() {"posts"})
If ddlSelectedValue = "Today" Then
Title.Text = "Login History By Today"
ElseIf ddlSelectedValue = "Yesterday" Then
Title.Text = "Login History By Yesterday"
ElseIf ddlSelectedValue = "Week" Then
Title.Text = "Login History By Last Week"
ElseIf ddlSelectedValue = "Month" Then
Title.Text = "Login History By Last Month"
ElseIf ddlSelectedValue = "Year" Then
Title.Text = "Login History By Last Year"
End If
'the appearance of the X-axis title.
Diagram.AxisX.Title.Visible = True
Diagram.AxisX.Title.Alignment = StringAlignment.Center
If ddlSelectedValue = "Today" OrElse ddlSelectedValue = "Yesterday" Then
Diagram.AxisX.Title.Text = "Hours"
ElseIf ddlSelectedValue = "Week" Then
Diagram.AxisX.Title.Text = "Days"
ElseIf ddlSelectedValue = "Month" Then
Diagram.AxisX.Title.Text = "Weeks"
ElseIf ddlSelectedValue = "Year" Then
Diagram.AxisX.Title.Text = "Months"
End If
Diagram.AxisX.Title.TextColor = Color.Black
Diagram.AxisX.Title.Font = New Font("georgia", 12, FontStyle.Regular)
Diagram.AxisX.Range.Auto = False
'the appearance of the Y-axis title.
Diagram.AxisY.Title.Visible = True
Diagram.AxisY.Title.Alignment = StringAlignment.Center
Diagram.AxisY.Title.Text = "Logins Counts"
Diagram.AxisY.Title.TextColor = Color.Black
Diagram.AxisY.Title.Antialiasing = True
Diagram.AxisY.Title.Font = New Font("georgia", 10)
CType(Series.View, SideBySideBarSeriesView).ColorEach = True
LoginHistoryChart.Width = 900
LoginHistoryChart.Legend.Visible = False
LoginHistoryChart.Titles.Add(Title)
Title.Font = New Font("georgia ", 14)
End Sub
I don't see that you are setting the name of the table to bind to anywhere. I would change
Series.DataSource = ds
to
Series.DataSource = ds.Tables["tbl"]

Loops - Adding Numbers - Visual Basic

So the program has to add all the numbers from "x" to "y".
But it also has to display all the numbers added :
i.e. 10 to 20 should display 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 = 165
Here's what I have:
Dim firstnum As Integer = Val(TextBox1.Text)
Dim secondnum As Integer = Val(TextBox2.Text)
Dim sum As Integer = 0
While firstnum <= secondnum
sum = sum + firstnum
firstnum = firstnum + 1
Label3.Text = firstnum & "+"
End While
suum.Text = " = " & Val(sum)
With the following:
Label3.Text = firstnum & "+"
You are overwriting the value in Label3 every time you go through the loop. What you probably want to do is concatenate the existing value with the next number.
This should get you on your way:
Label3.Text = Label3.Text & firstnum & " + "
Is Linq ok? Then you can use Enumerable.Range and Enumerable.Sum:
Dim startNum = Int32.Parse(TextBox1.Text)
Dim endNum = Int32.Parse(TextBox2.Text)
Dim numbers = Enumerable.Range(startNum, endNum - startNum + 1) 'inclusive, therefore + 1
Label3.Text = String.Join(" + ", numbers)
suum.Text = numbers.Sum()
your Label3.Text will only contain the last num and "+" at the end of the algorithm. You should replace
Label3.Text = firstnum & "+"
with
Label3.Text = Label3.Text & firstnum & "+ "