Samuel Rossille
Samuel Rossille

Reputation: 19858

How can i retrieve the default value of a given structure in VB.net?

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 ?

Upvotes: 4

Views: 909

Answers (3)

Tilak
Tilak

Reputation: 30718

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

Upvotes: 2

Tim Schmelter
Tim Schmelter

Reputation: 460208

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.

Upvotes: 1

vcsjones
vcsjones

Reputation: 141668

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).

Upvotes: 6

Related Questions