I want to calculate Reynolds Number using VB.NET
This is my code:
Public Class Form1
Dim vis As Integer
Dim Den As Integer
Dim hd As Integer
Dim vl As Integer
Dim re As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
vis = Int(TextBox1.Text)
Den = Int(TextBox2.Text)
hd = Int(TextBox4.Text)
vl = Int(TextBox5.Text)
re = (Den * vl * hd) / vl
TextBox3.Show(re)
End Sub
End Class
See my UI here.
Why I am still getting an error message "too many arguments" ?
There are a few things wrong in the code you posted, firstly the calculation is wrong for Reynolds number. Second, please turn on Option Strict as with your current code it would not compile. Third please use conventional naming conventions it makes it difficult in the long run... There's more but not the point...
Suggested Solution
Variable Declaration Meaning:
d = Diameter of the pipe
v = Velocity of Liquid
u = Viscoscity of the Liquid
p = Density of the Liquid
tot = The Reynolds Number
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim d,v,u,p,tot As Single
If Single.TryParse(TextBox1.Text,d) AndAlso Single.TryParse(TextBox2.Text,v) AndAlso Single.TryParse(TextBox3.Text,u) AndAlso Single.TryParse(TextBox1.Text,p) Then
tot = (d * v * p) / (u * 0.001)
MessageBox.Show(tot.ToString)
'OR
TextBox3.Text = tot.ToString
End If
End Sub
The Int function does not do type conversion. It simply returns the integral portion of a value (14.8 would become 14). To do this conversion, you want to use CInt, if you guarantee that the incoming text really is a number.
Since you are using user supplied values, you might want to use some error correction.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Integer.TryParse(TextBox1.Text, vis) AndAlso _
Integer.TryParse(TextBox2.Text, Den) AndAlso _
Integer.TryParse(TextBox4.Text, hd) AndAlso _
Integer.TryParse(TextBox5.Text, vl) Then
'Do your calculation
Else
'There is some kind of error. Don't do the calculation
End If
End Sub
I'm not going to address whether your formula is correct or not.
Related
The code below is a program to calculate the BMI using text boxes. I am having an issue however that when I clear one of the text boxes it will throw an exception and freeze the program. I was wondering if anyone had an answer on how to prevent this. I already tried setting my variables to 0 and 1 to see if that was the issue but it does not appear to be.
Private Sub tboxWeight_TextChanged(sender As Object, e As EventArgs) Handles tboxWeight.TextChanged
Weight = 0
Weight = Convert.ToInt64(tboxWeight.Text)
End Sub
Private Sub tboxHFeet_TextChanged(sender As Object, e As EventArgs) Handles tboxHFeet.TextChanged
Height_feet = 0
Height_feet = Convert.ToInt64(tboxHFeet.Text)
Get_BMI(1)
End Sub
Private Sub tboxHInch_TextChanged(sender As Object, e As EventArgs) Handles tboxHInch.TextChanged
Height_Inches = 0
Height_Inches = Convert.ToInt64(tboxHInch.Text)
Get_BMI(1)
End Sub
Private Sub tboxAge_TextChanged(sender As Object, e As EventArgs) Handles tboxAge.TextChanged
Age = Convert.ToDouble(tboxAge.Text)
End Sub
Function Get_BMI(ByVal j As Integer) As Double
BMI = (Weight / (Height_Inches + (Height_feet * 12) ^ 2) * 703)
tboxBMI.Text = Convert.ToString(BMI)
Exit Function
End function
It is because you set a textbox into an integer field, so when the textbox is empty it will throw exception because the textbox doesn't contain a number.
Try using If else statement for each textboxes.
String.IsNullOrEmpty function will be sufficient.
Good/Best practice says, you need to validate the data before performing calculation i.e. Get_BMI(). Below code snippet will help you.
Dim textBoxValue As String
If Not String.IsNullOrEmpty(textBoxValue) Then
If IsNumeric(textBoxValue) Then
End If
End If
I'm trying to multiply two variables within VB Studio, however I am getting a syntax error when trying to do this.
(Quantity * ItemCost) gives me a syntax error on the first bracket, and removing the brackets gives me the error that method arguments must be enclosed in parenthesis.
Sorry if this was rather vague or really obvious, I am new to programming in general and am doing my best to make simple programs. I know my code is far from the most efficient but it's the best I can do at the moment.
The whole of my code is below :
Public Class Form1
Dim CustomerName As String
Dim Pensioner As Boolean
Dim Takeout As Boolean
Dim Items(4) As Array
Dim CostOfItems(4) As Array
Dim Alpha As Integer = 0
Dim Quantity As Integer
Dim ItemCost As Integer
Private Sub txtName_TextChanged(sender As Object, e As EventArgs) Handles txtName.TextChanged
CustomerName = txtName.Text
End Sub
Private Sub cboItemName_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboItemName.SelectedIndexChanged
If cboItemName.Text = "Tea" Then
txtItemPrice.Text = "£0.50"
ElseIf cboItemName.Text = "Coffee" Then
txtItemPrice.Text = "£0.70"
ElseIf cboItemName.Text = "Orange Juice" Then
txtItemPrice.Text = "£0.80"
ElseIf cboItemName.Text = "Apple Juice" Then
txtItemPrice.Text = "£0.80"
ElseIf cboItemName.Text = "Potato" Then
txtItemPrice.Text = "£1.00"
End If
ItemCost = txtItemPrice.Text
End Sub
Private Sub txtQuantity_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtQuantity.KeyPress
If Asc(e.KeyChar) <> 8 Then
If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
e.Handled = True
End If
End If
Quantity = txtQuantity.Text
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
If chxPension.Checked = True Then
Pensioner = True
chxPension.Hide()
End If
If cbxTakeOut.Checked = True Then
TakeOut = True
cbxTakeOut.Hide()
End If
Quantity * ItemCost
txtQuantity.Text = ""
cboItemName.Text = ""
Alpha += 1
txtName.ReadOnly = True
End Sub
End Class
(I have now changed the ItemCost value to not include a £ or . , however I am still getting a syntax error on the bracket)
The problem with your code is that you are not assigning the result of the multiplication to any variable. Some languages permit that (calculating a value and throwing it away), but apparently VB.NET doesn't. It is trying to save you from a common programmer mistake.
Do it like this instead:
Dim TotalCost As Integer = Quantity * ItemCost
The result of the multiplication will now be stored in the variable TotalCost, which you can use for whatever you need.
Of course, you could also store the result of the calculation in one of the operand variables (assuming, of course, it is not a constant). In this case, judging from the names of the variables, it doesn't make much sense, but sometimes it is appropriate:
ItemCost = Quantity * ItemCost ' probably wrong, but syntactically legal
You also need to declare your variables correctly.
item cost can not be integer.
Item cost should be something like double or decimal
Dim TotalCost as Decimal = Quantity * ItemCost
Could you please remove the £ in your txtItemPrice.Text?
Replace this in your existing codes:
ItemCost = cInt(txtItemPrice.Text)
Quantity = cInt(txtQuantity.Text)
This is to convert the Strings (.text) into Integer
For the computation of Total Cost:
Dim Total As Integer = Quantity * ItemCost
Please Take Note:
Integer data types cannot hold values with decimal. Use Double instead.
I have 2 buttons and a DataGridView with 2 Columns (0 & 1).
The 1st button transfers a randomized cell from the Column(1) to a TextBox. Then, it stores that Cell in variable (a), plus the cell that opposites it in variable (b).
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim rnd As New Random
Dim x As Integer = rnd.Next(0, Form1.DataGridView1.Rows.Count)
Dim y As Integer = 1
Dim a As String = Form1.DataGridView1.Rows(x).Cells(y).Value
Dim b As String = Form1.DataGridView1.Rows(x).Cells(y - 1).Value
TextBox3.Text = a
End Sub
The 2nd button, however, is supposed to compare if another TextBox's text has the same string variable (b) has as Strings. Now, if so, then it has to display a certain message and so on...
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If TextBox4.Text = b Then '<<< ISSUE HERE!
MsgBox("Correct! ^_^")
ElseIf TextBox4.Text = "" Then
MsgBox("You have to enter something first! O_o")
Else
MsgBox("Wrong! >,<")
End If
End Sub
The problem is that the variable (b) is surely not shared across the two "private" subs. And so, there is NOTHING to compare to in the 2nd button's sub! I presume that the solution here is to split the "randomization process" into a separate function, then execute it directly when the 1st button gets activated. Furthermore, that function's variables have to be SHARED somehow, and I certainly don't know how!
Thanks for Mr. Olivier, the code has been improved significantly! Yet, I still encounter a "wrong" comparison issue, somehow!
Dim RND As New Random
Dim x As Integer
Private Function GetCell(ByVal rowIndex As Integer, ByVal cellIndex As Integer) As String
Return Form1.DataGridView1.Rows(rowIndex).Cells(cellIndex).Value
End Function
Private Sub btnRoll_Click(sender As Object, e As EventArgs) Handles btnRoll.Click
x = RND.Next(0, Form1.DataGridView1.Rows.Count)
tbxRoll.Text = GetCell(x, 1)
End Sub
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
If tbxSubmit.Text = GetCell(x, 0) Then
MsgBox("Correct! ^_^")
ElseIf tbxSubmit.Text = "" Then
MsgBox("You have to enter something first! O_o")
Else
MsgBox("Wrong! >,<")
End If
End Sub</code>
Well, unbelievably, I read a guide about "comparison operations" in VB.net and tried out the first yet the most primal method to compare equality - which was to use .Equals() command - and worked like a charm! Thank God, everything works just fine now. ^_^
If tbxSubmit.Text.Equals(GetCell(x, 0)) Then
Alright now... This is going to sound weird! But, following Mr. Olivier's advise to investigate "debug" the code, I rapped the string I'm trying to compare with brackets and realized that it's been outputted after a break-line space! So, I used the following function to remove the "white-space" from both of the comparison strings! And it bloody worked! This time for sure, though. ^_^
Function RemoveWhitespace(fullString As String) As String
Return New String(fullString.Where(Function(x) Not Char.IsWhiteSpace(x)).ToArray())
End Function
If RemoveWhitespace(tbxSubmit.Text) = RemoveWhitespace(GetCell(x, 0)) Then
Turn the local variables into class fields.
Dim rnd As New Random
Dim x As Integer
Dim y As Integer
Dim a As String
Dim b As String
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
x = rnd.Next(0, Form1.DataGridView1.Rows.Count)
y = 1
a = Form1.DataGridView1.Rows(x).Cells(y).Value
b = Form1.DataGridView1.Rows(x).Cells(y - 1).Value
TextBox3.Text = a
End Sub
These fields can now be accessed from every Sub, Function and Property.
Of course Button3_Click must be called before Button2_Click because the fields are initialized in the first method. If this is not the case then you should consider another approach.
Create a function for the Cell access
Private Function GetCell(ByVal rowIndex As Integer, ByVal cellIndex As Integer) _
As String
Return Form1.DataGridView1.Rows(rowIndex).Cells(cellIndex).Value
End Function
And then compare
If TextBox4.Text = GetCell(x, y - 1) Then
...
And don't store the values in a and b anymore. If y is always 1 then use the numbers directly.
If TextBox4.Text = GetCell(x, 0) Then
...
One more thing: give speaking names to your buttons in the properties grid before creating the Click event handlers (like e.g. btnRandomize). Then you will get speaking names for those routines as well (e.g. btnRandomize_Click).
See:
- VB.NET Class Examples
- Visual Basic .NET/Classes: Fields
I am trying to parse a textbox.text with a input value of 15.75. Here is all the code.
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim lnVendorNo, lnInHouseID, lnInventoryPackID As Integer
Dim lcVendProdID, lcVendProdDesc, lcDeliverPack As String
Dim lnDelivPackCost, lnDelivPackCost2 As Short
Integer.TryParse(txtVendorNo.Text, lnVendorNo)
lcVendProdID = txtVendProdID.Text
Integer.TryParse(txtInHouseID.Text, lnInHouseID)
lcVendProdDesc = txtVendProdDesc.Text
lcDeliverPack = txtDeliverPack.Text
txtDeliverPackCost.Text = "15.75"
Decimal.TryParse(txtDeliverPackCost.Text, lnDelivPackCost)
' Value of lnDelivPackCost in watch window is 16 and type is short
lnDelivPackCost2 = Double.Parse(txtDeliverPackCost.Text)
' value of lnDelivPackCost2 in watch window is 16 and type is short
I have another sub with the following code that works just fine.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim value As String
Dim number As Decimal
Dim lnDelivPackCost As Decimal
' Parse a floating-point value with a thousands separator.
value = "1643.57"
If Decimal.TryParse(value, number) Then
Console.WriteLine(number) ' Value of number in watch = 1643.57
End If
txtDeliverPackCost.Text = "15.75"
Decimal.TryParse(txtDeliverPackCost.Text, lnDelivPackCost) ' Value on lnDelivPackCost in watch = 15.75D
End Sub
Can anyone tell me why the parses work on one sub and not the other sub. Is it because of parsing to integers earlier in the sub. I am going bonkers trying to figure this out. Any help would be appreciated.
Larry
I'm hoping you guys can help with a problem that should be simple to solve, I've just had issues finding a solution. In the program that I'm writing some of the textbox's have to be numeric between 1 and 10, and others just have to be numeric. Instead of coding each textbox to verify these parameters I decided to write methods for each of them. I'm having problems passing the arguments and getting it to function correctly. Included is some of my code that shows what I'm trying to accomplish.
Public Shared Sub checkforonetoten(ByVal onetoten As Double)
If (onetoten > 1 & onetoten < 10) Then
Else
MessageBox.Show("Please enter a Number between 1-10", "Error")
End If
End Sub
Public Shared Sub checkfornumber(numCheck As Double)
Dim numericCheck As Boolean
numericCheck = IsNumeric(numCheck)
If (numericCheck = False) Then
MessageBox.Show("Please enter a number", "Error")
End If
End Sub
Private Sub textboxS_TextChanged(sender As Object, e As EventArgs) Handles textboxS.TextChanged
Dim S As Double
S = textboxS.Text
checkfornumber(S)
checkforonetoten(S)
End Sub
One of your main problems is you're converting your text without validating it. You're also programming without the Options On to warn you of bad conversion techniques like you're using in the event handler.
The TryParse method would come in handy here:
Private Sub textboxS_TextChanged(sender As Object, e As EventArgs) Handles textboxS.TextChanged
Dim S As Double
If Double.TryParse(textboxS.Text, S) Then
checkforonetoten(S)
End If
End Sub
Since the TryParse method validates your text and sets the value to 'S', you only need to check the range.
Of course using NumericUpDown controls would make all this moot, since the values will always only be numbers and you can set the range on each one.
one way to structure it is to have one event procedure process the similar TB types:
Private Sub textboxS_TextChanged(sender As Object, e As EventArgs) _
Handles textbox1.TextChanged, textbox12.TextChanged, _
Handles textbox16.TextChanged
Dim S As Double
If Double.TryParse(Ctype(sender, TextBox).Text, S) Then
' or place the Check code here for all the TextBoxes listed above
checkforonetoten(S)
End If
End Sub
The plain numeric kind:
Private Sub textboxQ_TextChanged(sender As Object, e As EventArgs) _
Handles textbox2.TextChanged, textbox6.TextChanged
Dim S As Double
If Double.TryParse(Ctype(sender, TextBox).Text, S) = False Then
MessageBox.Show("Please enter a number", "Error")
End If
End Sub
Rather than calling a function and passing the current TextBox from events (which is fine), have 2 or 3 events process them all. The point is adding/moving the Handles clause to a common event procedure (be sure to delete the old ones).
If you do decide to call a common function, dont do anything in the events (you still have one per TB) and do it all in the common proc:
Private Sub textboxS_TextChanged(sender As Object, e As EventArgs) _
Handles textboxS.TextChanged
checkforonetoten(Sender)
End Sub
private Sub checkforonetoten(tb As Textbox)
Dim S As Double
If Double.TryParse(tb.Text, S) Then
' your check for 1 - 10 on var S
else
' error: not a valid number
End If
end sub
Also:
If (onetoten > 1 & onetoten < 10) Then
should be:
If (onetoten > 1) AndAlso (onetoten < 10) Then