PDA

Click to See Complete Forum and Search --> : Need help pls 'fraction division in VB 2005


ChemEng
Dec 27th, 2008, 02:01 PM
Hello,,,,,,,,,,,,,,,

Would some1 please help me to let the below code work for proper fractions( positive numbers less than 1)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim D1, D2 As Integer
D1 = CInt(TextBox1.Text)
D2 = CInt(TextBox2.Text)

TextBox3.Text = D2 / D1

End Sub
End Class

If I put 0.1 in textbox2 and 0.3 in textbox1 the results in textbox3 is NaN ??!!!:(


Cheers

Pradeep1210
Dec 27th, 2008, 02:15 PM
That's because you are converting to Integer before division. You should use SINGLE, DOUBLE or DECIMAL datatype to handle decimal points.


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim D1, D2 As Double
D1 = CDbl(TextBox1.Text)
D2 = CDbl(TextBox2.Text)

TextBox3.Text = D2 / D1

End Sub
End Class

ChemEng
Dec 27th, 2008, 03:40 PM
Loads of cheers Pradeep1210. It's really working now. I just unable to find the way to rate the answer u posted, otherwise it is 10/10.

many thanks.

Zach_VB6
Dec 27th, 2008, 06:35 PM
Click this button:

http://www.vbforums.com/images/buttons/reputation.gif

ChemEng
Dec 27th, 2008, 06:45 PM
Gents,

Since I'm working with the same problems above, would someone please guide me through my code below, since I got an error to process it!!

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim v As Double ' fluid velocity
Dim Qv as integer = textbox1.text ' volumertric flow rate
Dim D2 as integer = textbox2.text
v = (4 * Qv) / (Math.PI * D2 ^ 2)
v = CDbl(TextBox9.Text)
End sub

ChemEng
Dec 27th, 2008, 06:46 PM
Click this button:

http://www.vbforums.com/images/buttons/reputation.gif

thanks Zach

jemidiah
Dec 28th, 2008, 04:17 AM
I think you at least want "v = CDbl(TextBox9.Text)" to be "TextBox9.Text = Val(v)". Otherwise the line above it does nothing.

Pradeep1210
Dec 28th, 2008, 07:06 AM
Gents,

Since I'm working with the same problems above, would someone please guide me through my code below, since I got an error to process it!!

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim v As Double ' fluid velocity
Dim Qv as integer = textbox1.text ' volumertric flow rate
Dim D2 as integer = textbox2.text
v = (4 * Qv) / (Math.PI * D2 ^ 2)
v = CDbl(TextBox9.Text)
End sub

I think you better get familiar with different datatypes before you continue further with number manipulation. So in the above code you would be able to find the problem yourself.
68164
Integers are numbers without any floating points. If you assign a decimal value to any datatype which can handle only integers (e.g. byte,short,integer,long) the decimal point will be stripped off; or worse cause an error. So if you want decimals to be considered, you must use one of those datatypes that can handle floating points. (e.g. single, double, decimal, currency etc.)

ChemEng
Dec 29th, 2008, 05:25 PM
I think you at least want "v = CDbl(TextBox9.Text)" to be "TextBox9.Text = Val(v)". Otherwise the line above it does nothing.


Many thanks Jemidiah and your code worked well.:wave:

ChemEng
Dec 29th, 2008, 05:29 PM
I think you better get familiar with different datatypes before you continue further with number manipulation. So in the above code you would be able to find the problem yourself.
68164
Integers are numbers without any floating points. If you assign a decimal value to any datatype which can handle only integers (e.g. byte,short,integer,long) the decimal point will be stripped off; or worse cause an error. So if you want decimals to be considered, you must use one of those datatypes that can handle floating points. (e.g. single, double, decimal, currency etc.)

I appreciate your answer and I'm trying to understand the table by putting more examples and excersies to figure out the difference between each data type.

Load of cheers :thumb:

ChemEng
Dec 30th, 2008, 11:51 AM
Good evening..

As I went through the difference between each data type, I got a short code but :( I got it wrong somewhere in the declarations.

The code is below and please please I need your support here to run it :

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Qv As Double
Dim Ao, A1 As Double
Dim D1 As Integer = TextBox1.Text

Dim D2 As Integer = TextBox2.Text
Ao = (Math.PI * D2 ^ 2) / 4
TextBox3.Text = Val(Ao) ' area of the orifice

A1 = (Math.PI * D1 ^ 2) / 4
TextBox4.Text = Val(A1) ' area of the pipe
Dim P1 As Integer = TextBox5.Text
Dim P2 As Integer = TextBox6.Text
Dim CD As Double = TextBox7.Text
Dim rho As Integer = TextBox8.Text
Qv = ((CD * Ao) / (Math.Sqrt(1 - (Ao / A1) ^ 2))) * Math.Sqrt((2 * (P1 - P2)) / (rho))
TextBox9.Text = Val(Qv)

End Sub


loads of cheers:wave:

Pradeep1210
Dec 30th, 2008, 02:39 PM
Good evening..

As I went through the difference between each data type, I got a short code but :( I got it wrong somewhere in the declarations.

The code is below and please please I need your support here to run it :

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Qv As Double
Dim Ao, A1 As Double
Dim D1 As Integer = TextBox1.Text

Dim D2 As Integer = TextBox2.Text
Ao = (Math.PI * D2 ^ 2) / 4
TextBox3.Text = Val(Ao) ' area of the orifice

A1 = (Math.PI * D1 ^ 2) / 4
TextBox4.Text = Val(A1) ' area of the pipe
Dim P1 As Integer = TextBox5.Text
Dim P2 As Integer = TextBox6.Text
Dim CD As Double = TextBox7.Text
Dim rho As Integer = TextBox8.Text
Qv = ((CD * Ao) / (Math.Sqrt(1 - (Ao / A1) ^ 2))) * Math.Sqrt((2 * (P1 - P2)) / (rho))
TextBox9.Text = Val(Qv)

End Sub


loads of cheers:wave:
Have a look at one of those variables that you declared INTEGER that might be causing the problem. To be on the safe side, you could declare all of them as DOUBLE.

ChemEng
Dec 30th, 2008, 03:14 PM
Have a look at one of those variables that you declared INTEGER that might be causing the problem. To be on the safe side, you could declare all of them as DOUBLE.


That is true Pradeep, I found the problem and it looks working now,

many thanks again and again :wave:

ChemEng
Dec 30th, 2008, 03:33 PM
Pradeep, Jemidiah and other experts;

I can say I'm almost in the finishings to end up my VB work, but I've the following code to be viewed and corrected please.
The code shall converts the textbox value according to the unit selected from the combobox. Example, if I enter 0.2 in the text box and put m as meter the value shall stay the same since m is the SI unit for length. Whereas if I change the unit to cm or mm from the combobox the value of 0.2 shall be converted into cm or mm according to the combobox unit selected, and visa versa if I put the value in cm or mm and want to change it to m then I just need to select the unit from the combobox.
I put the code but when I've tried to return it to m I failed as it keep converting, which gives wrong figures.

The code is

Public Class Form1
Dim unit_d1 As Single = 1 'multiple input in D1 by this value to get into SI units
Dim unit_d2 As Single = 1
Dim DI As Double 'pipe internal dia

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim unit As String
unit = ComboBox1.Text ' get current value from combo box

If unit = "m" Then
DI = DI * 1.0
ElseIf unit = "cm" Then
DI = DI * 0.01
ElseIf unit = "mm" Then
DI = DI * 0.001
Else
If TextBox1.Text = " " Then
MsgBox(" please enter a valid ID value")

End If
End If
TextBox1.Text = DI
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.Items.Add("m")
ComboBox1.Items.Add("cm")
ComboBox1.Items.Add("mm")
ComboBox1.SelectedIndex = 0

TextBox1.Text = "0.2"
DI = CSng(TextBox1.Text)
End Sub

Private Sub TextBox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
'Dim DI As Double 'Commented by Mr.Z !!

If (TextBox1.Text.Length > 0) Then ' dont check empty textbox
DI = CSng(TextBox1.Text) ' only apply the unit multiplier when the calc button is pressed.
If (DI > 0.4) Or (DI <= 0.1) Then
MsgBox(" Pipe ID to be from >0.1m to 0.4m ")
End If
End If
End Sub
End Class

Loads of cheers

Pradeep1210
Dec 30th, 2008, 11:25 PM
You are not storing the value of textbox anywhere before changing it. So whenever combobox value changes, only the latest value is assumed to be in metres. You should store the actual metre value somewhere before applying the change.
Also, the Units and the Multiplier go side by side. So instead of calculating the multiplier in the combobox change event, you could keep it with the item itself. Since the combobox Items is a collection of objects, you can store both the things (maybe more properties if you want) in the Items collection as a Class or Structure.


PS: Please put your code in [CODE] or [HIGHLIGHT] tags to make it more readable.


Code removed and posted modified code in next post


Pradeep :)

Pradeep1210
Dec 31st, 2008, 12:50 AM
I think this is what you need:


Public Class Form1
Dim DI As Double 'pipe internal dia

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
TextBox1.Text = DI * ComboBox1.SelectedItem.Multiplier
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.Items.Add(New SIUnit("m", 1))
ComboBox1.Items.Add(New SIUnit("cm", 100))
ComboBox1.Items.Add(New SIUnit("mm", 1000))
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
ComboBox1.SelectedIndex = 0

TextBox1.Text = "0.2"
DI = CDbl(TextBox1.Text)
End Sub

Private Sub TextBox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
If (TextBox1.Text.Length > 0) Then ' dont check empty textbox
If (DI > 0.4) Or (DI <= 0.1) Then
MsgBox(" Pipe DI to be from >0.1m to 0.4m ")
End If
End If
End Sub

Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
DI = Val(TextBox1.Text) / ComboBox1.SelectedItem.Multiplier
End Sub

End Class

Public Structure SIUnit
Public Unit As String
Public Multiplier As Single
Public Sub New(ByVal unit As String, ByVal multiplier As Single)
Me.Unit = unit
Me.Multiplier = multiplier
End Sub
Public Overrides Function ToString() As String
Return Me.Unit
End Function
End Structure

ChemEng
Dec 31st, 2008, 05:45 PM
Happy New Year every1 and wish all our dreams turn true. Wish the peace to spread over the world. Wish all the people live in an equity and prosperity:) .

Thanks very much Pradeep for your time and valuable comments and I highly appreciate your support and concern. Thanks for Jemidiah as well. Many thanks for everyone tried to put some effort in this issue.:wave:

ChemEng
Jan 1st, 2009, 03:49 PM
Hi again :o

I have a combobox with drop down list say water, Oil, air and Gas.
I have a volumetric flow rate formula is used to calculate the flow when the combobox selection is water, oil and air. Whereas for Gas I have different formula. Now, I need to switch the volumetric flow rate calculation according to the combobox selection.

Here is the code (refering to above help from Pradeep and Jemidiah)


Qv = ((CD * A2) / (Math.Sqrt(1 - (A2 / A1) ^ 2))) * Math.Sqrt((2 * (P1 - P2)) / (rho)) ' formula cited from Perry chapter 8-48 Process Control
TextBox12.Text = Val(Qv) 'Calculated volumetric flow rate based on selected orifice size
If ComboBox6.SelectedIndex = 3 Then ' selection 3 refers to Hydrocarbon Gas
Dim expcoeff, Qv As Double ' expcoeff is e or expansion coefficient for gas

Qv = ((CD * expcoeff * (Math.Sqrt(1 / (1 - (D2 / D1) ^ 4))) * ((Math.PI * D2 ^ 2) / 4) * (Math.Sqrt(2 * (P1 - P2)) / (rho))))
TextBox12.Text = Val(Qv)
expcoeff = 1 - (0.41 + 0.35 * (D2 / D1) ^ 4) * ((P1 - P2) / (1.4 * P1)) ' 1.4 refers to k which is a isentropic coefficient for gas
TextBox16.Text = Val(expcoeff)
End If

When I put this code and choise gas from the dropdown list of the combobox the Qv value in the textbox will turn to 0 !!!1

ChemEng
Jan 1st, 2009, 05:24 PM
After many attempts, I've solved the above problem...

Cheers.