这个匿名对象初始化语法有什么问题?

时间:2010-04-08 08:28:39

标签: vb.net

这个匿名对象初始化语法有什么问题?

If (Not row Is Nothing) Then
    Dim info As New CultureInfo(Conversions.ToString(row.Item("cultureId"))) With { _
            .NumberFormat = New With {.CurrencySymbol = Conversions.ToString(row.Item("symbol")), _
                  .CurrencyGroupSeparator = Conversions.ToString(row.Item("thousSep")), _
                  .CurrencyDecimalSeparator = Conversions.ToString(row.Item("thousSep")), _
                  .CurrencyDecimalDigits = Conversions.ToInteger(row.Item("decimals")), _
                  .NumberGroupSeparator = Conversions.ToString(row.Item("thousSep")), _
                  .NumberDecimalSeparator = Conversions.ToString(row.Item("thousSep")), _
                  .NumberDecimalDigits = Conversions.ToInteger(row.Item("decimals"))}}}
    hashtable.Add(key, info)
End If

这是语法错误或对象初始化类型转换问题。

感谢。

2 个答案:

答案 0 :(得分:2)

您正在尝试将CultureInfo的NumberFormat设置为匿名类型实例。 CultureInfo.NumberFormat的类型为NumberFormatInfo。所以你需要写:

Dim info As New CultureInfo(...) With { _
  .NumberFormat = New NumberFormatInfo With { ... } _
}                   ' ^^^^^^^^^^^^^^^^

答案 1 :(得分:1)

首先尝试这种非匿名语法:

If (Not row Is Nothing) Then
     Dim numberFormat as New NumberFormat()
     numberFormat.CurrencySymbol = Conversions.ToString(row.Item("symbol"))
     numberFormat.CurrencyGroupSeparator = Conversions.ToString(row.Item("thousSep"))
     numberFormat.CurrencyDecimalSeparator = Conversions.ToString(row.Item("thousSep"))
     numberFormat.CurrencyDecimalDigits = Conversions.ToInteger(row.Item("decimals"))
     numberFormat.NumberGroupSeparator = Conversions.ToString(row.Item("thousSep"))
     numberFormat.NumberDecimalSeparator = Conversions.ToString(row.Item("thousSep"))
     numberFormat.NumberDecimalDigits = Conversions.ToInteger(row.Item("decimals"))

     Dim info As New CultureInfo(Conversions.ToString(row.Item("cultureId")))
     info.NumberFormat = numberFormat

     hashtable.Add(key, info)
End If

如果有效,请尝试将其重构为您想要的语法,一步一步。每个步骤,检查代码是否仍然有效。如果没有,那么您已找到问题所在,您可以尝试找到解决方案。