Thursday, December 29, 2011

Visual Basic 2010 - Unit Converter?

I am attempting to make a unit converter that converts between km, m, cm, miles, yards, feet, and inches. I have a textbox that is used to input the value to convert followed by a space and the units (i.e., 56 km) and a second textbox where the user types what unit they want to convert to (i.e., cm). When converting, the output will be displayed in the second textbox. (I know this is complicated, but this is the way it is to be done).


I am doing this using sub and function procedures and If blocks and Select Case blocks (i.e., keeping it as simple as possible). I also need to add a comma every 3 digits in the answer, which i am unsure on how to do. Any help would be appreciated on where to start or how to accomplish this. As an added note, I am a beginning programmer so if explanations could be kept in simple terms, I would appreciate it.





Here is something I have tried but I can't figure out what is wrong:





Private Sub Button1_Click(..)


Dim smplTB As String = "1,000.045 km" 'simulated textbox entry


Dim parts() As String = smplTB.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)


parts(0) = parts(0).Replace(",", "")


Dim valPart As Double


Dim unitPart As String


Dim numDecs As Integer = 0


If Double.TryParse(parts(0), valPart) Then


'valid number


unitPart = parts(1)


Debug.WriteLine(unitPart %26amp; " " %26amp; valPart.ToString)


If parts(0).IndexOf("."c) %26lt;%26gt; -1 Then


numDecs = parts(0).Length - parts(0).IndexOf("."c) - 1


End If


Debug.WriteLine(numDecs.ToString)


End If





Dim meters As Double = 0


'from unit converted to meters


Select Case unitPart


Case ""


Case "km"


meters = valPart * 1000


Case "m"


meters = valPart


Case "cm"


meters = valPart / 100


Case "mm"


meters = valPart / 1000


Case "in"


meters = valPart * 0.00254


Case "ft"


meters = valPart * 0.3048


Case "mi"


meters = valPart * 1609.3


End Select





Dim toUnit As String = "in"


Dim toVal As Double = 0


'to unit based on meters


Select Case toUnit


Case "in"


toVal = meters * 39.37


Case "ft"


toVal = meters * 3.28 ..... etc, etc. through all the units


End Select|||By your program example it looks like you are on the right track. Remember normally someone will not enter in a number with a "," in it soo your text input example of "1,000.045 km" probably would have really been entered 1000.045 km in the text box so I don't think you need to worry about parsing out the "," in the input number... (I may have misunderstood.)





OK... Now to get the number and variable seperated from each other look at the Text Box entry as a string and look for the "Space" Use the instring command like this.





WhereSpaceIs = instr$(Text1.Text," ") This will give you where you need to split the string.





So now on the left side of the space should be a number. On the right side is the begining units.





So since you have a string in your hand you will need to convert the left side to a real number like this. valPart=Val(left$(Text1.Text,WhereSpaceI鈥?This will make the valPart var a real number again.





Then you can get the units by using the Right$ command such as.





unitPart = Right$(Text1.Text,Len(Text1.Text)-WhereS鈥?This should give you the right most characters for your units. It's already a string so you don't have to do anything with it.





Then your Text2.Text box holds what you want to take it to so all you have to do is run either your Case's that you have outlined or I would actually do If Then statements.





When you go to put your answer back into the Text2.Text box you will convert it back to a string using the :


StringAnswer = Str$(Answer) command then just test the length of this to add your comma's back in....





Let me know if you need more help....

No comments:

Post a Comment