Ramiz Uddin
Ramiz Uddin

Reputation: 4259

What is wrong with this Anonymous Object Initializate Syntax?

What is wrong with this Anonymous Object Initialize syntax?

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

It is a syntax error or object initialization type casting issue.

Thanks.

Upvotes: 0

Views: 125

Answers (2)

Prutswonder
Prutswonder

Reputation: 10074

Try this non-anonymous syntax first:

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

If it works, try to refactor it back into the syntax you want, step by step. With each step, check if the code still works. If not, then you have found your problem and you can try to find a solution for it.

Upvotes: 1

itowlson
itowlson

Reputation: 74822

You're trying to set the NumberFormat of the CultureInfo to an anonymous type instance. CultureInfo.NumberFormat is of type NumberFormatInfo. So you need to write:

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

Upvotes: 2

Related Questions