473,325 Members | 2,442 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,325 software developers and data experts.

Use of the "new" keyword in class members.

Can anyone explain to me a meningfull use of the "new" keyword (Shadows
in VB).

I think i understand how it works fully but i cant figure out why you
would use it. Why would you want to declare a variable in a derived
class with the same name as in the base class but with a diffrent
meaning?

Anyone can think of a scenario for this?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #1
12 1883
Ola Johansson <ol*@whoa.nu> wrote:
Can anyone explain to me a meningfull use of the "new" keyword (Shadows
in VB).

I think i understand how it works fully but i cant figure out why you
would use it. Why would you want to declare a variable in a derived
class with the same name as in the base class but with a diffrent
meaning?

Anyone can think of a scenario for this?


Yes. Suppose you derive from another class, which at the time of
writing doesn't have a member called Foo. You include one, and it
becomes part of your public interface. Now, later the base class gains
a member called Foo - you have three choices:

1) Hide the base member using "new"
2) Rename your member
3) Only use the old version of the class

2 and 3 may well not be viable options, leaving the first one. It's
always an unfortunate situation to find yourself in, but that's why
it's there.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Say I design my own messagebox form, and implement my own Show() method. I
dont want them to use the base so I use new to say im hiding the base and
using my own.
"Ola Johansson" <ol*@whoa.nu> wrote in message
news:uQ**************@TK2MSFTNGP12.phx.gbl...
Can anyone explain to me a meningfull use of the "new" keyword (Shadows
in VB).

I think i understand how it works fully but i cant figure out why you
would use it. Why would you want to declare a variable in a derived
class with the same name as in the base class but with a diffrent
meaning?

Anyone can think of a scenario for this?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #3
<di********@discussion.microsoft.com> wrote:
Say I design my own messagebox form, and implement my own Show() method. I
dont want them to use the base so I use new to say im hiding the base and
using my own.


I would suggest *not* doing that. Hiding members deliberately when
there's no need to is a bad idea, IMO. I would just give the member a
different name. Otherwise users may well get confused and believe
they're calling the original one.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
another reason is that the designer of the base ddin't make a method
virtual because they didn't see the need to override their method,
but you, being much smarter than the original designer have a better
way of doing things, so the compiler is ofrcing you to say 'new'
to make sure you really know what you are doing

JIM

"Ola Johansson" <ol*@whoa.nu> wrote in message
news:uQ**************@TK2MSFTNGP12.phx.gbl...
Can anyone explain to me a meningfull use of the "new" keyword (Shadows
in VB).

I think i understand how it works fully but i cant figure out why you
would use it. Why would you want to declare a variable in a derived
class with the same name as in the base class but with a diffrent
meaning?

Anyone can think of a scenario for this?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #5
james <no****@hypercon.net> wrote:
another reason is that the designer of the base ddin't make a method
virtual because they didn't see the need to override their method,
but you, being much smarter than the original designer have a better
way of doing things, so the compiler is ofrcing you to say 'new'
to make sure you really know what you are doing


But hiding the member is *very* different from overriding it...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
Why not, if you want to replace the MessageBox with youre own and you want
the method to be called Show() there is a good reason for it , Its a new
method with different parameters.

The caller does not NEED toe base Show() method for this class and uses the
"new" Show(...) methods.
Actually having the base Show() method visible is not desireable for this
example.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
<di********@discussion.microsoft.com> wrote:
Say I design my own messagebox form, and implement my own Show() method. I dont want them to use the base so I use new to say im hiding the base and using my own.


I would suggest *not* doing that. Hiding members deliberately when
there's no need to is a bad idea, IMO. I would just give the member a
different name. Otherwise users may well get confused and believe
they're calling the original one.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #7
actually wait I didnt use new, what I did was to use a static Show method
and make the ctor private and construct it in the static Show method. Was
much cleaer , since the basic MessageBox doesnt allow you to localize the
button text (its uses the CurrentUICulture).

<.> wrote in message news:eR*************@TK2MSFTNGP12.phx.gbl...
Why not, if you want to replace the MessageBox with youre own and you want
the method to be called Show() there is a good reason for it , Its a new
method with different parameters.

The caller does not NEED toe base Show() method for this class and uses the "new" Show(...) methods.
Actually having the base Show() method visible is not desireable for this
example.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
<di********@discussion.microsoft.com> wrote:
Say I design my own messagebox form, and implement my own Show()
method.
I dont want them to use the base so I use new to say im hiding the base and using my own.


I would suggest *not* doing that. Hiding members deliberately when
there's no need to is a bad idea, IMO. I would just give the member a
different name. Otherwise users may well get confused and believe
they're calling the original one.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 15 '05 #8
<<.>> wrote:
Why not, if you want to replace the MessageBox with youre own and you want
the method to be called Show() there is a good reason for it , Its a new
method with different parameters.

The caller does not NEED toe base Show() method for this class and uses the
"new" Show(...) methods.
Actually having the base Show() method visible is not desireable for this
example.


In that case you shouldn't be inheriting from MessageBox in the first
place. If an object can't be used as if it were an instance of its base
class, you should be using composition instead.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9
Im not im inheriting from Form and making my own messagebox.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<<.>> wrote:
Why not, if you want to replace the MessageBox with youre own and you want the method to be called Show() there is a good reason for it , Its a new
method with different parameters.

The caller does not NEED toe base Show() method for this class and uses the "new" Show(...) methods.
Actually having the base Show() method visible is not desireable for this example.


In that case you shouldn't be inheriting from MessageBox in the first
place. If an object can't be used as if it were an instance of its base
class, you should be using composition instead.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #10
<<.>> wrote:
Im not im inheriting from Form and making my own messagebox.


In that case your original message was somewhat unclear, I'm afraid.
I'm not sure it's worth pursuing this thread much further though...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #11
Jon Skeet [C# MVP] wrote:
james <no****@hypercon.net> wrote:
another reason is that the designer of the base ddin't make a method
virtual because they didn't see the need to override their method,
but you, being much smarter than the original designer have a better
way of doing things, so the compiler is ofrcing you to say 'new'
to make sure you really know what you are doing

But hiding the member is *very* different from overriding it...


If the base class' method is _not_ virtual, is there really a difference
in the derived class between new and override on the method?

Nov 15 '05 #12
Kevin P. Fleming <kp*******@cox.net> wrote:
But hiding the member is *very* different from overriding it...


If the base class' method is _not_ virtual, is there really a difference
in the derived class between new and override on the method?


It the base class's method isn't virtual, you *can't* override it - you
have no option but to hide it or choose another name.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #13

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

Similar topics

3
by: Paul Auleciems | last post by:
Hi: I'm having trouble using an Object which is created based on the following: Public CarDetail () as Car Where the CLASS "Car" is defined as: Public Class Car
1
by: alanrn | last post by:
I've implemented a number of strongly-typed collections that inherit from CollectionBase and recently noticed something that I don't fully understand. CollectionBase defines method RemoveAt(). ...
6
by: dpr | last post by:
I have come accross a piece of C++ code with the construct: MyClass *c = new class MyClass(); Is there a difference between this and: MyClass *c = new MyClass(); ?
8
by: Dot net work | last post by:
I need VB.NET's "shadows" functionality inside a C# project. I tried the "new" keyword, but it didn't seem to work, because my particular function does in fact differ in signature to the function...
0
by: Michael Steidl | last post by:
I use XMLspy for handling XML files and started testing its feature to generate C# code from XML Schemas. Now I got stuck in a strange situation: (I have to add being not a C# geek, only currently...
5
by: Sam | last post by:
Hi everyone Could anyone help me understand the usage of the "New" keyword I'm new to VB.Net. 1. Why do we use the "New" keyword on some object type variables such as the myPen of the...
4
by: Ben R. | last post by:
I'm curious about the differeng behavior of the "new" keyword when dealing with value versus object types. If I'm correct, when I do: dim x as integer There's no need for "new" to be called...
6
by: vinnie | last post by:
i'm at the very beginning, and while trying to use the following small code, i got an error msg thta said use the "new" keyword to create an object instance. Actually, since i'm at the very...
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: 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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.