Reken vraagje

Status
Niet open voor verdere reacties.

old Hippy

Gebruiker
Lid geworden
24 mei 2008
Berichten
911
Hallo allemaal
heb een reken probleempje.
moet voor mijn CNC machine alles omrekenen van mm naar inch.
dus iets gemaakt maar werkt niet goed.
0.09 inch moet 0.2286 mm zijn
maar in mijn textbox komt 22.86 mm te staan.

Code:
 Try
            If TextBoxX1.Text IsNot Nothing And TextBoxX2.Text = "" Then
                Dim mm As Decimal = TextBoxX1.Text
                Dim inch As Decimal = 0.03937
                TextBoxX2.Text = mm * inch
            End If
            If TextBoxX2.Text IsNot Nothing And TextBoxX1.Text = "" Then
                Dim mm As Decimal = 2.54
                Dim inch As Integer = TextBoxX2.Text
                TextBoxX1.Text = mm * inch
            End If
        Catch ex As Exception
            MsgBox("Geef een getal in", MsgBoxStyle.Information)
        End Try

Wat zie ik over het hoofd?
 
Het moet 0,2286 CM zijn dus het klopt toch?

(Dim mm As Decimal = 2.54) moet dus 25.4 zijn

Je kunt ook een kleine extensie class maken als je veel moet omzetten:
[CPP]Public NotInheritable Class Extensions
Private Sub New()
End Sub
<System.Runtime.CompilerServices.Extension> _
Public Shared Function ConvertMilimeterToInch(milimeter As Decimal) As Decimal
Return (milimeter / 25.4D)
End Function
<System.Runtime.CompilerServices.Extension> _
Public Shared Function ConvertInchToMilimeter(inch As Decimal) As Decimal
Return (inch * 25.4D)
End Function
End Class[/CPP]

(omgezet van C# naar VB, hoop dat de syntax correct is)

of een eigen convert class voor afmetingen zoals je die al vaker gebruikt:

[CPP]Public NotInheritable Class DimensionConvert
Private Sub New()
End Sub
Public Shared Function ToInch(milimeter As Decimal) As Decimal
Return milimeter / 25.4D
End Function
Public Shared Function ToMilimeter(inch As Decimal) As Decimal
Return inch * 25.4D
End Function
End Class[/CPP]

ziet er ook goed uit in code voor andere

[CPP]Dim mm = DimensionConvert.ToMilimeter(0.09D)[/CPP]
 
Laatst bewerkt:
Ha Bloodshed
Dank voor je snelle oplossing.
Heb er dankbaar gebruik van gemaakt.


De Class

Code:
Public NotInheritable Class DimensionConvert
    Private Sub New()
    End Sub
    Public Shared Function ToInch(milimeter As Decimal) As Decimal
                Return milimeter / 25.4D
    End Function
    Public Shared Function ToMilimeter(inch As Decimal) As Decimal
        Return inch * 25.4D
    End Function
End Class


Uiteindelijke code
Code:
 Try
            For Each tb As Control In MyBase.Controls
                If TypeOf (tb) Is TextBox Then tb.Text = tb.Text.Replace(".", ",")
            Next

            If TB_milimeter.Text IsNot Nothing And TB_inches.Text = "" Then
                Dim inch = DimensionConvert.ToInch(TB_milimeter.Text)
                TB_inches.Text = Math.Round(inch, 5)
            End If
            If TB_inches.Text IsNot Nothing And TB_milimeter.Text = "" Then
                Dim mm = DimensionConvert.ToMilimeter(TB_inches.Text)
                TB_milimeter.Text = Math.Round(mm, 5)

            End If
        Catch ex As Exception
            MsgBox("Geef een getal in", MsgBoxStyle.Information)
        End Try

Werk zeer naar tevredenheid

Met veel dank Old Hippy
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan