** Please write in Visual Basic** create a class called DateInformation that inc
ID: 3936741 • Letter: #
Question
** Please write in Visual Basic**
create a class called DateInformation that includes three pieces of information as instance variables - a month (type Integer), a day (type Integer), and a year (type Integer). Your class should provide properties that enable a client of the class to Get and Set the month, day, and year values. The Set accessors should perform validation and throw exceptions for invalid values. Your class shoud have a constructor that initializes the three instance variables and uses the class's properties to set each instance variable's value. Provide a method ToString that returns the month, day, and year separated by forward slashes.
Explanation / Answer
'DateInformation class defintion
Public Class DateInformation
'instance variables of class
Private _month As Integer
Private _day As Integer
Private _year As Integer
'Constructor of class
Sub New()
_month = 0
_day = 0
_year = 0
End Sub
'Constructor of class
Sub New(ByVal m As Integer, ByVal d As Integer, ByVal y As Integer)
_month = m
_day = d
_year = y
End Sub
'Month property
Public Property Month() As String
Get
Return _month
End Get
Set(ByVal value As String)
_month = value
End Set
End Property
'Day property
Public Property Day() As String
Get
Return _day
End Get
Set(ByVal value As String)
_day = value
End Set
End Property
'Year property
Public Property Year() As String
Get
Return _year
End Get
Set(ByVal value As String)
_year = value
End Set
End Property
'Override toString method
Public Overrides Function ToString() As String
Return String.Format("Date : {0}/{1}/{2}", _month, _day, _year)
End Function
End Class
--------------------------------------------------------------------------------------------------------------------
''Test module1 for DateInformation class
Module Module1
Sub Main()
'Create an instance of DateInformation with today's date month, day and year
Dim today As DateInformation = New DateInformation()
'Set properties
'set month
today.Month = 11
'set day
today.Day = 8
'set year
today.Year = 2016
'Call toString method on today object
Console.WriteLine(today.ToString())
'pause program output on console
Console.ReadLine()
End Sub
End Module
--------------------------------------------------------------------------------------------------------------------
Sample Output:
Date : 11/8/2016
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.