数组中的转换错误

时间:2014-10-31 21:42:40

标签: vb.net string visual-studio double

我收到一个完全有道理的错误,但对我来说没有意义,为什么我得到它..

我的错误是从字符串转换为Double类型无效。我调试了这个,每个值都在“”,我看不到任何可能尝试转换为Double的东西,除了我有两个成本字段,所以我放置了一个 ToString(),它们仍然不喜欢它。我正在构建一个附加到电子邮件正文的表格,这是下面这段代码的最终产品。有人可以向我解释为什么我收到上述错误?

Dim body as String  
Dim param As Object() = New Object() { _
                            drpDownLstEnt.SelectedValue(), _
                            txtBoxEligibility.Text, _
                            txtBoxCondition.Text, _
                            approverType, _
                            txtBoxNameOfApprover.Text, _
                            txtBoxOriginatorResults.Text, _
                            txtBoxDate.Text, _
                            drpDownLstNameNumber.SelectedValue(), _
                            lblInternalNameList.Text, _
                            lblExternalNameList.Text, _
                            txtBoxTotalAttendees.Text, _
                            txtBoxCustBeingEnt.Text, _
                            txtBoxEstCost.Text, _
                            txtBoxCostPerPerson.Text, _
                            txtBoxLocation.Text, _
                            txtBoxNameOfEst.Text, _
                            txtBoxComments.Text}

body= String.Format("<table><tr><td>Field 0</td><td>{0}</td></tr>" _
                             + "<tr><td>Field 1</td><td>{1}</td></tr>" _
                             + "<tr><td>Field 2</td><td>{2}</td></tr>" _
                             + "<tr><td>Field 3</td><td>{3}</td></tr>" _
                             + "<tr><td>Field 4</td><td>{4}</td></tr>" _
                             + "<tr><td>Field 5</td><td>{5}</td></tr>" _
                             + "<tr><td>Field 6</td><td>{6}</td></tr>" _
                             + "<tr><td>Field 7</td><td>{7}</td></tr>" _
                             + "<tr><td>Field 8</td><td>{8}</td></tr>" _
                             + "<tr><td>Field 9</td><td>{9}</td></tr>" _
                             + "<tr><td>Field 10</td><td>{10}</td></tr>" _
                             + "<tr><td>Field 11</td><td>{11}</td></tr>" _
                             + "<tr><td>Field 12</td><td>{12}</td></tr>" _
                             + "<tr><td>Field 13</td><td>{13}</td></tr>" _
                             + "<tr><td>Field 14</td><td>{14}</td></tr>" _
                             + "<tr><td>Field 15</td><td>{15}</td></tr>" _
                             + "<tr><td>Field 16</td><td>{16}</td></tr></table>", _
                             param)

message.Body = (currentUser + vbCrLf + vbCrLf + _
                    "Here is your Form ID for the form that was submitted on " _
                    + emailTimeStamp _
                    + ". You will also see a copy of your completed form below." _
                    + vbCrLf + vbCrLf + "Form ID: " + formID + vbCrLf + vbCrLf _
                    + "Completed Form:" + vbCrLf + vbCrLf + emailBody)

3 个答案:

答案 0 :(得分:1)

您发布的代码中存在的一个问题是,您需要在之后使用最终双引号。

所以不是:

+ "<tr><td>Field 16</td><td>{16}</td></tr></table>, param)

可是:

+ "<tr><td>Field 16</td><td>{16}</td></tr></table>", param)

但是在这种情况下代码不会编译,除非你有其他一些奇怪的东西。鉴于它是一个Object(),你在param的定义中得到错误并不是真的有意义。除非您使用某种自定义控件或DependencyProperty将文本或String中的选项转换为其getter中的Double - 在这种情况下,如果控件没有,则会出现上述错误选择 - 您提供的代码实际上似乎不可能导致您提供的错误。

答案 1 :(得分:0)

如果ValueMember引用未执行有效转换的属性,则在访问组合框的SelectedValue属性时可能会出现类似错误。例如:

Public Class Form1

   Protected Overrides Sub OnLoad(e As EventArgs)
      MyBase.OnLoad(e)
      Dim i As New SampleClass() With {.MyStringValue = "Not a number"}
      ComboBox1.ValueMember = "MyValue"
      ComboBox1.DisplayMember = "MyStringValue"
      ComboBox1.DataSource = {i}
   End Sub

   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      System.Diagnostics.Debug.WriteLine(ComboBox1.SelectedValue)
   End Sub

   Public Class SampleClass
      Private _myValue As Object

      Public Property MyValue As Double
         Get
            Return DirectCast(_myValue, Double)
         End Get
         Set(value As Double)
            _myValue = CStr(value)
         End Set
      End Property

      Public Property MyStringValue As String
         Set(value As String)
            _myValue = value
         End Set
         Get
            Return _myValue
         End Get
      End Property
   End Class
End Class

我建议您仔细查看访问SelectedValue属性时发生的情况。

答案 2 :(得分:0)

  

不是答案,而是找到确切错误的一种方法..

替换这段代码..

body = String.Format("<table><tr><td>Field 0</td><td>{0}</td></tr>" _
                         + "<tr><td>Field 1</td><td>{1}</td></tr>" _
                         + "<tr><td>Field 2</td><td>{2}</td></tr>" _
                         + "<tr><td>Field 3</td><td>{3}</td></tr>" _
                         + "<tr><td>Field 4</td><td>{4}</td></tr>" _
                         + "<tr><td>Field 5</td><td>{5}</td></tr>" _
                         + "<tr><td>Field 6</td><td>{6}</td></tr>" _
                         + "<tr><td>Field 7</td><td>{7}</td></tr>" _
                         + "<tr><td>Field 8</td><td>{8}</td></tr>" _
                         + "<tr><td>Field 9</td><td>{9}</td></tr>" _
                         + "<tr><td>Field 10</td><td>{10}</td></tr>" _
                         + "<tr><td>Field 11</td><td>{11}</td></tr>" _
                         + "<tr><td>Field 12</td><td>{12}</td></tr>" _
                         + "<tr><td>Field 13</td><td>{13}</td></tr>" _
                         + "<tr><td>Field 14</td><td>{14}</td></tr>" _
                         + "<tr><td>Field 15</td><td>{15}</td></tr>" _
                         + "<tr><td>Field 16</td><td>{16}</td></tr></table>", _
                         param)

......通过这个:

body = "<table>"
For i As Integer = 0 To param.Length - 1
    System.Diagnostics.Debug.WriteLine( _
            "Conversion [" + i.ToString() + "] : " _
            + param(i).GetType().ToString() + " -> String")
    body = body _
           + "<tr><td>Field " _
           + i.ToString() + "</td><td>" _
           + param(i) + "</td></tr>"

           ' Option Strict forbid implicit casting param(i) -> String
           ' I assume you're on Option Strict Off
Loop
body = body + "</table>"

这将告诉您参数的哪个元素确切地给出了错误 approverType的类型是什么?
已回答:approverType属于String类型 - 谢谢。