Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 ?

like image 998
Samuel Rossille Avatar asked Jan 16 '23 14:01

Samuel Rossille


2 Answers

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

like image 137
vcsjones Avatar answered Apr 20 '23 01:04

vcsjones


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

like image 23
Tilak Avatar answered Apr 20 '23 00:04

Tilak