Use of the "new" keyword in class members.

O

Ola Johansson

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?
 
J

Jon Skeet [C# MVP]

Ola Johansson said:
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.
 
G

Guest

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.
 
J

Jon Skeet [C# MVP]

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.
 
J

james

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
 
J

Jon Skeet [C# MVP]

james said:
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...
 
G

Guest

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.
 
G

Guest

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).

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.

method.
 
J

Jon Skeet [C# MVP]

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.
 
J

Jon Skeet [C# MVP]

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...
 
K

Kevin P. Fleming

Jon said:
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?
 
J

Jon Skeet [C# MVP]

Kevin P. Fleming said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top