473,327 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Overrides and mybase ??

Hello,

I have seen the following routines in a class and don't understand how they
work. For one, why two new() routines? Which is executed? Why not just use
one routine? What does the mybase do in this case? Bit confusing! :) Thanks

'<remarks/>
Public Sub New()
MyBase.New()
Me.Url =
System.Configuration.ConfigurationSettings.AppSett ings("ReportServerURL") +
"/ReportService.asmx"
End Sub

'<remarks/>
Public Sub New(ByVal ReportServerURL As String)
MyBase.New()
Me.Url = ReportServerURL + "/ReportService.asmx"
End Sub
Jun 16 '06 #1
5 2298
These constructors have 2 different parameters. Someone might want to create
an instance of this class and pass in the URL for the report server, or not.

If they don't pass it in, they get the report server from the configuration
file.

Otherwise, whatever they passed in is used.

In both cases, the base class's constructor is called, to make sure that the
entire instance gets instantiated properly. The base class constructor may
do some work without which the class isn't usable.

"TheBee" <Th****@discussions.microsoft.com> wrote in message
news:AF**********************************@microsof t.com...
Hello,

I have seen the following routines in a class and don't understand how
they
work. For one, why two new() routines? Which is executed? Why not just
use
one routine? What does the mybase do in this case? Bit confusing! :)
Thanks

'<remarks/>
Public Sub New()
MyBase.New()
Me.Url =
System.Configuration.ConfigurationSettings.AppSett ings("ReportServerURL")
+
"/ReportService.asmx"
End Sub

'<remarks/>
Public Sub New(ByVal ReportServerURL As String)
MyBase.New()
Me.Url = ReportServerURL + "/ReportService.asmx"
End Sub

Jun 16 '06 #2

TheBee wrote:
Hello,

I have seen the following routines in a class and don't understand how they
work. For one, why two new() routines? Which is executed? Why not just use
one routine? What does the mybase do in this case? Bit confusing! :) Thanks

'<remarks/>
Public Sub New()
MyBase.New()
Me.Url =
System.Configuration.ConfigurationSettings.AppSett ings("ReportServerURL") +
"/ReportService.asmx"
End Sub

'<remarks/>
Public Sub New(ByVal ReportServerURL As String)
MyBase.New()
Me.Url = ReportServerURL + "/ReportService.asmx"
End Sub


MyBase.New in this case is not strictly necessary, since it is simply
calling the base classes default constructor. The only time that
MyBase.New is required is if the Base class can't be constucted without
parameters - in other words, there is no constructor that doesn't take
arguments. You may optionally use it if you want to pass arguments to
the base class constuctor... Maybe a small example to make this clear:

Option Strict On
Option Explicit On

Imports System

Module Module1

' Base with only a default constructor
Private Class Base
Public Sub New(ByVal param As String)
Console.WriteLine("Base New - {0}", param)
End Sub
End Class

Private Class Descendant
Inherits Base

Public Sub New()
Console.WriteLine("Descendant New")
End Sub

Public Sub New(ByVal param As String)
Console.WriteLine("Descendant New - {0}", param)
End Sub

End Class

Sub Main()
Dim a As New Descendant
Dim b As New Descendant("hi")
End Sub

End Module

Now... If you were to add a constructor to the Base class that took
parameters...

Imports System

Module Module1

Private Class Base
Public Sub New()
Console.WriteLine("Base New")
End Sub

Public Sub New(ByVal param As String)
Console.WriteLine("Base New - {0}", param)
End Sub
End Class

Private Class Descendant
Inherits Base

Public Sub New()
Console.WriteLine("Descendant New")
End Sub

Public Sub New(ByVal param As String)
' for fun, lets pass on param to the base!
MyBase.New(param)
Console.WriteLine("Descendant New - {0}", param)
End Sub

End Class
Sub Main()
Dim a As New Descendant
Dim b As New Descendant("hi")
End Sub

End Module

No, if you remove the Base.New () :

Option Strict On
Option Explicit On

Imports System

Module Module1

Private Class Base

Public Sub New(ByVal param As String)
Console.WriteLine("Base New - {0}", param)
End Sub
End Class

Private Class Descendant
Inherits Base

Public Sub New()
MyBase.New("default")
Console.WriteLine("Descendant New")
End Sub

Public Sub New(ByVal param As String)
MyBase.New(param)
Console.WriteLine("Descendant New - {0}", param)
End Sub

End Class
Sub Main()
Dim a As New Descendant
Dim b As New Descendant("hi")
End Sub

End Module

Anyway... Only the last example REQUIRES the call to MyBase.New. Most
times, this is simply a convienient way to base arguments to a base
class constructor. I think a lot of people still explicitly call
MyBase.New() simply because in some of the early VB.NET's (pre 1.0) it
was required...

--
Tom Shelton [MVP]

Jun 16 '06 #3

Tom Shelton wrote:
TheBee wrote:
Hello,

I have seen the following routines in a class and don't understand how they
work. For one, why two new() routines? Which is executed? Why not just use
one routine? What does the mybase do in this case? Bit confusing! :) Thanks

'<remarks/>
Public Sub New()
MyBase.New()
Me.Url =
System.Configuration.ConfigurationSettings.AppSett ings("ReportServerURL") +
"/ReportService.asmx"
End Sub

'<remarks/>
Public Sub New(ByVal ReportServerURL As String)
MyBase.New()
Me.Url = ReportServerURL + "/ReportService.asmx"
End Sub


MyBase.New in this case is not strictly necessary, since it is simply
calling the base classes default constructor. The only time that
MyBase.New is required is if the Base class can't be constucted without
parameters - in other words, there is no constructor that doesn't take
arguments. You may optionally use it if you want to pass arguments to
the base class constuctor... Maybe a small example to make this clear:

Option Strict On
Option Explicit On

Imports System

Module Module1

' Base with only a default constructor
Private Class Base
Public Sub New(ByVal param As String)
Console.WriteLine("Base New - {0}", param)
End Sub
End Class


Crap... I copied in the wrong constructor.. That should simply be:

' Base with only a default constructor
Public Sub New()
Console.WriteLine("Base New")
End Sub

Sorry for the confusion!

--
Tom Shelton [MVP]

Jun 16 '06 #4
Thank you very much for the time you spent replying. It is greatly
appreciated.

So the person who wrote the code really didn't need to do both. They could
have just used the first one? So you can duplicate a name of a routine many
times without issue? Does compiler than automatically choose which to use
based on parameters passed? What if both routines have same parameters with
types? What happens then?

Thanks again

"Tom Shelton" <to*@mtogden.com> wrote in message
news:11**********************@f6g2000cwb.googlegro ups.com...

Tom Shelton wrote:
TheBee wrote:
> Hello,
>
> I have seen the following routines in a class and don't understand how
> they
> work. For one, why two new() routines? Which is executed? Why not
> just use
> one routine? What does the mybase do in this case? Bit confusing! :)
> Thanks
>
> '<remarks/>
> Public Sub New()
> MyBase.New()
> Me.Url =
> System.Configuration.ConfigurationSettings.AppSett ings("ReportServerURL")
> +
> "/ReportService.asmx"
> End Sub
>
> '<remarks/>
> Public Sub New(ByVal ReportServerURL As String)
> MyBase.New()
> Me.Url = ReportServerURL + "/ReportService.asmx"
> End Sub


MyBase.New in this case is not strictly necessary, since it is simply
calling the base classes default constructor. The only time that
MyBase.New is required is if the Base class can't be constucted without
parameters - in other words, there is no constructor that doesn't take
arguments. You may optionally use it if you want to pass arguments to
the base class constuctor... Maybe a small example to make this clear:

Option Strict On
Option Explicit On

Imports System

Module Module1

' Base with only a default constructor
Private Class Base
Public Sub New(ByVal param As String)
Console.WriteLine("Base New - {0}", param)
End Sub
End Class


Crap... I copied in the wrong constructor.. That should simply be:

' Base with only a default constructor
Public Sub New()
Console.WriteLine("Base New")
End Sub

Sorry for the confusion!

--
Tom Shelton [MVP]

Jun 16 '06 #5
The Bee,
| So you can duplicate a name of a routine many
| times without issue?
Yes this is known as overloading.

| Does compiler than automatically choose which to use
| based on parameters passed?
Yes the compiler looks at the number of parameters & their types to decide
which overload to call.

| What if both routines have same parameters with
| types?
The signatures of the methods (routines) need to be unique on number & type
of parameters. Overloading on return value or parameter name will cause a
compile error.
Advanced information:

..NET 2.0 (VS 2005) allows you to overload generic classes on the number of
type parameters, however you cannot overload a generic simply on the
constraint. For example:

System.Nullable - has shared methods pertaining to all nullable types.

System.Nullable(Of T) - creates a specific nullable type, such as
Nullable(Of Integer) & Nullable(Of Double).

The reason for the two Nullable types is to simplify calling of the methods
on System.Nullable...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"The Bee" <Th****@discussions.microsoft.com> wrote in message
news:Oz**************@TK2MSFTNGP03.phx.gbl...
| Thank you very much for the time you spent replying. It is greatly
| appreciated.
|
| So the person who wrote the code really didn't need to do both. They
could
| have just used the first one? So you can duplicate a name of a routine
many
| times without issue? Does compiler than automatically choose which to use
| based on parameters passed? What if both routines have same parameters
with
| types? What happens then?
|
| Thanks again
|
| "Tom Shelton" <to*@mtogden.com> wrote in message
| news:11**********************@f6g2000cwb.googlegro ups.com...
| >
| > Tom Shelton wrote:
| >> TheBee wrote:
| >> > Hello,
| >> >
| >> > I have seen the following routines in a class and don't understand
how
| >> > they
| >> > work. For one, why two new() routines? Which is executed? Why not
| >> > just use
| >> > one routine? What does the mybase do in this case? Bit confusing!
:)
| >> > Thanks
| >> >
| >> > '<remarks/>
| >> > Public Sub New()
| >> > MyBase.New()
| >> > Me.Url =
| >> >
System.Configuration.ConfigurationSettings.AppSett ings("ReportServerURL")
| >> > +
| >> > "/ReportService.asmx"
| >> > End Sub
| >> >
| >> > '<remarks/>
| >> > Public Sub New(ByVal ReportServerURL As String)
| >> > MyBase.New()
| >> > Me.Url = ReportServerURL + "/ReportService.asmx"
| >> > End Sub
| >>
| >> MyBase.New in this case is not strictly necessary, since it is simply
| >> calling the base classes default constructor. The only time that
| >> MyBase.New is required is if the Base class can't be constucted without
| >> parameters - in other words, there is no constructor that doesn't take
| >> arguments. You may optionally use it if you want to pass arguments to
| >> the base class constuctor... Maybe a small example to make this clear:
| >>
| >> Option Strict On
| >> Option Explicit On
| >>
| >> Imports System
| >>
| >> Module Module1
| >>
| >> ' Base with only a default constructor
| >> Private Class Base
| >> Public Sub New(ByVal param As String)
| >> Console.WriteLine("Base New - {0}", param)
| >> End Sub
| >> End Class
| >>
| >
| > Crap... I copied in the wrong constructor.. That should simply be:
| >
| > ' Base with only a default constructor
| > Public Sub New()
| > Console.WriteLine("Base New")
| > End Sub
| >
| > Sorry for the confusion!
| >
| > --
| > Tom Shelton [MVP]
| >
|
|
Jun 17 '06 #6

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

Similar topics

4
by: Christopher W. Douglas | last post by:
I am developing a VB.NET app using Visual Studio.NET 2003. VB.NET allows me to create a class with two or more methods that have the same name, as long as they have different (non-optional)...
3
by: TJS | last post by:
I want to insert a message with an override if a condition exists but the overrides does not kick in. How do I invoke the overrides to display a message from my class code? ============Overrides...
4
by: Charles Law | last post by:
In a form, there is a set of events that can be handled. There is also a list of (Overrides). So, for the Closed event, for example, there is an override OnClosed. Where should exit code be...
5
by: Iouri | last post by:
I have added the Overrides proc onMouseMove like Protected Overrides Sub onMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs) ........... End Sub This proc supposed to fire when I...
10
by: Atif | last post by:
Hi I am here to solve a small confusion i have in "Overloads Overrides". "Overloading" says that the method's name should be same while no. of parameters and/or their datatypes should be changed...
4
by: Anthony | last post by:
Hi Folks, I'm adding some columns to my datagrid which are of Combo Box type. I'm inheriting DataGridTextBoxColumn and doing all the usual stuff to get them populated. This is working fine. I...
2
by: Kalvin | last post by:
I found some code in Google, don't remember where, for an AutoComplete combobox. Everything is great with it except for one thing. If I use the mouse to drop the list down, then start typing to...
2
by: Matt Brown - identify | last post by:
Hello, I'm very new to this level of programming and .net in general. My background is VB. I am attempting to use the WebBrowser control to load a flash movie, and it is a known issue...
12
by: André | last post by:
Hi, i'm learning working with classes. In class "classbase1", i defined an overridable function. In the class "subclass1", i defined the same function with 'Overrides'. In class "classbase2", i...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.