473,569 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Configur ation.Configura tionSettings.Ap pSettings("Repo rtServerURL") +
"/ReportService.a smx"
End Sub

'<remarks/>
Public Sub New(ByVal ReportServerURL As String)
MyBase.New()
Me.Url = ReportServerURL + "/ReportService.a smx"
End Sub
Jun 16 '06 #1
5 2312
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****@discuss ions.microsoft. com> wrote in message
news:AF******** *************** ***********@mic rosoft.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.Configur ation.Configura tionSettings.Ap pSettings("Repo rtServerURL")
+
"/ReportService.a smx"
End Sub

'<remarks/>
Public Sub New(ByVal ReportServerURL As String)
MyBase.New()
Me.Url = ReportServerURL + "/ReportService.a smx"
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.Configur ation.Configura tionSettings.Ap pSettings("Repo rtServerURL") +
"/ReportService.a smx"
End Sub

'<remarks/>
Public Sub New(ByVal ReportServerURL As String)
MyBase.New()
Me.Url = ReportServerURL + "/ReportService.a smx"
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.WriteLi ne("Base New - {0}", param)
End Sub
End Class

Private Class Descendant
Inherits Base

Public Sub New()
Console.WriteLi ne("Descendant New")
End Sub

Public Sub New(ByVal param As String)
Console.WriteLi ne("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.WriteLi ne("Base New")
End Sub

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

Private Class Descendant
Inherits Base

Public Sub New()
Console.WriteLi ne("Descendant New")
End Sub

Public Sub New(ByVal param As String)
' for fun, lets pass on param to the base!
MyBase.New(para m)
Console.WriteLi ne("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.WriteLi ne("Base New - {0}", param)
End Sub
End Class

Private Class Descendant
Inherits Base

Public Sub New()
MyBase.New("def ault")
Console.WriteLi ne("Descendant New")
End Sub

Public Sub New(ByVal param As String)
MyBase.New(para m)
Console.WriteLi ne("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.Configur ation.Configura tionSettings.Ap pSettings("Repo rtServerURL") +
"/ReportService.a smx"
End Sub

'<remarks/>
Public Sub New(ByVal ReportServerURL As String)
MyBase.New()
Me.Url = ReportServerURL + "/ReportService.a smx"
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.WriteLi ne("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.WriteLi ne("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.co m> wrote in message
news:11******** **************@ f6g2000cwb.goog legroups.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.Configur ation.Configura tionSettings.Ap pSettings("Repo rtServerURL")
> +
> "/ReportService.a smx"
> End Sub
>
> '<remarks/>
> Public Sub New(ByVal ReportServerURL As String)
> MyBase.New()
> Me.Url = ReportServerURL + "/ReportService.a smx"
> 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.WriteLi ne("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.WriteLi ne("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****@discuss ions.microsoft. com> wrote in message
news:Oz******** ******@TK2MSFTN GP03.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.co m> wrote in message
| news:11******** **************@ f6g2000cwb.goog legroups.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.Configur ation.Configura tionSettings.Ap pSettings("Repo rtServerURL")
| >> > +
| >> > "/ReportService.a smx"
| >> > End Sub
| >> >
| >> > '<remarks/>
| >> > Public Sub New(ByVal ReportServerURL As String)
| >> > MyBase.New()
| >> > Me.Url = ReportServerURL + "/ReportService.a smx"
| >> > 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.WriteLi ne("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.WriteLi ne("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
2097
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) arguments, such as: FirstClass Public Sub Populate(string) Public Sub Populate(string, string) I don't have to use Overloads, because both methods...
3
1114
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 code================ Protected Overrides Sub Render(writer As HtmlTextWriter) if Application("idx") = true then Dim vMessage As String =...
4
1336
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 placed: in the Closed event handler, or in the OnClosed override function? What is the difference, and why have the two? When would I use each one?
5
2250
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 move the mouse. When I am moving mouse over the form it fires. But if mouse is over any control (textbox, listbox
10
13779
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 either in the same class or inherited class. right? "Overriding" says that the method's signature should be same(name,no. of parameters and...
4
1797
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 have added some functionality where if the user right clicks the combobox a context menu will appear, saying "Fill the complete column with value...
2
2216
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 find the item in the list, the visible text changes, but the values of me.Text and Mybase.Text do not change. If you use the mouse and actually...
2
2258
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 that the WebBrowser control doesn't natively support mouse clicks (unless the form loses and regains focus).
12
2379
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 defined the same function and in the class "subclass2", i defined the same function with 'Overloads'. Result: i get the same output.
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7926
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. ...
0
8132
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...
1
7678
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7982
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
944
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.