Welcome to the { mindfrost82.com } forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Go Back   { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Microsoft > .NET Framework

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-12-2007, 01:15 PM
Mike
 
Posts: n/a
Hiding a base function so it is not accessible

According to the MSDN (see reference below), it is possible to change
a base class function from public to private. How is this done? I've
tried using "new", but the function is still accessible.

From MSDN:
"A derived type can hide an inherited member by defining a new member
with the same signature. This might be done to make a previously
public member private or to define new behavior for an inherited
method that is marked as final."

http://msdn2.microsoft.com/en-us/lib...t6(VS.71).aspx
Reply With Quote
  #2 (permalink)  
Old 12-12-2007, 01:27 PM
cfps.Christian
 
Posts: n/a
Re: Hiding a base function so it is not accessible

On Dec 12, 7:15 am, Mike <MLM...@hotmail.com> wrote:
> According to the MSDN (see reference below), it is possible to change
> a base class function from public to private. How is this done? I've
> tried using "new", but the function is still accessible.
>
> From MSDN:
> "A derived type can hide an inherited member by defining a new member
> with the same signature. This might be done to make a previously
> public member private or to define new behavior for an inherited
> method that is marked as final."
>
> http://msdn2.microsoft.com/en-us/lib...t6(VS.71).aspx


Base:
public void DoSomething()
{}

Derived:
private new string DoSomething()
{ return ""; }

its either that or new private string -
The term as I know its used in VB is shadowing methods
Reply With Quote
  #3 (permalink)  
Old 12-12-2007, 01:40 PM
Mike
 
Posts: n/a
Re: Hiding a base function so it is not accessible

Using "new" does not do it. Perhaps it appears to work for you because
you also changed the return type from void to string?

On Dec 12, 8:27 am, "cfps.Christian" <ge0193...@otc.edu> wrote:
> On Dec 12, 7:15 am, Mike <MLM...@hotmail.com> wrote:
>
> > According to the MSDN (see reference below), it is possible to change
> > a base class function from public to private. How is this done? I've
> > tried using "new", but the function is still accessible.

>
> > From MSDN:
> > "A derived type can hide an inherited member by defining a new member
> > with the same signature. This might be done to make a previously
> > public member private or to define new behavior for an inherited
> > method that is marked as final."

>
> >http://msdn2.microsoft.com/en-us/lib...t6(VS.71).aspx

>
> Base:
> public void DoSomething()
> {}
>
> Derived:
> private new string DoSomething()
> { return ""; }
>
> its either that or new private string -
> The term as I know its used in VB is shadowing methods


Reply With Quote
  #4 (permalink)  
Old 12-12-2007, 01:42 PM
Jon Skeet [C# MVP]
 
Posts: n/a
Re: Hiding a base function so it is not accessible

On Dec 12, 1:15 pm, Mike <MLM...@hotmail.com> wrote:
> According to the MSDN (see reference below), it is possible to change
> a base class function from public to private. How is this done? I've
> tried using "new", but the function is still accessible.


You can't. You can create a new method which hides the old method, but
anyone calling "base.Foo()" will still get the old method.

If you don't want to inherit the functionality of a class, don't
derive from it. See Liskov's Substitution Principle:
http://en.wikipedia.org/wiki/Liskov_...tion_principle


Jon
Reply With Quote
  #5 (permalink)  
Old 12-12-2007, 02:20 PM
Mike
 
Posts: n/a
Re: Hiding a base function so it is not accessible

Thanks Jon, but why does the MSDN say it's possible?

Also, I would tend to disagree with the "don't derive from it"
approach. I can't see the benefit in creating a whole new class that
is 99% redundant code. There is a function in the base class that has
parameters. My derived class does not need the parameters because it
always knows what the values should be. I want to replace that
function with one that takes no parameters. Is there another approach
I have overlooked?

On Dec 12, 8:42 am, "Jon Skeet [C# MVP]" <sk...@pobox.com> wrote:
> On Dec 12, 1:15 pm, Mike <MLM...@hotmail.com> wrote:
>
> > According to the MSDN (see reference below), it is possible to change
> > a base class function from public to private. How is this done? I've
> > tried using "new", but the function is still accessible.

>
> You can't. You can create a new method which hides the old method, but
> anyone calling "base.Foo()" will still get the old method.
>
> If you don't want to inherit the functionality of a class, don't
> derive from it. See Liskov's Substitution Principle:http://en.wikipedia.org/wiki/Liskov_...tion_principle
>
> Jon


Reply With Quote
  #6 (permalink)  
Old 12-12-2007, 03:10 PM
Armin Zingler
 
Posts: n/a
Re: Hiding a base function so it is not accessible

"Mike" <MLM450@hotmail.com> schrieb
> Thanks Jon, but why does the MSDN say it's possible?
>
> Also, I would tend to disagree with the "don't derive from it"
> approach. I can't see the benefit in creating a whole new class that
> is 99% redundant code.


The benefit is that it only contains the public members that you want to
offer to the class user - if you want it bullet proof. In addition, the code
doesn't have to be redundant/duplicated because the new class is a
proxy/wrapper delegating all calls to an internal instance of the class that
you are currently deriving from.

A general rule: If an object reference is made available, everything can be
done to the object. If you don't want this, either don't pass the reference,
or only pass a wrapper object.


> There is a function in the base class that
> has
> parameters. My derived class does not need the parameters because it
> always knows what the values should be. I want to replace that
> function with one that takes no parameters. Is there another
> approach
> I have overlooked?


The question is, whether it is forbidden to call the base class' function
when accessing an instance of the derived class. If it is, see above.
Otherwise, if you want an /additional/ function without parameters, you can
add an overloaded method in the derived class.

By definition, inheritance means "inherit everything". "Inherit almost
everything" is not possible.


Armin


> On Dec 12, 8:42 am, "Jon Skeet [C# MVP]" <sk...@pobox.com> wrote:
> > On Dec 12, 1:15 pm, Mike <MLM...@hotmail.com> wrote:
> >
> > > According to the MSDN (see reference below), it is possible to
> > > change
> > > a base class function from public to private. How is this done?
> > > I've
> > > tried using "new", but the function is still accessible.

> >
> > You can't. You can create a new method which hides the old method,
> > but
> > anyone calling "base.Foo()" will still get the old method.
> >
> > If you don't want to inherit the functionality of a class, don't
> > derive from it. See Liskov's Substitution
> > Principle:http://en.wikipedia.org/wiki/Liskov_...tion_principle
> >
> > Jon


Reply With Quote
  #7 (permalink)  
Old 12-12-2007, 03:35 PM
James Crosswell
 
Posts: n/a
Re: Hiding a base function so it is not accessible

Mike wrote:
> Thanks Jon, but why does the MSDN say it's possible?
>
> Also, I would tend to disagree with the "don't derive from it"
> approach. I can't see the benefit in creating a whole new class that
> is 99% redundant code. There is a function in the base class that has
> parameters. My derived class does not need the parameters because it
> always knows what the values should be. I want to replace that
> function with one that takes no parameters. Is there another approach
> I have overlooked?


You don't need to get rid of the old method then - simply oveload it
with your new method.

As for the philosophical discussion, if you were able to remove methods
from classes that you inherited from then .NET wouldn't be able to
support polymorphism... which is a pretty big price to pay for the
fairly rare case in which you'd want to do what you were suggesting.

Best Regards,

James Crosswell
Microforge.net LLC
http://www.microforge.net
Reply With Quote
  #8 (permalink)  
Old 12-12-2007, 03:41 PM
Jon Skeet [C# MVP]
 
Posts: n/a
Re: Hiding a base function so it is not accessible

On Dec 12, 2:20 pm, Mike <MLM...@hotmail.com> wrote:
> Thanks Jon, but why does the MSDN say it's possible?


Basically it's badly written.

> Also, I would tend to disagree with the "don't derive from it"
> approach. I can't see the benefit in creating a whole new class that
> is 99% redundant code. There is a function in the base class that has
> parameters. My derived class does not need the parameters because it
> always knows what the values should be. I want to replace that
> function with one that takes no parameters. Is there another approach
> I have overlooked?


Create a parameterless overload of the method which then calls the one
that takes parameters.

You should ask yourself what should happen if someone calls the method
with a *different* set of parameters though. If you can't handle that,
then you shouldn't be deriving from the class.

Note that you don't always need to reimplement the same functionality
with redundant code, however - use aggregation instead of derivation,
i.e. create an instance of the other class "inside" your class, rather
than deriving from it. Sometimes that won't work, but often it will.

Jon
Reply With Quote
  #9 (permalink)  
Old 12-12-2007, 03:48 PM
Mike
 
Posts: n/a
Re: Hiding a base function so it is not accessible

Thanks for the info. Yes, overloading is needed but I never want the
old method called outside my class because passing the wrong values
will cause problems. I am not really talking about removing the
function, just changing it from public to private. For now, I have
overridden the function I want to hide so it throws an exception when
called. Not pretty, but it keeps anyone from using it.

Thanks.

On Dec 12, 10:35 am, James Crosswell <ja...@microforge.net> wrote:
> Mike wrote:
> > Thanks Jon, but why does the MSDN say it's possible?

>
> > Also, I would tend to disagree with the "don't derive from it"
> > approach. I can't see the benefit in creating a whole new class that
> > is 99% redundant code. There is a function in the base class that has
> > parameters. My derived class does not need the parameters because it
> > always knows what the values should be. I want to replace that
> > function with one that takes no parameters. Is there another approach
> > I have overlooked?

>
> You don't need to get rid of the old method then - simply oveload it
> with your new method.
>
> As for the philosophical discussion, if you were able to remove methods
> from classes that you inherited from then .NET wouldn't be able to
> support polymorphism... which is a pretty big price to pay for the
> fairly rare case in which you'd want to do what you were suggesting.
>
> Best Regards,
>
> James Crosswell
> Microforge.net LLChttp://www.microforge.net


Reply With Quote
  #10 (permalink)  
Old 12-12-2007, 03:54 PM
Jon Skeet [C# MVP]
 
Posts: n/a
Re: Hiding a base function so it is not accessible

On Dec 12, 3:48 pm, Mike <MLM...@hotmail.com> wrote:
> Thanks for the info. Yes, overloading is needed but I never want the
> old method called outside my class because passing the wrong values
> will cause problems.


In other words, someone who has a reference to an instance of your
class and wants to treat it as an instance of the more general class
won't be able to, right?

That's breaking the substitution principle. Sometimes it's pragmatic
to do so, but it's worth being aware of it and avoiding it wherever
possible.

Jon
Reply With Quote
Reply

  { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Microsoft > .NET Framework


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 11:06 AM.


Powered by vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
© 1999-2008 mindfrost82.com v11.0


Sponsors:
Mortgage Calculator | Music Store | Credit Check | Mortgage Loans | Auto Loans



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114