VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 300,329 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,831 people online right now. Registration is fast and FREE... Join Now!




Need some help with a US to metric converter

 

Need some help with a US to metric converter

Amerowolf

31 Jan, 2009 - 12:14 PM
Post #1

New D.I.C Head
*

Joined: 31 Jan, 2009
Posts: 3


My Contributions
I need some help coding something. I'm quite new so...yeah.

CODE

    Private Sub DistanceConvertButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DistanceConvertButton.Click
        'converts Distances to metric
        Dim numInch As Decimal
        Dim numFeet As Decimal
        Dim numMile As Decimal
        Dim numCenti As Decimal
        Dim numMeter As Decimal
        Dim numKilo As Decimal

        If txtInch.Text <> " " Then
            numInch = txtInch.Text * 2.54
            txtCenti.Text = numInch
        Else
            numCenti = txtCenti.Text / 2.54
            txtInch.Text = numCenti
        End If

        If txtFeet.Text <> " " Then
            numFeet = txtFeet.Text * 0.3048
            txtMeter.Text = numFeet
        Else
            numMeter = txtMeter.Text / 0.3048
            txtFeet.Text = numMeter
        End If
        If txtMile.Text <> " " Then
            numMile = txtMile.Text * 1.61
            txtKilo.Text = numMile
        Else
            numKilo = txtKilo.Text / 1.61
            txtMile.Text = numKilo
        End If
    End Sub


I'm trying to get this to work so that if I type some measurement in metric it will convert to US measurements, and if I type something in US measurements it will convert to Metric.

Before I up in all the If then statements it converted to US to metric just fine, though I don't know if the actual conversions were correct, but it worked anyway. Now it just give me an error saying IvalidCastException unhandled.

And I'm not sure what that means...

User is offlineProfile CardPM
+Quote Post


PsychoCoder

RE: Need Some Help With A US To Metric Converter

31 Jan, 2009 - 12:39 PM
Post #2

Dyslexics Untie!
Group Icon

Joined: 26 Jul, 2007
Posts: 14,701



Thanked: 501 times
Dream Kudos: 11450
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
Moved to VB.NET smile.gif
User is offlineProfile CardPM
+Quote Post

Amerowolf

RE: Need Some Help With A US To Metric Converter

31 Jan, 2009 - 12:40 PM
Post #3

New D.I.C Head
*

Joined: 31 Jan, 2009
Posts: 3


My Contributions
Oh. Sorry about that, thanks for the move, man.
User is offlineProfile CardPM
+Quote Post

nightscream

RE: Need Some Help With A US To Metric Converter

31 Jan, 2009 - 03:06 PM
Post #4

D.I.C Head
**

Joined: 4 Dec, 2008
Posts: 111



Thanked: 1 times
My Contributions
that means that there is a problem with conversion.
You should debug it next time, an empty textbox is not " " but just "" change it and it should work.
Or you can also use Nothing or String.empty I think there are some more.

This post has been edited by nightscream: 31 Jan, 2009 - 03:14 PM
User is offlineProfile CardPM
+Quote Post

Amerowolf

RE: Need Some Help With A US To Metric Converter

1 Feb, 2009 - 10:18 AM
Post #5

New D.I.C Head
*

Joined: 31 Jan, 2009
Posts: 3


My Contributions
I replaces the " " with Nothing and it worked the first time, but on the second run it gave me the same invalid IvalidCastException unhandled error on

CODE

numInch = txtInch.Text * 2.54


Okay, but once I stop debugging, the program works fine. Thanks, man. Now hopefully things go smoother with the temperature and liquid conversions.
User is offlineProfile CardPM
+Quote Post

Jayman

RE: Need Some Help With A US To Metric Converter

1 Feb, 2009 - 12:28 PM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,544



Thanked: 226 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Set the textbox to be an empty string, do not clear it using Nothing.

CODE

TextBox.Text.Clear()

TextBox.Text = ""

TextBox.Text = String.Empty

User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 03:49PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month