473,746 Members | 2,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VB.NET-101 Constructors and Methods

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(firstNumbe r, 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(thi sFirstNumber, thisLastNumber)

Console.WriteLi ne("The sum of {0} and {1} = {2}", _
thisFirstNumber , thisLastNumber, thisSum.mSum)

End Sub

End Module
Nov 21 '05 #1
10 1761
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" <Jo**@discussio ns.microsoft.co m> wrote in message
news:2B******** *************** ***********@mic rosoft.com...
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(firstNumbe r, 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(thi sFirstNumber, thisLastNumber)

Console.WriteLi ne("The sum of {0} and {1} = {2}", _
thisFirstNumber , thisLastNumber, thisSum.mSum)

End Sub

End Module

Nov 21 '05 #2
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" <Jo**@discussio ns.microsoft.co m> wrote in message
news:2B******** *************** ***********@mic rosoft.com...
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(firstNumbe r, 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(thi sFirstNumber, thisLastNumber)

Console.WriteLi ne("The sum of {0} and {1} = {2}", _
thisFirstNumber , thisLastNumber, thisSum.mSum)

End Sub

End Module


Nov 21 '05 #3
Sorry, I am not following at all as far as what the parameters in your
methods have to do with your constructors. They are completely separate and
unrelated.

Delete all the constructors in that class you posted as an example. Have
just the Sum method left and variable declaration. Everything should work
fine.

Might be time to get a new book.

"John" <Jo**@discussio ns.microsoft.co m> wrote in message
news:84******** *************** ***********@mic rosoft.com...
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" <Jo**@discussio ns.microsoft.co m> wrote in message
news:2B******** *************** ***********@mic rosoft.com...
> 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(firstNumbe r, 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(thi sFirstNumber, thisLastNumber)
>
> Console.WriteLi ne("The sum of {0} and {1} = {2}", _
> thisFirstNumber , thisLastNumber, thisSum.mSum)
>
> End Sub
>
> End Module


Nov 21 '05 #4

"John" <Jo**@discussio ns.microsoft.co m> wrote in message
news:2B******** *************** ***********@mic rosoft.com...
:
: 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(firstNumbe r, 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(thi sFirstNumber, thisLastNumber)
:
: Console.WriteLi ne("The sum of {0} and {1} = {2}", _
: thisFirstNumber , thisLastNumber, thisSum.mSum)
:
: End Sub
:
: End Module
I'll be honest and acknowledge up front I'm not entirely sure what you
are asking, so plz forgive me if this doesn't address your qurestion.
The constructors are needed in order to instantiate the class CSum. The
2nd constructor is in fact unneeded in the context you've offered here.
The Sub 'Sum' stands alone - all you need is an instance of the CSum
class and you can call it from there:

'************** *************** *************** ***
Imports System

Public Class CSum
Public mSum As Integer

Public Sub New()
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(thi sFirstNumber, thisLastNumber)

Console.WriteLi ne("The sum of {0} and {1} = {2}", _
thisFirstNumber , thisLastNumber, thisSum.mSum)
End Sub
End Module

'************** *************** *************** ***
As an alternative, you could have instead declared the CSum class as a
structure. In this case, no contructor is needed at all (in fact, you
get compile errors if you declare an empty structure (uncomment the Sub
New() lines below to see what I mean):
'************** *************** *************** ***
Public Structure CSum
Public mSum As Integer

' Public Sub New()
' End Sub

Public Sub Sum(ByVal firstNumber As Integer, _
ByVal lastNumber As Integer)
mSum = firstNumber + lastNumber
End Sub

End Structure

'************** *************** *************** ***
An alternative approach would be to declare Sum as a shared fuction:

'************** *************** *************** ***
Imports System

Public Class CSum
Public mSum As Integer

Private Sub New()
End Sub

Public Shared Function Sum( _
ByVal firstNumber As Integer, _
ByVal lastNumber As Integer) As Integer
Return firstNumber + lastNumber
End Function

End Class

Module Module1
Sub Main()
Dim thisFirstNumber As Integer = 10
Dim thisLastNumber As Integer = 100
Dim thisSum As Integer

thisSum = CSum.Sum(thisFi rstNumber, thisLastNumber)

Console.WriteLi ne("The sum of {0} and {1} = {2}", _
thisFirstNumber , thisLastNumber, thisSum)
End Sub
End Module

'************** *************** *************** ***

In this case, you can't create an instance of the CSum class as there
aren't any publicly accessible constructors you can invoke with the New
keyword.

HTH,

Ralf
Nov 21 '05 #5
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" <Jo**@discussio ns.microsoft.co m> wrote in message
news:84******** *************** ***********@mic rosoft.com...
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" <Jo**@discussio ns.microsoft.co m> wrote in message
news:2B******** *************** ***********@mic rosoft.com...
> 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(firstNumbe r, 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(thi sFirstNumber, thisLastNumber)
>
> Console.WriteLi ne("The sum of {0} and {1} = {2}", _
> thisFirstNumber , thisLastNumber, thisSum.mSum)
>
> End Sub
>
> End Module


Nov 21 '05 #6
gg
pardon me for jumping in.
Please explain when and why would one use shared function without static
variable inside?

I am new to .net and trying to lean. SO please forgive me if I asked a dump
question.

"_AnonCowar d" <ab*@xyz.com> wrote in message
news:X_******** *************@t wister.southeas t.rr.com...

"John" <Jo**@discussio ns.microsoft.co m> wrote in message
news:2B******** *************** ***********@mic rosoft.com...
:
: 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(firstNumbe r, 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(thi sFirstNumber, thisLastNumber)
:
: Console.WriteLi ne("The sum of {0} and {1} = {2}", _
: thisFirstNumber , thisLastNumber, thisSum.mSum)
:
: End Sub
:
: End Module
I'll be honest and acknowledge up front I'm not entirely sure what you
are asking, so plz forgive me if this doesn't address your qurestion.
The constructors are needed in order to instantiate the class CSum. The
2nd constructor is in fact unneeded in the context you've offered here.
The Sub 'Sum' stands alone - all you need is an instance of the CSum
class and you can call it from there:

'************** *************** *************** ***
Imports System

Public Class CSum
Public mSum As Integer

Public Sub New()
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(thi sFirstNumber, thisLastNumber)

Console.WriteLi ne("The sum of {0} and {1} = {2}", _
thisFirstNumber , thisLastNumber, thisSum.mSum)
End Sub
End Module

'************** *************** *************** ***
As an alternative, you could have instead declared the CSum class as a
structure. In this case, no contructor is needed at all (in fact, you
get compile errors if you declare an empty structure (uncomment the Sub
New() lines below to see what I mean):
'************** *************** *************** ***
Public Structure CSum
Public mSum As Integer

' Public Sub New()
' End Sub

Public Sub Sum(ByVal firstNumber As Integer, _
ByVal lastNumber As Integer)
mSum = firstNumber + lastNumber
End Sub

End Structure

'************** *************** *************** ***
An alternative approach would be to declare Sum as a shared fuction:

'************** *************** *************** ***
Imports System

Public Class CSum
Public mSum As Integer

Private Sub New()
End Sub

Public Shared Function Sum( _
ByVal firstNumber As Integer, _
ByVal lastNumber As Integer) As Integer
Return firstNumber + lastNumber
End Function

End Class

Module Module1
Sub Main()
Dim thisFirstNumber As Integer = 10
Dim thisLastNumber As Integer = 100
Dim thisSum As Integer

thisSum = CSum.Sum(thisFi rstNumber, thisLastNumber)

Console.WriteLi ne("The sum of {0} and {1} = {2}", _
thisFirstNumber , thisLastNumber, thisSum)
End Sub
End Module

'************** *************** *************** ***

In this case, you can't create an instance of the CSum class as there
aren't any publicly accessible constructors you can invoke with the New
keyword.

HTH,

Ralf

Nov 21 '05 #7
"gg" <no@Email.pls > wrote in message
news:O7******** ******@TK2MSFTN GP09.phx.gbl...
: pardon me for jumping in.
: Please explain when and why would one use shared function without
: static variable inside?
:
: I am new to .net and trying to lean. SO please forgive me if I
: asked a dump question.
According to the documentation that accompanies the SDK:
"The Shared keyword indicates that one or more declared programming
elements are shared. Shared elements are not associated with a specific
instance of a class or structure. You can access them by qualifying them
either with the class or structure name, or with the variable name of a
specific instance of the class or structure."
In short, a Shared (static in C#) function is one that exposes
functionality that is not part of a given instance. Shared functions may
not operate on instance variables. The previous example, while trivial,
is an example:
Public Class CSum
Public Shared Function Sum(ByVal First As Integer, _
ByVal Last As Integer) As Integer
Return First + Last
End Function
End Class
The function simply returns the sum of two integers passed in. You don't
need an instance of the CSum class to access this functionality.
A non-trivial example of a Shared function is the System.Console class.
You cannot create an instance of this class nor do you need to in order
to use the various functions it exposes (e.g.: WriteLine).
HTH

Ralf
Nov 21 '05 #8
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.WriteLi ne("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.WriteLi ne("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 thisSecondNumbe r As Integer = 100
Dim thisLastNumber As Integer = 1000

Dim thisSum As New CSum

thisSum.Sum(thi sFirstNumber, thisSecondNumbe r)

thisSum.Sum(thi sFirstNumber, thisSecondNumbe r, 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" <Jo**@discussio ns.microsoft.co m> wrote in message
news:84******** *************** ***********@mic rosoft.com...
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" <Jo**@discussio ns.microsoft.co m> wrote in message
news:2B******** *************** ***********@mic rosoft.com...
> 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(firstNumbe r, 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(thi sFirstNumber, thisLastNumber)
>
> Console.WriteLi ne("The sum of {0} and {1} = {2}", _
> thisFirstNumber , thisLastNumber, thisSum.mSum)
>
> End Sub
>
> End Module


Nov 21 '05 #9
I guess that this tread is no longer followed. I will post it as a new one.
John
*************** *********

"John" wrote:
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.WriteLi ne("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.WriteLi ne("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 thisSecondNumbe r As Integer = 100
Dim thisLastNumber As Integer = 1000

Dim thisSum As New CSum

thisSum.Sum(thi sFirstNumber, thisSecondNumbe r)

thisSum.Sum(thi sFirstNumber, thisSecondNumbe r, 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" <Jo**@discussio ns.microsoft.co m> wrote in message
news:84******** *************** ***********@mic rosoft.com...
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" <Jo**@discussio ns.microsoft.co m> wrote in message
> news:2B******** *************** ***********@mic rosoft.com...
> > 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(firstNumbe r, 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(thi sFirstNumber, thisLastNumber)
> >
> > Console.WriteLi ne("The sum of {0} and {1} = {2}", _
> > thisFirstNumber , thisLastNumber, thisSum.mSum)
> >
> > End Sub
> >
> > End Module
>
>
>


Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4049
by: lawrence | last post by:
I haven't been able to reach www.php.net for days. Most of the rest of the web is working for me, though I've bad trouble reaching any English sites. Anyone else having trouble?
0
1480
by: Georgeo Pulikkathara[MS] | last post by:
ASP.NET Webcast Week - January 19 - 23, 2004 Learn about ASP.NET from the experts! These free webcasts are live and interactive. Live code demos and attendees asking in depth engaging questions are all part of these live webcasts. This is a great way to get educated, engaged, and enlightened on Microsoft ASP.NET. Register for all 12 webcasts at http://msdn.microsoft.com/asp.net/using/training/webcasts. ...
1
2972
by: Novice | last post by:
Hi all, I am a C++ and Java developer with over 3 years of industry experience. I've written low level C++ code, in addition to web clients that use web services. I've just recently installed the Visual Studio .net Professional trial version 2003. I have been reading up various documents that discuss - "What is Microsoft .Net" and have found some enlightening information. I'm trying to write a paper on security and software...
20
3343
by: Olav.NET | last post by:
I am a .NET/C++ developer who is supposed to do some work with Access. I do not know much about it except for the DB part. Questions: *1* I am looking for INTENSIVE books to get quickly up to speed. I like books with practical exercises, and also with test questions (like cert books) *2*
0
1444
by: Georgeo Pulikkathara[MS] | last post by:
ASP.NET Webcast Week - January 19 - 23, 2004 Learn about ASP.NET from the experts! These free webcasts are live and interactive. Live code demos and attendees asking in depth engaging questions are all part of these live webcasts. This is a great way to get educated, engaged, and enlightened on Microsoft ASP.NET. Register for all 12 webcasts at http://msdn.microsoft.com/asp.net/using/training/webcasts. ...
3
3048
by: hussein | last post by:
'm not bale to run my projec on the 2000 server, he error was: error while trying to run project: unable to start debugging on the web server.erver side error occurred on sending debug HTTP request mak sure that the server is running correctly . verify that there are o syntax errors in the web.config... i check the IIS, it is running correctly, also the webconfig so what am i sposed to to do be able to run it
9
2761
by: Bob | last post by:
Is ASP.NET 1.1 available on the 64 bit extended version of Windows 2003 Server? When I install VS.NET 2003 I then get Service Unavailable from IIS when navigating to the main under construction site (http://localhost) and I get error 'HTTP/1.1 503 Service Unavailable' when trying to start a new ASP.NET project. I uninstalled IIS and reinstalled IIS, and the under construction default page comes up normally, I ran aspnet_regiis -i and then...
132
4862
by: Kevin Spencer | last post by:
About 2 years ago, and as recently as perhaps 1 year ago, I can recall seeing many posts about what language to use with ASP.Net. The consensus was that employers paid more for C# programmers, and it seems that C# became the darling of the ASP.Net crowd. In the meantime, I have observed an interesting phenomenon. Originally, employers hired programmers who used C# because it was based on C, and the prevailing opinion was (and may still...
16
2167
by: Nathan Sokalski | last post by:
I have Visual Studio .NET and SQL Server Desktop Engine on my computer. I have created an empty database using Visual Studio .NET's Server Explorer. However, I am having trouble connecting to the database using ASP.NET. I think the problem is somewhere in my connection string, but because I do not know much about connection strings, I am not sure what it should look like. Can someone please help me determine what my connection string should...
2
1228
by: Juan T. Llibre | last post by:
Welcome to the ASP.NET FAQ. Thank you for reading this FAQ! Use it to improve your online experience. The Microsoft Public Newsgroups allow users of Microsoft products to exchange technical information and expertise. Please avoid personal attacks, slurs and profanity in your posts. A FAQ is a list of Frequently Answered Questions; This is a guide to using this newsgroup intelligently in order to find answers to your ASP.NET...
0
8800
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9500
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8227
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6060
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4575
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4836
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3292
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.