-2

I mean something like this (in bold):

Dim anonType = New With {.Property1 = 10, .Property2 As Decimal? = Nothing}

kexx
  • 275
  • 5
  • 13
  • What do you think, any research? – Trevor Feb 11 '18 at 19:42
  • I did research, I know it's not possible the way I presented, but it does not mean it is not possible somehow else. It's not a far fetched idea. It seems some people thoughts about stackoverflow is right, it's starting to be toxic, i can't understand why is this a bad question – kexx Feb 11 '18 at 20:08
  • David Wilson: I know that, but not working around is always better – kexx Feb 11 '18 at 20:19
  • Codexer: Thank you for you answer. They are of course object, but not System.Object. They are strongly typed object created compile time. If you see them runtime, you'll see some kind of a mystical generic object thingies like this: VB$AnonymousType_42(Of Boolean, Date, Date, Integer?, Integer?). This means if you want to set a string as the first property (boolean), it will cause an exception. So the idea here is forcing the type when you define props, basically telling the compiler do this, instead of create a system.object typed property, when the initial value specified is null. – kexx Feb 11 '18 at 20:50
  • Codexer: Considering your answer this is not part of the language. The other guy's also right, it can be worked around easily by declaring a stringly typed variable for the value before, then using that for auto-initialization for the anonymous type. But this can be a feature of the language in the future – kexx Feb 11 '18 at 20:52
  • @Codexer Would you mind having a look at my answer to check it please? It seems to work, but I'll be quite interested in any comments you have to make. Cheers. – David Wilson Feb 11 '18 at 22:11
  • Why do you think you need this? – jmcilhinney Feb 11 '18 at 22:30
  • @jmcilhinney I process this anon object with reflection, this is a value holder for different kind filters and order expressions passed to a simple orm tool I wrote. The filter names could be various (every property of an entity is automatically a simple filter), so creating an ordinary class makes no sense this case, but an anonymous type is good for it. – kexx Feb 11 '18 at 22:34
  • @kexx `Thank you for you answer. They are of course object, but not System.Object.`, you are wrong, it inherits directly from `System.Object`... Please reference docs here https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/objects-and-classes/anonymous-types and here https://learn.microsoft.com/en-us/dotnet/api/system.object?view=netframework-4.7.1 . If an anonymous type declaration contains at least one key property, the type definition overrides three members inherited from System.Object: Equals, GetHashCode, and ToString. – Trevor Feb 11 '18 at 23:01
  • @David Wilson, I left a few comments mainly for OP... – Trevor Feb 11 '18 at 23:53

1 Answers1

0

You could always work around it like this..

Private Sub test()
    Dim tempDecimal? As Decimal = Nothing
    Dim anonType = New With {.Property1 = 10, .Property2 = tempDecimal}
    ' property value is nothing here, but if tempDecimal isnt declared as nullable,
    'the value will be 0
    anonType.property2 = Nothing 
    anonType.property2 = 5
    'tp will be Decimal here
    Dim tp As Type = anonType.property2.GetType
End Sub
David Wilson
  • 4,369
  • 3
  • 18
  • 31
  • Thank you, this is a possible workaround, I actually ended up doing this. – kexx Feb 11 '18 at 22:30
  • No worries - It's not an ideal solution, but if it works for you - great. – David Wilson Feb 11 '18 at 22:32
  • In the example `Property2` type could be set as `0D` if you want to force type IMO, instead of creating another variable to set it's type – Trevor Feb 11 '18 at 23:05
  • @Codexer which works perfectly, however if you want to set the value of a nullable type explicitly to null, it does not work any more, and you still need the extra declaration – kexx Feb 11 '18 at 23:10
  • @kexx `.Property2 = New Decimal?` there, that is `Nothing` (null is c# not vb.net).. easy change, still ***no variable***... On the other hand ***if you don't want it nothing*** then `.Property2 = New Decimal`... Creating variables for anonymous types defeats the purpose of even having an anonymous object and is code smell. – Trevor Feb 11 '18 at 23:36
  • @Codexer good idea about using `New Decimal?` That, for some reason didn't occur to me. I'm getting old.... – David Wilson Feb 12 '18 at 08:50
  • @DavidWilson we are all getting old :), thanks for the kind comments. – Trevor Feb 12 '18 at 15:03