Simple multiplication progran help needed
Hi I just started playing around with Visual Basics and I tried to make a simple program to work out a price on a square item of steel, but I'm stuck. The program has to multiply the length, width, thickness, the rate, and the mass 8.3 rounded off standard and then decided by 100000 to get rid of all the 0's.
So If we have a 300 square plate that's 12mm thick the calculation will be 300 x 300 x 12 (thickness) x 12(rate in Rands) x 8.3 (constant) / 1000000 the answer will be 107.568.
Please help me this is what I have done.
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim num1, num2, num3, num4, num5, num6, product As Single
num1 = TextBox1.Text
num2 = TextBox2.Text
num3 = TextBox3.Text
num4 = TextBox4.Text
num5 = Val(8.3)
num6 = Val(1000000)
product = num1 * num2 * num3 * num4 * num5 / num6
End Sub
Re: Simple multiplication progran help needed
Hi,
You have not mentioned what is going wrong so I cannot specifically comment on your problem but the one thing I would say is to remember basic school maths. What does BODMAS mean?
Hope that helps.
Cheers,
Ian
Re: Simple multiplication progran help needed
Sorry about that. The Program doesn't work. nothing happens and I get an error "Conversion from string "" to type 'Single' is not valid."
I'm stuck I just started working on VB today just playing around.
Re: Simple multiplication progran help needed
So the math will be as followes.
(300x300x12x12x8.3) \ 1000000 = 107.568
Quote:
Originally Posted by
IanRyder
Hi,
You have not mentioned what is going wrong so I cannot specifically comment on your problem but the one thing I would say is to remember basic school maths. What does BODMAS mean?
Hope that helps.
Cheers,
Ian
Re: Simple multiplication progran help needed
Hi,
A few things for you then:-
1) Since you are just starting out with VB turn Option Strict ON and Option Explicit On in your project settings and leave them on for all your projects. This will help you to avoid bad practices from the start.
2) With Option Strict On, this will immediately tell you where you are currently getting your type conversion error so that you can see what you need to deal with.
3) If you need to Explicitly set any values in your projects then these should always be declared as constants. i.e:-
Code:
Const Num5 As Single = 8.3
Const Num6 As Single = 1000000
4) When declaring variables in your project then always use descriptive names since Num1, Num2 etc etc means nothing and will easily confuse you later on in your programming.
5) The next thing to do is to always validate any input from the project before you use them in your calculations. For instance, you are looking for 4 single type values to be returned from your 4 TextBoxes, but what would happen if someone were to type text values into these TextBoxes? So, you would use TryParse of the type you are expecting to validate any input before you use it. i.e:-
Code:
Dim Num1, Num2, Num3, Num4 As Single
If Single.TryParse(TextBox1.Text, Num1) AndAlso Single.TryParse(TextBox2.Text, Num2) _
AndAlso Single.TryParse(TextBox3.Text, Num3) AndAlso Single.TryParse(TextBox4.Text, Num4) Then
'Write your valid calculation code here
Else
MessageBox.Show("Invalid Data in TextBoxes!")
End If
6) Once all the above is done the calculation should now be simple:-
Code:
Product = (Num1 * Num2 * Num3 * Num4 * Num5) / Num6
Be careful with the division character that you are using. In your last post you used the character "\" which is the symbol to perform INTEGER DIVISION and not standard division.
Hope that helps.
Cheers,
Ian
Re: Simple multiplication progran help needed
Quote:
Originally Posted by
IanRyder
Hi,
A few things for you then:-
1) Since you are just starting out with VB turn Option Strict ON and Option Explicit On in your project settings and leave them on for all your projects. This will help you to avoid bad practices from the start.
2) With Option Strict On, this will immediately tell you where you are currently getting your type conversion error so that you can see what you need to deal with.
3) If you need to Explicitly set any values in your projects then these should always be declared as constants. i.e:-
Code:
Const Num5 As Single = 8.3
Const Num6 As Single = 1000000
4) When declaring variables in your project then always use descriptive names since Num1, Num2 etc etc means nothing and will easily confuse you later on in your programming.
5) The next thing to do is to always validate any input from the project before you use them in your calculations. For instance, you are looking for 4 single type values to be returned from your 4 TextBoxes, but what would happen if someone were to type text values into these TextBoxes? So, you would use TryParse of the type you are expecting to validate any input before you use it. i.e:-
Code:
Dim Num1, Num2, Num3, Num4 As Single
If Single.TryParse(TextBox1.Text, Num1) AndAlso Single.TryParse(TextBox2.Text, Num2) _
AndAlso Single.TryParse(TextBox3.Text, Num3) AndAlso Single.TryParse(TextBox4.Text, Num4) Then
'Write your valid calculation code here
Else
MessageBox.Show("Invalid Data in TextBoxes!")
End If
6) Once all the above is done the calculation should now be simple:-
Code:
Product = (Num1 * Num2 * Num3 * Num4 * Num5) / Num6
Be careful with the division character that you are using. In your last post you used the character "\" which is the symbol to perform INTEGER DIVISION and not standard division.
Hope that helps.
Cheers,
Ian
Thank you Ian I have tried to code it better but I can't seem to fix the code now. This is what I've done.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Length, Width, Thickness, Rate, Quantity, Price As Single, Weight
Const steelweight As Single = 7.85
Const Decimalfix As Single = 1000000
Const steelcost As Single = 8.3
If Single.TryParse(Lengthbox.Text, Length) AndAlso Single.TryParse(Width.Text, Width) _AndAlso Single.TryParse(Thicknessbox.Text, Thickness) AndAlso Single.TryParse(Ratebox.Text, Rate) Then
'Write your valid calculation code here
Length = Lengthbox.Text
Width = Widthbox.Text
Thickness = Thicknessbox.Text
Rate = Ratebox.Text
Quantity = Quantitybox.Text
Weightbox = (Length * Width * Thickness * steelweight) / Decimalfix
Price = (Length * Width * Thickness * steelcost) / Decimalfix
Quantity = (Price * Quantity)
Weightbox.Text = Weight
Price = Pricebox.Text
Weightbox.Text = CStr(Weight) + " kg"
But it does not work.
Please help me.
Re: Simple multiplication progran help needed
Hi Ian I managed to fix the code but I still dont know how to use:
Code:
If Single.TryParse(Lengthbox.Text, Length) AndAlso Single.TryParse(Width.Text, Width) _AndAlso Single.TryParse(Thicknessbox.Text, Thickness) AndAlso Single.TryParse(Ratebox.Text, Rate) Then
'Write your valid calculation code here
Here is the correct code there is only one problem I need it to show 2 decimals now it fills up the text box with them.
Code:
Dim Length, Width, Thickness, Rate, Quantity, Price, Weight
Const steelweight As Single = 7.85
Const Decimalfix As Single = 1000000
Const steelcost As Single = 8.3
Length = Lengthbox.Text
Width = Widthbox.Text
Thickness = Thicknessbox.Text
Rate = Ratebox.Text
Quantity = Quantitybox.Text
Weight = (Length * Width * Thickness * steelweight) / Decimalfix
Price = (Length * Width * Thickness * Rate * steelcost) / Decimalfix
Totalbox.Text = (Price * Quantity)
Weightbox.Text = Weight
Pricebox.Text = Price
Weightbox.Text = CStr(Weight) + " kg"
Re: Simple multiplication progran help needed
This is what I've got now:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Length, Width, Thickness, Rate, Quantity, Price, Weight
Const steelweight As Single = 7.85
Const Decimalfix As Single = 1000000
Const steelcost As Single = 8.3
Length = Lengthbox.Text
Width = Widthbox.Text
Thickness = Thicknessbox.Text
Rate = Ratebox.Text
Quantity = Quantitybox.Text
Weight = (Length * Width * Thickness * steelweight) / Decimalfix
Price = (Length * Width * Thickness * Rate * steelcost) / Decimalfix
Totalbox.Text = (Price * Quantity)
Weightbox.Text = Weight
Pricebox.Text = Price
Weightbox.Text = CStr(Weight) + " kg"
If Single.TryParse(Lengthbox.Text, Length) AndAlso Single.TryParse(Widthbox.Text, Width) AndAlso Single.TryParse(Thicknessbox.Text, Thickness) AndAlso Single.TryParse(Ratebox.Text, Rate) Then
'Write your valid calculation code here
Else
MessageBox.Show("Invalid Data in TextBoxes!")
End If
End Sub
Re: Simple multiplication progran help needed
Hi,
Quote:
If Single.TryParse(Lengthbox.Text, Length) AndAlso Single.TryParse(Width.Text, Width) _AndAlso Single.TryParse(Thicknessbox.Text, Thickness) AndAlso Single.TryParse(Ratebox.Text, Rate) Then
'Write your valid calculation code here
Length = Lengthbox.Text
Width = Widthbox.Text
Thickness = Thicknessbox.Text
Rate = Ratebox.Text
Quote:
I managed to fix the code but I still dont know how to use:
If Single.TryParse(Lengthbox.Text, Length) AndAlso Single.TryParse(Width.Text, Width) _AndAlso Single.TryParse(Thicknessbox.Text, Thickness) AndAlso Single.TryParse(Ratebox.Text, Rate) Then
'Write your valid calculation code here
The first thing to do if there is something that you do not understand is to read the Documentation for the method that you are using. MSDN has a wealth of information on every Method and Property for Every Class and Type in VB.NET. Have a look here on the Single.TryParse method:-
http://msdn.microsoft.com/en-us/library/26sxas5t.aspx
Once you have read this you should be able to structure your code correctly.
With regards to rounding, have a look here:-
http://msdn.microsoft.com/en-gb/library/zy06z30k.aspx
Hope that helps.
Cheers,
Ian
Re: Simple multiplication progran help needed
You still haven't turned Option Strict ON, because you are still using implicit conversions. Not only are implicit conversions slow, but they are prone to exceptions, as well, because they don't always do what you expect them to do and they don't always work at all.
Re: Simple multiplication progran help needed
Moved the thread from Maths Forum
Also Chris450, could you please add the code tags when posting the code. It becomes easier to read it. I have done it for you right now :)