VB.NET101 - Constructors and Methods (2)

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Before anything else, thanks Marina, Workgroups and Ralf, for your help so far.
I am now able to better define the question!

After adding more console printout lines to CSum, I tried all permutations
for constructors (none, default, two argument) and method call in body of
constructor (none and one).
Maybe this example is not representative, but for this example I found the
following:
1. Without any constructors, the program works fine (new object
instantiated, method called, sum calculated, no build errors listed).
2. With two constructors (empty and two arguments ones), the program works
fine as well.
3. With only the empty constructor, the program also works fine.
4. With only the two argument constructor, the program works, but lists the
build error: ‘Arguments not specified for parameters “first” (and “second”)
of Public Sub New(first As Integer, second As Integer)’. Note: The “Why?” for
this was my original posted question!
5. The only difference using a method call Sum(0,0) in a constructor is that
the Sum method calculates it’s result using two zero arguments before the
arguments listed in Main are passed on to Sum.
6. If used, the Console.WriteLine method placed in the body of the two
argument constructor is never printed. Hence, it looks like the program
pointer never runs though this constructor. Why?

See 1st attachment below.

I also used overloaded methods in the 2nd attachment, with basically the
same results; the method calls for both the two and three argument methods
worked fine without the use of any constructors (so no constructors needed to
make overloading possible!)

The main remaining question is: What is the added value of using a
constructor?
I am sure there must be good reasons why constructors are used and that they
are used for features which otherwise cannot be dealt with.

Your explanation is highly appreciated,

Regards,

John
(Attachment 1)

Module Module1
Sub Main()

Dim thisFirstNumber As Integer = 10
Dim thisSecondNumber As Integer = 100

Dim thisSum As New CSum

thisSum.Sum(thisFirstNumber, thisSecondNumber)

End Sub
End Module

Public Class CSum

Public mSum As Integer = 1

'Public Sub New() 'Empty
constructor
'Console.WriteLine("After entering empty constructor, value of mSum =
{0}", mSum)
'Console.WriteLine("Program pointer jumps to Sum method after this line
to calc Sum(0,0).")
'Sum(0, 0)
'Console.WriteLine("Leaving empty constructor")
'Console.WriteLine()
'End Sub

'Public Sub New(ByVal first As Integer, ByVal second As Integer) 'Two
argument constructor
'Console.WriteLine("After entering 2-par. constructor, value of mSum =
{0}", mSum)
'Console.WriteLine("Program pointer jumps to Sum method after this line
to calc Sum(0,0).")
'Sum(0, 0)
'Console.WriteLine("Leaving 2-par. constructor")
'Console.WriteLine()
'End Sub

Public Sub Sum(ByVal first As Integer, ByVal second As Integer)
'Sum method
Console.WriteLine("Value of mSum before using formula is: {0}", mSum)
mSum = first + second
Console.WriteLine("After using Sum formula: The sum of {0} and {1} =
{2}", first, second, mSum)
End Sub

End Class

(Attachment 2)

Module Module1
Sub Main()

Dim thisFirstNumber As Integer = 10
Dim thisSecondNumber As Integer = 100
Dim thisLastNumber As Integer = 1000

Dim thisSum As New CSum

thisSum.Sum(thisFirstNumber, thisSecondNumber)

thisSum.Sum(thisFirstNumber, thisSecondNumber, thisLastNumber)

End Sub
End Module

Public Class CSum

Public mSumOfTwo As Integer = 1
Public mSumOfThree As Integer = 2

Public Sub New() 'Empty constructor
Console.WriteLine("Value mSumOfTwo after entering empty constructor
is: {0} " _
, mSumOfTwo)
Sum(3, 4)
Console.WriteLine("Value mSumOfTwo after Sum(0,0) in empty constructor
is: {0} " _
, mSumOfTwo)
Console.WriteLine("Value mSumOfThree after entering the empty
constructor is: {0} " _
, mSumOfThree)
Sum(5, 6, 7)
Console.WriteLine("Value mSumOfThree after Sum(5,6,7) in empty
constructor is: {0} " _
, mSumOfThree)
Console.WriteLine()
End Sub

'Two argument CSum constructor
'Public Sub New(ByVal first As Integer, ByVal second As Integer)
'Console.WriteLine("Value mSumOfTwo after entering two argument
constructor is: {0} " _
' , mSumOfTwo)
'Console.WriteLine()
'Sum(0, 0)
'End Sub

'Three argument CSum constructor
Public Sub New(ByVal first As Integer, ByVal second As Integer, ByVal
third As Integer)
Sum(0, 0, 0)
End Sub

Public Sub Sum(ByVal first As Integer, ByVal second As Integer)

Console.WriteLine("Value mSumOfTwo before using the addition formula
is: {0} " _
, mSumOfTwo)

mSumOfTwo = first + second

Console.WriteLine("The sum of {0} and {1} = {2}", _
first, second, mSumOfTwo)
Console.WriteLine()
End Sub

Public Sub Sum(ByVal first As Integer, ByVal second As Integer, ByVal
third As Integer)

Console.WriteLine("Value mSumOfThree before using the addition formula
is: {0} " _
, mSumOfThree)

mSumOfThree = first + second + third

Console.WriteLine("The sum of {0} and {1} and {2} = {3}", _
first, second, third, mSumOfThree)
Console.WriteLine()
End Sub

End Class

******************************************************
Again, thanks for your support,
John


.



Relevant Pages

  • Re: Error trying to display array member
    ... instantiates the array within the constructor, ... Inherits CTwoDimensionalShape ' A triangle is a 2d shape ... Public Sub New ... Public Sub New ...
    (microsoft.public.dotnet.languages.vb)
  • Re: VB.NET-101 Constructors and Methods
    ... > disappeared after adding the empty constructor. ... > 'Public Sub New ... > 'Public Sub New ... > 'Public Sub New(ByVal first As Integer, ByVal second As Integer, ByVal ...
    (microsoft.public.dotnet.languages.vb)
  • Re: VB.NET-101 Constructors and Methods
    ... An empty one is needed if you don't want to call the one with 2 paramters. ... You declared the other constructor, ... > Public Sub New ... > Public Sub New(ByVal firstNumber As Integer, ...
    (microsoft.public.dotnet.languages.vb)
  • RE: VB.NET101 - Constructors and Methods (2)
    ... I have not been following the full discussion, but the constructor will be ... > Sub Main ... > Public Sub Sum ... > Console.WriteLine("Value mSumOfTwo after entering empty constructor ...
    (microsoft.public.dotnet.languages.vb)
  • Re: VB.NET-101 Constructors and Methods
    ... disappeared after adding the empty constructor. ... 'Public Sub New ... 'Public Sub New(ByVal first As Integer, ByVal second As Integer, ByVal ... This gives each of them a unique "signature", ...
    (microsoft.public.dotnet.languages.vb)