Question:
Default constructor and parameterized constructor in VB.net?
melbourne
2009-11-12 19:30:05 UTC
I am new to VB.NET. There is a question i feel a little confused about. For the code below:

Public Sub New()
_month=string.empty
_day=String.empty
End Sub

Public Sub New (ByVal monthNum As String, ByVal dayNum As String)
Month=monthNum
Day=dayNum
End Sub

The second constructor (parameterized constructor) is called by Dim hireDate As New FormattedDate(monTextBox.Text, dayTextBox.Text) when the object 'hireDate' is created. My confusion is, the first default constructor seems never be called, then why bother being written? I see similar examples like above in my text book that both default and parameterized constructors are written in the Class, but only the parameterized constructor is called, do you must have the default one even if its not called?
Three answers:
anonymous
2009-11-13 18:19:26 UTC
You made the design decision that you will allow the user of the class to create an object with or without a Initializer list for the member of the base class.

you must therefore provide constructors of both type of object instantiation.



If u want to provide an empty FormattedDate you need the first one.
?
2016-10-05 04:44:58 UTC
Vb.net Constructor
Payal Bansal
2009-11-12 20:27:32 UTC
the first can also be called without any parameters like:

Dim hireDate As New FormattedDate()





hope this resolves your confusion :)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...