4

If I execute the following statement:

dim defaultDate as Date = Nothing

defaultDate contains the default value of the Date structure

I suspect there is a clean way to retreive this value, like some kind of buitin constant, but i was not able to find it.

Q: What is the clean way to retrieve the default value of a given structure in VB.net ?

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Samuel Rossille
  • 18,940
  • 18
  • 62
  • 90

3 Answers3

6

As you have already found, Nothing is the correct way to do this in VB.NET for value types.

C# has a more "explicit" way of doing that with default(T).

vcsjones
  • 138,677
  • 31
  • 291
  • 286
  • So there is no exact equivalent of default(T) in VB ? – Samuel Rossille May 23 '12 at 14:48
  • 4
    @SamuelRossille `Nothing` actually is `default(T)` in VB.NET. http://stackoverflow.com/a/303525/492405 Most people assume that `Nothing` in VB.NET means "null". It actually means "The default value", and for all reference types that happens to be null. For value types, it's the default value. – vcsjones May 23 '12 at 14:50
2

Value types does not need explicit initialization.

By default all the fields are initialized to default values.

dim defaultDate as Date ' Nothing not required
Console.WriteLine(defaultDate) ' 1/1/0001 12:00:00 AM 

Explanation on default constructor here and here

Community
  • 1
  • 1
Tilak
  • 30,108
  • 19
  • 83
  • 131
1

The default Date is Date.MinValue.

0:00:00 (midnight) on January 1, 0001.

Date Data Type (Visual Basic)

Dim d As Date = Nothing
If d = Date.MinValue Then
    ' yes, we are really here '
End If

I usually prefer Date.MinValue instead of Nothing, but i assume that's a matter of taste.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • I'd say OP is looking for a way to get the default date for any given structure, not just date. – Joel Etherton May 23 '12 at 14:46
  • @JoelEtherton: OP wanted to know this constant: _"is there a clean way to retreive this value, like some kind of buitin constant, but i was not able to find it. "_ **Edit** Oh, you're right, i've missed the very last part. – Tim Schmelter May 23 '12 at 14:48
  • @TimSchmelter should have been more explicit right from the start ;=)... but why do you call me OP ? What does it means ? – Samuel Rossille May 23 '12 at 15:10
  • @SamuelRossille: It's just an abbreviation for _original poster_ http://en.wiktionary.org/wiki/OP (on SO he who asked the question) – Tim Schmelter May 23 '12 at 15:17