1

I found that there can be 5 different ways of declaring a date in VB.Net. They are:

  1. Dim date_one As Date = #2021-3-31#: This is one of the allowed formats of date literals in VB
  2. Dim date_two As New Date(2021,3,31) : Initializes an instance of the DateTime structure to the year, month and day
  3. Dim date_three As DateTime : Declaring a DateTime Value to its default value
  4. Dim date_four As New DateTime() : Calling parameterless constructor - sets to default value
  5. Dim date_five As New DateTime(2021, 3, 31) : Supplying parameters to the constructor

My confusion /questions w.r.t to the above are the following:

A. DateTime is a structure in the system namespace. We do not use NEW keyword to declare a structure type variable. So why then in points 2,4,5 are we allowed to use the NEW keyword?

B. In point 2, we declare the type as DATE whereas in 3,4,5 we declare the type as DateTime. Infact if we compare2 & 5 then we see that they are doing exactly the same thing with the difference being tht one is declared as DATE and the other as DateTime. So my question is What is the difference between DATE and DateTime? If there is no difference why do we have two different words for the same thing?

C. Point 1 is a VB.Net language specific syntax for a date literal. This means I cant use this syntax in another .NET language. But the dates defined in points 2,3,4 and 5 make use of the DateTime structure which is in the system namespace and therefore this way of declaring or constructing dates is allowed in other .Net languages. ----> Is this understanding correct?

D. I also came across a class named DateAndTime. This belongs to the Microsoft.VisualBasic namespace and I believe that is reason this class is exclusive to VB.Net. Since it doesnt belong to the system namespace We cant import it in another .Net language --- Is this understanding correct?

E. Finally, how to decide whether to to use DATE or DateTime? Also when should we use DateAndTime Class? What advantage/limitation does each of them provide?

Would be grateful to anyone willing to help,

Regards,

Sougata

Sougata
  • 319
  • 1
  • 10

2 Answers2

4

A. Class fields (variables at class level) and local variables (i.e., variables declared in methods or property getters and setters) get initialized to their default value if there is no initializer and there is no initialization code in the class constructor (for fields). So, a New is not required if you are happy with the default value.

Note that a Date (or DateTime) is a Structure and therefore a value type. Default values of value types are "real" values in contrast to reference types (Classes) having a default value of Nothing, meaning that no object is assigned to this variable. This is also called a null reference. (In fact you can assign Nothing to a value type; however, this will translate to a value like 0, False, #1/1/0001 12:00:00 AM#, etc.)

You must differentiate between declaring fields or variables and initializing them. To declare means to give them a name and a type. Initialize means assigning them value for the first time.

You can do this with two statements

Dim d As Date              'Declare
d = New Date(2021, 3, 30)  'Initialize

or use an initializer

Dim d As Date = New Date(2021, 3, 30)  'Declare and initialize

These two variants are equivalent. The first one allows you to assign a value later, the second one is more compact.

B. The .NET Framework defines primitive types and gives them a name in the System Namespace. E.g., System.String, System.Int32 or System.DateTime. Visual Basic gives many of them an alias. For the previously listed types those are String, Integer and Date. C# gives them the different aliases, string, int and has no alias for the DateTime.

The framework name and the corresponding Visual Basic (or C#) aliases are totally equivalent. System.Int32 ≡ VB Integer ≡ C# int.

C. Yes, e.g., in C# you must write new System.DateTime(...) (of course you can always import the namespace and omit the System. prefix). The Visual Basic date literal is just a short form for New Date(...) or New System.DateTime(...).

D. The DateAndTime Class is in fact a Visual Basic module. It is called class, because C# has no notion of modules as in VB and instead implements them as static classes.

The documentation says:

The DateAndTime module contains the procedures and properties used in date and time operations.

Languages in the .NET Framework have a Common Type System. Therefore, you can use a type defined in one language in all the other .NET languages. Also, the generated code is compatible, so you can share methods as well.

E. In Visual Basic I would use the aliases. It makes the code more concise.

You are using the DateAndTime automatically when you write for instance

Dim d  As Date = Now

Now is declared in this module. DateAndTime does not compete with Date or DateTime, as you cannot use it to declare variables, i.e., DateAndTime is not a type.


See also:

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • 2
    Your answer in (1) isn't quite technically correct as to the meaning of `Nothing`. `Nothing` in VB is equivalent to `default` in C#, so in fact the default values for `Structure`s are trivially `Nothing`. The problem is that this overloading of `Nothing` means there isn't a strictly unambiguous way to refer to `null` in VB. The answer is probably adequate for a beginner but they might be surprised by what happens with `Nothing` and value types in the future. – Craig Mar 31 '21 at 13:21
  • 1
    Good point. See: [Nothing keyword (Visual Basic)](https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/nothing). – Olivier Jacot-Descombes Mar 31 '21 at 14:00
  • 1
    @BartHofland I have my statement straight from the horse's mouth, as it were. It's discussed in Anthony Green's blog post (he's formerly the head guy for VB). `Nothing` is exactly equivalent to `default` in C#. It happens that the default value for a reference type is null, but there is no exact VB equivalent to the C# `null`. It only becomes an issue in certain cases, though... the majority of the time, if you treat `Nothing` as equivalent to `null`, you won't go wrong. Reference: https://anthonydgreen.net/2019/02/12/exhausting-list-of-differences-between-vb-net-c/ – Craig Mar 31 '21 at 20:45
  • @Craig . . . You are right indeed. In C#, `default` will be `null` for reference types. It is also clearly stated in the first line of the remarks section in the documentation of the `Nothing` keyword on Microsoft Docs. Thank you very much for the additional clarification and the reference. I will go read it. – Bart Hofland Apr 01 '21 at 08:20
  • 2
    @Craig . . . That article you refer to ([An Exhausting List of Differences Between VB.NET & C#](https://anthonydgreen.net/2019/02/12/exhausting-list-of-differences-between-vb-net-c/)) is *great*. Thanks a lot! – Bart Hofland Apr 01 '21 at 08:38
2

A. DateTime is a structure in the system namespace. We do not use NEW keyword to declare a structure type variable. So why then in points 2,4,5 are we allowed to use the NEW keyword?

Structures may have constructors

B. In point 2, we declare the type as DATE whereas in 3,4,5 we declare the type as DateTime. Infact if we compare2 & 5 then we see that they are doing exactly the same thing with the difference being tht one is declared as DATE and the other as DateTime. So my question is What is the difference between DATE and DateTime? If there is no difference why do we have two different words for the same thing?

see https://stackoverflow.com/questions/5625795/date-instead-of-datetime

C. Point 1 is a VB.Net language specific syntax for a date literal. This means I cant use this syntax in another .NET language. But the dates defined in points 2,3,4 and 5 make use of the DateTime structure which is in the system namespace and therefore this way of declaring or constructing dates is allowed in other .Net languages. ----> Is this understanding correct?

The data type IS the same across .Net regardless of how it comes to be.

D. I also came across a class named DateAndTime. This belongs to the Microsoft.VisualBasic namespace and I believe that is reason this class is exclusive to VB.Net. Since it doesnt belong to the system namespace We cant import it in another .Net language --- Is this understanding correct?

If the .Net language allows you to import the Microsoft.VisualBasic namespace you can probably use it.  Would NOT suggest it.

E. Finally, how to decide whether to to use DATE or DateTime? Also when should we use DateAndTime Class? What advantage/limitation does each of them provide?

Hmmmm.... DateAndTime does have some different date calculations that can be useful.  

My preference is to use DateTime.

If all I'm interested in is the Date or Time then I reference that.

        Dim d1 As Date = Date.Now
        Dim d2 As DateTime = DateTime.Now

        If d1 = d2 Then
            Stop
        End If

        If d1.Date = d2.Date Then
            Stop
        End If

        If d1.TimeOfDay = d2.TimeOfDay Then
            Stop
        End If

A lot of these questions can be answered by searching the documentation.

dbasnett
  • 11,334
  • 2
  • 25
  • 33