1

I will only know the type at runtime.

Public Shared Function DefaultValue(Type As System.Type) As Object
  '???
End Function

Can anyone fill in the function? thank you!

EDIT: After the answer and more discussion, I have learned you can just do:

MyExpression = Nothing

to determine if the expression contains the default value for its type. Having the shared function in my library might help me remember this about the language but otherwise the function is not necessary. Thanks to Dave for pointing this out.

toddmo
  • 20,682
  • 14
  • 97
  • 107
  • possible duplicate of [.NET - Get default value for a reflected PropertyInfo](http://stackoverflow.com/questions/407337/net-get-default-value-for-a-reflected-propertyinfo) - see answer by @MarkJones (not the accepted one). – MarcinJuraszek Jul 02 '14 at 23:12
  • @MarcinJuraszek, please let them fill in the function. This question has never been asked in such a simple and direct way. thank you! – toddmo Jul 02 '14 at 23:14
  • default value of a Type or default value of a type *property*? – Ňɏssa Pøngjǣrdenlarp Jul 02 '14 at 23:14
  • @Plutonix, of any System.Type. The signature of the function is correctly what I'm asking. – toddmo Jul 02 '14 at 23:17
  • @MarcinJuraszek, this can and will be used outside of reflection. It's a different question. – toddmo Jul 02 '14 at 23:18
  • `return Activator.CreateInstance(type)` for value types, anyway; otherwise, probably nothing – Ňɏssa Pøngjǣrdenlarp Jul 02 '14 at 23:21
  • @Plutonix, thank you! Do you want me to mark your comment as the answer? – toddmo Jul 02 '14 at 23:25
  • it apparently has been asked...long, long ago. see: http://stackoverflow.com/questions/325426/programmatic-equivalent-of-defaulttype – Ňɏssa Pøngjǣrdenlarp Jul 02 '14 at 23:33
  • Actually, in VB "Nothing" always means "the default value of the type" for both reference and value types (unlike C# and 'null'), so I don't think you even need this function in VB - just use "Nothing" instead of calling this function. – Dave Doknjas Jul 03 '14 at 00:10
  • Are you under the impression that an object of the type is the default value for a reference type? It's not. What if that type has no parameterless constructor? As has been said, the default value for any type is `Nothing`, which is what an uninitialised variable of that type contains. If what you really want to do is be able to create an instance of any type based on a `Type` then that's what you should have said and, while `Activator.CreateInstance` will work for most types, it won't work for all. – jmcilhinney Jul 03 '14 at 02:02
  • @DaveDoknjas, for an integer property, for example, if I compare 0 (the default value) to nothing, I don't think it will say that's equal. `if my property contains the default value, do something...` – toddmo Jul 03 '14 at 15:46
  • @toddmo: Have you tried it? An integer i set to 0 will yield true for "i = Nothing". Remember, VB is not like C# or Java - "Nothing" is *not* the same as "null" in these other languages. – Dave Doknjas Jul 03 '14 at 16:41
  • @DaveDoknjas, well, I'll be a monkey's uncle. I never knew that because i had only ever used `foo Is Nothing`. I never used `foo = Nothing`. – toddmo Jul 03 '14 at 19:34
  • @toddmo: Yes - " = Nothing"/" <> Nothing" for value type and " Is Nothing"/" IsNot Nothing" for reference types. – Dave Doknjas Jul 03 '14 at 20:50

1 Answers1

1

I think that there is a very simple approach to solving this problem: if the type is not a value type, return nothing, otherwise return a new instance of the type, which will be initialized to the default value:

Public Shared Function DefaultValue(Type As System.Type) As Object
    If Not Type.IsValueType Then
        Return Nothing
    Else
        Return Activator.CreateInstance(Type)
    End If
End Function
competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • You must be my mental doppleganger. Your function is character by character EXACTLY what I typed yesterday into my library. I just didn't want to answer my own question. Thanks Buddy! – toddmo Jul 03 '14 at 15:34
  • This is overkill - "Nothing" in VB handles both reference and value types. – Dave Doknjas Jul 03 '14 at 16:43
  • @DaveDoknjas: Yes, and when you convert this to C#, another VB shortcut or assumption will bite you. Having experienced the "joys" of a conversion of a substantial amount of VB to C# 2 years ago, we now insist on standard framework functionality as much as possible in all of our code reviews. And "overkill" is extraordinarily subjective. This code is essentially self-documenting and could be used as a basis for other functionality, where as the use of Nothing would have to be thoroughly documented. – competent_tech Jul 04 '14 at 01:29
  • @competent_tech: Since the question was in VB, I assumed a VB answer would be appropriate. You're right though - "Nothing" in VB can be confusing, especially to C# developers. – Dave Doknjas Jul 04 '14 at 01:52