VB.net:“输入字符串的格式不正确。”使用Convert.ToDouble

时间:2018-11-20 20:39:58

标签: vb.net

我一直收到标题中提到的错误。我已经尝试过用其他线程中提到的“&”替换所有“ +”,但这似乎不起作用。我想创建一个程序,使我可以使用VB.net查看其他行星上的重量,但无法克服此“输入字符串格式不正确”的错误。有什么帮助吗?错误发生在第一行。

                    Convert.ToDouble(ComboBox1.SelectedItem())
    If ComboBox1.SelectedItem = 0 Then
        Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 0.378)
    ElseIf ComboBox1.SelectedItem = 1 Then
        Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 0.905)
    ElseIf ComboBox1.SelectedItem = 2 Then
        Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 1.0)
    ElseIf ComboBox1.SelectedItem = 3 Then
        Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 0.379)
    ElseIf ComboBox1.SelectedItem = 4 Then
        Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 2.529)
    ElseIf ComboBox1.SelectedItem = 5 Then
        Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 1.066)
    ElseIf ComboBox1.SelectedItem = 6 Then
        Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 0.903)
    ElseIf ComboBox1.SelectedItem = 7 Then
        Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 1.096)
    ElseIf ComboBox1.SelectedItem = 8 Then
        Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 0.069)
    End If

2 个答案:

答案 0 :(得分:0)

谢谢大家的帮助!我通过在用户输入之前添加CDbl(),使用Option Strict On并使用SelectedIndex()而不是SelectedItem()来解决了这个问题。

代码如下:

If ComboBox1.SelectedIndex = 0 Then
        Label4.Text = "Your weight on Mercury" & " is " & (CDbl(TextBox1.Text) * 0.378)
    ElseIf ComboBox1.SelectedIndex = 1 Then
        Label4.Text = "Your weight on Venus" & " is " & (CDbl(TextBox1.Text) * 0.905)
    ElseIf ComboBox1.SelectedIndex = 2 Then
        Label4.Text = "Your weight on Earth" & " is " & (CDbl(TextBox1.Text) * 1.0)
    ElseIf ComboBox1.SelectedIndex = 3 Then
        Label4.Text = "Your weight on Mars" & " is " & (CDbl(TextBox1.Text) * 0.379)
    ElseIf ComboBox1.SelectedIndex = 4 Then
        Label4.Text = "Your weight on Jupiter" & " is " & (CDbl(TextBox1.Text) * 2.529)
    ElseIf ComboBox1.SelectedIndex = 5 Then
        Label4.Text = "Your weight on Saturn" & " is " & (CDbl(TextBox1.Text) * 1.066)
    ElseIf ComboBox1.SelectedIndex = 6 Then
        Label4.Text = "Your weight on Uranus" & " is " & (CDbl(TextBox1.Text) * 0.903)
    ElseIf ComboBox1.SelectedIndex = 7 Then
        Label4.Text = "Your weight on Neptune" & " is " & (CDbl(TextBox1.Text) * 1.096)
    ElseIf ComboBox1.SelectedIndex = 8 Then
        Label4.Text = "Your weight on Pluto" & " is " & (CDbl(TextBox1.Text) * 0.069)
    End If

答案 1 :(得分:0)

使用数据绑定代替If else
Double.TryParse而不是CDbl来实现从字符串到整数的转换
组合字符串(或字符串插值)时使用&+代替Public Class Planet Public ReadOnly Property Name As String Public ReadOnly Property WeightFactor As Double Public Sub New(name As String, weightFactor As Double) Me.Name = name Me.WeightFactor = weightFactor End Sub End Class ' Create planets and bind it to the ComboBox ' Form constructor Public Sub New() InitializeComponent() Dim planets As New List(Of Planet) From { New Planet("Mercury", 0.378), New Planet("Venus", 0.905), New Planet("Earth", 1.0), New Planet("Mars", 0.379), } ' Bind planets to combobox ComboBox1.DisplayMember = "Name" ' Name of the property used for displaying text ComboBox1.ValueMember = "WeightFactor" ' Name of the property used as selected value ComboBox1.DataSource = planets End Sub

  

选项严格启用-始终(您已经这样做了)

Dim personWeight AS Double
If Double.TryParse(textbox1.Text, personWeight) Then
    ' SelectedItem is instance of selected planet
    Dim planet = DirectCast(ComboBox1.SelectedItem, Planet)

    Dim weightOnPlanet = personWeight * planet.WeightFactor
    Label4.Text = $"Your weight on {planet.Name} is {weightOnPlanet}"
End If

然后您的计算代码将变得更简单

 scene.rootNode.addChildNode(node)
    node.geometry?.firstMaterial?.diffuse.contents = self.createLayer() // CALayer

    let renderer = SCNRenderer(
        device: MTLCreateSystemDefaultDevice(),
        options: nil)

----renderer.prepare([node]) { (success) in  // this line is useful
        if (success){
            let renderTime = CACurrentMediaTime() + 1
            let size = CGSize(width: 600, height: 600)
            let  image = renderer.snapshot(
                atTime: renderTime,
                with: size,
                antialiasingMode: .multisampling4X)
            complete(image)
        }
    }