Variables in Visual Basic are more than just names or placeholders for values. They’re intelligent entities that can not only store but also process their values. A variable that holds dates is declared as such with the following statement:
Dim ProductionDate As Date
This is how we always used variables, in most languages. In addition to holding a date, however, the expiration variable can manipulate dates. The
following expression will return a new date that’s ten years ahead of the date stored in the expiration variable:
after10Years = ProductionDate.AddYears(10)
AddYears is a method that knows how to add a number of years to a Date variable. By adding a number of years (or months, or days) to a date, we get back another date. The method will take into consideration the number of days in each month and the leap years, which is a totally nontrivial task if we had to code it ourselves.
Dim ProductionDate As Date = Now Dim after10Years As Date Console.WriteLine("Product manufacturing date " & ProductionDate) after10Years = ProductionDate.AddYears(10) Console.WriteLine("expired on " & after10Years)
Product manufacturing date 19/10/2013 2:25:12 PM expired on 19/10/2023 2:25:12 PM