Re: VB.NET-101 Constructors and Methods

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



Before anything else, thanks Marina, Workgroups and Ralf, for your help.

(Note, I wrote this similar response before, but it got lost while posting
it!)

As per Marina's response, constructors don't need method calls and a new
objects can be instanciated without having any constructors defined. I tried
it in the example and indeed the sum is correctly calculated.

As per Workgoups, constructors can be overloaded, but that is the same for
all procedures isn't it? In the attached modified example, two methods with
the same name use two and three parameters. It works well with and without
constructors.
What is then the added value of using constructors?

The 2nd part of my original question (and code) referred to the observation
that the method with only two-argument constructor caused a build error which
disappeared after adding the empty constructor. I do not understand why.

******************************
Public Class CSum

Public mSumOfTwo As Integer
Public mSumOfThree As Integer

'Public Sub New()
'End Sub

'Public Sub New(ByVal first As Integer, ByVal second As Integer)
'Sum(0, 0)
'End Sub

'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)
mSumOfTwo = first + second

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

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

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

End Class
******************************

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
*************************************
Again, thanks for your support,

John

*************************************

"Workgroups" wrote:

> Perhaps this is what's tripping you up? Methods can have "multiple
> signatures", allowing more than two of the same name:
>
> Sub New ()
>
> We'll call this "Signature #1". And then this...
>
> Sub New (ByVal firstNumber As Integer, ByVal lastNumber As Integer)
>
> We'll call that "Signature #2".
>
> Both of these subroutines called New have the same name, but, they have
> different parameter lists (different datatypes in a different order) in
> their declarations. This gives each of them a unique "signature", a way
> for the compiler and the IDE (and you) to tell the difference between them,
> and allows them to exist as completely seperate methods despite having the
> same name. Signature #1 has no parmeters, whereas signature #2 has two
> integer parameters. These differences are enough to qualify them as unique,
> individual methods.
>
> By having 2 "New" constructors in your CSum class, you have provided
> yourself with 2 constructors (with different signatures) from which to
> choose when creating your class. You can call whichever one you want when
> instantiating your class. Class CSum doesn't care which you use. All you
> have to do is make your constructor call something that matches one of the
> signatures. I.e.,
>
> Dim thisSum As New CSum(10, 100)
>
> That passed two integers to construct a New CSum class object. Since we do
> indeed have a constructor that will accept two integers ("New" having
> Signature #2), that is the constructor that will be utilized (and Signature
> #1 will not be utilized at all, in this case).
>
> Alternatively if you were to say,
>
> Dim thisSum As New CSum
>
> Because no parameters were passed to the constructor, this matches Signature
> #1, the parameter-less constructor, and so that is chosen instead of
> Signature #2.
>
> The alternative to all of this "multiple signature" business would be to
> create a single New constructor with 2 optional integers. It would net you
> the same conceptual result. But creating multiple signatures of the same
> method is typically a cleaner way of doing it if the amount of parameters is
> lengthy, so you don't end up with a method with an optional parameter list a
> mile long. Instead, you can make different versions of the same method
> (with different signatures) that all eventually call a single base worker
> sub, etc.
>
> "John" <John@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> news:8444868B-EAF4-4F1B-AC50-09750A03A9C4@xxxxxxxxxxxxxxxx
> >I "commented out" the functions calls in both constructors. In all examples
> > in my book seen so far, where a method with X parameters was used, method
> > calls with X arguments (unless optional) were placed in each constructor.
> > Do I now understand that such message calls are not always needed?
> > When are they needed and when not?
> >
> > Thanks for your help.
> > John
> >
> > "Marina" wrote:
> >
> >> An empty one is needed if you don't want to call the one with 2
> >> paramters.
> >> You declared the other constructor, but you are not using. Now that it's
> >> there, if you want to use an empty constructor, you havet o add it (as
> >> you
> >> did).
> >>
> >> I'm not sure why you expected errors. You called the Sum method. Then you
> >> retrieved the result - makes sense that it shoudl all work to me.
> >>
> >> "John" <John@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> >> news:2BB95236-5EE1-4B93-8DD7-F90768DF8763@xxxxxxxxxxxxxxxx
> >> > Trying to find out what is essential / optional, I made an extremely
> >> > simple
> >> > Class and Module combination to add two numbers. (see below)
> >> >
> >> > It appears that an empty constructor is needed n order to work right,
> >> > although I quite don't see what is does in addition to the 2nd
> >> > constructor.
> >> >
> >> > Also, the example works fine without message calls in either
> >> > constructor
> >> > (the numerical answer is still there and correct!).
> >> > I exected it to no longer work and return build or run errors.
> >> > Please explain,
> >> >
> >> > Thanks for your help,
> >> > John
> >> >
> >> > ***************
> >> > Public Class CSum
> >> >
> >> > Public mSum As Integer
> >> >
> >> > Public Sub New()
> >> > 'Sum(0, 0)
> >> > End Sub
> >> >
> >> > Public Sub New(ByVal firstNumber As Integer, ByVal lastNumber As
> >> > Integer)
> >> > 'Sum(firstNumber, lastNumber)
> >> > End Sub
> >> >
> >> > Public Sub Sum(ByVal firstNumber As Integer, ByVal lastNumber As
> >> > Integer)
> >> > mSum = firstNumber + lastNumber
> >> > End Sub
> >> >
> >> > End Class
> >> >
> >> > Module Module1
> >> >
> >> > Sub Main()
> >> >
> >> > Dim thisFirstNumber As Integer = 10
> >> > Dim thisLastNumber As Integer = 100
> >> >
> >> > Dim thisSum As New CSum
> >> >
> >> > thisSum.Sum(thisFirstNumber, thisLastNumber)
> >> >
> >> > Console.WriteLine("The sum of {0} and {1} = {2}", _
> >> > thisFirstNumber, thisLastNumber, thisSum.mSum)
> >> >
> >> > End Sub
> >> >
> >> > End Module
> >>
> >>
> >>
>
>
>
.



Relevant Pages

  • Re: VB.NET-101 Constructors and Methods
    ... Sub New ... This gives each of them a unique "signature", ... Class CSum doesn't care which you use. ... have to do is make your constructor call something that matches one of the ...
    (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)
  • A Better Way??
    ... insert images of the signature of the key parties ... In the data sheet I inserted a two column mulit-row table. ... Sub GetSIGBPic() ... Set oShape = ActiveDocument.Shapes ...
    (microsoft.public.word.vba.general)
  • VB.NET101 - Constructors and Methods (2)
    ... argument constructor is never printed. ... Sub Main ... Public Sub Sum ... Console.WriteLine("Value mSumOfTwo after entering empty constructor ...
    (microsoft.public.dotnet.languages.vb)
  • 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)