Question

I have two questions:

  1. Date and DateTime : Are they different or same in VB ?

  2. DateTime can be assigned Nothing in VB, where as it cannot be assigned null in C#. Being a structure it cannot be null. So why is it being allowed in VB ?

---VB.NET-----

Module Module1

    Sub Main()
        Dim d As Date = Nothing
        Dim dt As DateTime = Nothing

        d = CType(MyDate, DateTime)


    End Sub

    Public ReadOnly Property MyDate As DateTime
        Get
            Return Nothing
        End Get
    End Property

End Module

---C#.NET-----

class Program
    {
        static void Main(string[] args)
        {
            DateTime dt = null;//compile time error            
        }
    }
Was it helpful?

Solution

Nothing in VB.NET is not the same as null in C#. It has also the function of default in C# and that is what happens when you use it on a structure like System.DateTime.

So both, Date and DateTime refer to the same struct System.DateTime and

Dim dt As Date = Nothing 

actually is the same as

Dim dt = Date.MinValue

or (in C#)

DateTime dt = default(DateTime);

OTHER TIPS

In c# You can use default keyword

DateTime dt = default(DateTime);

Date and DateTime are same in VB.NET. Date is just alias of DateTime

In vb.net Date is simply an alias for DateTime.

Some of aliases that exist in VB are there for legacy purposes to help with conversions from vb6 applications.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top