tags:

views:

26

answers:

2

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.

+2  A: 

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 { ... } _
}                   ' ^^^^^^^^^^^^^^^^
itowlson
Thanks @itowlson. It works!
Ramiz Uddin
+1  A: 

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.

Prutswonder
@Prutswonder you are right I should try to make it simple by avoiding anonymous statement :) thanks for help.
Ramiz Uddin