tags:

views:

24

answers:

2

I have a project to do in Visual Basic. My problem is that the outcome is always wrong (ex. instead of 2011 it gives 2000). And i cannot set as Desired unit the Inch(1) or feet(3), it gives the Infinity error. And if i put as Original and Desired unit the inch(1), the outcome is "Not a Number".

Here's the code i made so far. The project is about arrays. Any help appreciated.

Public Class Form1

Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click

    Dim original(9) As Long
    Dim desired(9) As Long
    Dim a As Integer
    Dim o As Integer
    Dim d As Integer
    Dim inch As Long, fathom As Long, furlong As Long, kilometer As Long
    Dim meter As Long, miles As Long, rod As Long, yard As Long, feet As Long

    a = Val(Input3.Text)
    o = Val(Input1.Text)
    d = Val(Input2.Text)

    inch& = 0.0833
    rod& = 16.5
    yard& = 3
    furlong& = 660
    meter& = 3.28155
    kilometer& = 3281.5
    fathom& = 6
    miles& = 5280

    original(1) = inch
    original(2) = fathom
    original(3) = feet
    original(4) = furlong
    original(5) = kilometer
    original(6) = meter
    original(7) = miles
    original(8) = rod
    original(9) = yard

    desired(1) = inch
    desired(2) = fathom
    desired(3) = feet
    desired(4) = furlong
    desired(5) = kilometer
    desired(6) = meter
    desired(7) = miles
    desired(8) = rod
    desired(9) = yard

    If o < 1 Or o > 9 Or d < 1 Or d > 9 Then
        MessageBox.Show("Units must range from 1-9.", "Error", _
        MessageBoxButtons.OK, _
        MessageBoxIcon.Information)
        Return
    End If

    Output.Text = (a * original(o)) / desired(d)

End Sub

End Class

A: 

Geia sou Manoli,

usually arrays are zero based. Also inch, meters etc should be declared as double, not long.

This code will do the convertion corectly

    Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
    Dim original(8) As Double
    Dim desired(8) As Double
    Dim a As Integer
    Dim o As Integer
    Dim d As Integer
    Dim inch As Double, fathom As Long, furlong As Double, kilometer As Double
    Dim meter As Double, miles As Long, rod As Double, yard As Double, feet As Long

    a = CInt(Val(Input3.Text))
    o = CInt(Val(Input1.Text))
    d = CInt(Val(Input2.Text))

    inch = 0.0833333333
    rod = 16.5
    yard = 3
    furlong = 660
    meter = 3.2808399
    kilometer = 3280.8399
    fathom = 6
    miles = 5280

    original(0) = inch
    original(1) = fathom
    original(2) = feet
    original(3) = furlong
    original(4) = kilometer
    original(5) = meter
    original(6) = miles
    original(7) = rod
    original(8) = yard

    desired(0) = inch
    desired(1) = fathom
    desired(2) = feet
    desired(3) = furlong
    desired(4) = kilometer
    desired(5) = meter
    desired(6) = miles
    desired(7) = rod
    desired(8) = yard

    If o < 0 Or o > 8 Or d < 0 Or d > 8 Then
        MessageBox.Show("Units must range from 0-8.", "Error", _
        MessageBoxButtons.OK, _
        MessageBoxIcon.Information)
        Return
    End If

    Output.Text = CStr((a * original(o)) / desired(d))

End Sub
Yiannis Mpourkelis
Also you can only use the original array. The desired array is not needed.For example: Output.Text = CStr((a * original(o)) / original(d))
Yiannis Mpourkelis
A: 

Thank you for the reply!

Now it works perfectly.

I was about to ask about another problem, but i figured it out.

Output.Text = CStr((a * original(o)) / desired(d))

has to be converted to

Output.Text = CStr((a * original(o - 1)) / desired(d - 1))

Euxaristw!

Manolis