473,508 Members | 2,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing members which have been hidden by "new"

Hello,

This example:

public class BaseC
{
public int x;
public void Invoke() {}
}
public class DerivedC : BaseC
{
new public void Invoke() {}
}

is from here: http://msdn2.microsoft.com/en-us/library/435f1dw2.aspx
I can access the original (hidden) method Invoke() by using BaseC.Invoke().

Now look at this:

public class BaseC
{
public int x;
public void Invoke() {}
}

public class DerivedC : BaseC
{
public int y;
}

public class DerivedDerivedC : DerivedC
{
public int z;
new public void Invoke() {}
}

How do I access the original (hidden) method ?

BaseC.Invoke() doesn't work? Why?

regards, Robert

Jun 27 '08 #1
12 4784
On Mon, 21 Apr 2008 15:34:04 -0700, Robert Fuchs <ni*@nix.atwrote:
public class BaseC {
public int x;
public void Invoke() {}
}
public class DerivedC : BaseC
{
new public void Invoke() {}
}

is from here: http://msdn2.microsoft.com/en-us/library/435f1dw2.aspx
I can access the original (hidden) method Invoke() by using
BaseC.Invoke().
Do you mean "base.Invoke()", as from within an instance method for
example? "BaseC.Invoke()" would imply a static method, and "Invoke()" is
not in this case a static method.
Now look at this:

public class BaseC {
public int x;
public void Invoke() {}
}

public class DerivedC : BaseC
{
public int y;
}

public class DerivedDerivedC : DerivedC
{
public int z; new public void Invoke() {}
}

How do I access the original (hidden) method ?

BaseC.Invoke() doesn't work? Why?
Again, do you mean "base.Invoke()"?

If so, then you can't because the immediate base class has no such
method. However, you can cast your instance to BaseC and access the
method that way.

That said, it's almost always a mistake to hide a class member anyway.
It's very easy for code to fail to operate correctly or as expected when
you do so, because the base classes won't necessarily have any way to know
about or use the hidden method.

Pete
Jun 27 '08 #2
If so, then you can't because the immediate base class has no such
method. However, you can cast your instance to BaseC and access the
method that way.
I also thought about casting would do the trick:
((BaseClass)this).MyMethod();
But I have another problem: MyMethod() is protected, and I don't want to
change that.

So I get the error message:
"Cannot access protected member 'member' via a qualifier of type 'type1';
the qualifier must be of type 'type2' (or derived from it)"

Any idea?

regards, Robert

Jun 27 '08 #3
Peter Duniho wrote:
On Mon, 21 Apr 2008 15:34:04 -0700, Robert Fuchs <ni*@nix.atwrote:
>Now look at this:

public class BaseC {
public int x;
public void Invoke() {}
}

public class DerivedC : BaseC
{
public int y;
}

public class DerivedDerivedC : DerivedC
{
public int z; new public void Invoke() {}
}

How do I access the original (hidden) method ?

BaseC.Invoke() doesn't work? Why?

Again, do you mean "base.Invoke()"?

If so, then you can't because the immediate base class has no such
method.
Yes it does, properly inherited from BaseC, and base.Invoke() is just going
to work.

--
J.
http://symbolsprose.blogspot.com
Jun 27 '08 #4
Peter Duniho wrote:
<...>
That said, it's almost always a mistake to hide a class member anyway.
It's very easy for code to fail to operate correctly or as expected when
you do so, because the base classes won't necessarily have any way to
know about or use the hidden method.

Whilst I agree to a point that it's _almost_ always a mistake to hide a
member.
A common pattern I use is with generics.
Say I have:
abstract class Fu
{
public virtual object Value
{
get { return ValueInternal; }
}
protected abstract object ValueInternal{get;}
}

class Bar<T: Fu
{
public new T Value
{
get { return m_Value; }
set { m_Value = value; }
}
protected override object ValueInternal
{
get { return Value; }
}
}
This obviously allows both strong typed functionality with generics and
easy polymorphism with the base non-generic class.

(Yes I know you said _almost_, just wanted to point out a valid reason)

JB
Jun 27 '08 #5
On Mon, 21 Apr 2008 16:39:59 -0700, Robert Fuchs <ni*@nix.atwrote:
>If so, then you can't because the immediate base class has no such
method. However, you can cast your instance to BaseC and access the
method that way.

I also thought about casting would do the trick:
((BaseClass)this).MyMethod();
But I have another problem: MyMethod() is protected, and I don't want to
change that.

So I get the error message:
"Cannot access protected member 'member' via a qualifier of type
'type1'; the qualifier must be of type 'type2' (or derived from it)"
Interesting. I admit, I didn't anticipate that. It looks to me as though
protected members are defined as private with respect to use through their
defining type, accessible implicitly only when visible through inheritance.

I find it a little weird, actually, because it means you can only call
hidden protected methods from within the instance of the deriving class,
while you can call any protected method from any instance.

For example:

class A
{
protected void MethodA() { Console.WriteLine("A.MethodA"); }
protected void MethodC() { Console.WriteLine("A.MethodC"); }
}

class B : A
{
}

class C : B
{
new protected void MethodA() { Console.WriteLine("C.MethodA");
}

public void MethodB()
{
C c = new C();

base.MethodA(); // this works
c.MethodC(); // this works
// ((A)this).MethodA(); // this doesn't
// ((A)c).MethodA(); // this doesn't
}
}
Any idea?
Well, I admit...my previous reply made an assumption, which Jeroen pointed
out was wrong. That is, I assumed that you had actually tried using the
"base." notation and that had failed. Obviously you hadn't, because it
does work and as near as I can tell does just what you want it to.

Pete
Jun 27 '08 #6
On Mon, 21 Apr 2008 18:10:22 -0700, John B <jb******@yahoo.comwrote:
Whilst I agree to a point that it's _almost_ always a mistake to hide a
member.
A common pattern I use is with generics.
Say I have:
abstract class Fu
{
public virtual object Value
{
get { return ValueInternal; }
}
protected abstract object ValueInternal{get;}
}

class Bar<T: Fu
{
public new T Value
{
get { return m_Value; }
set { m_Value = value; }
}
protected override object ValueInternal
{
get { return Value; }
}
}
This obviously allows both strong typed functionality with generics and
easy polymorphism with the base non-generic class.
Well, I'd say that once you allow the polymorphism with the base
non-generic class, you've lost the main advantage of using generics. That
is, you no longer have compile-time type checking for instances of that
class. Why bother with a derived generic in that case?

I also don't understand the point of making the base property virtual, if
you're just going to hide it anyway. Who's going to override it?

If one _is_ going that route, I'd say you might as well just use a
different name for the typed (generic) form of the property. Like
"TypedValue" or something like that. You'll still want to override the
base Value property, but then at least there'd be a point to having the
base class property be "virtual" and the code wouldn't seem so circular.
:)

Obviously I won't go so far as to say hiding a member is _never_ the right
thing to do, but I don't agree that your example is proof of that. :)
It's my opinion that there are better ways to approach a situation like
that.

Pete
Jun 27 '08 #7
Peter Duniho wrote:
On Mon, 21 Apr 2008 18:10:22 -0700, John B <jb******@yahoo.comwrote:
>Whilst I agree to a point that it's _almost_ always a mistake to hide
a member.
A common pattern I use is with generics.
Say I have:
abstract class Fu
{
public virtual object Value
{
get { return ValueInternal; }
}
protected abstract object ValueInternal{get;}
}

class Bar<T: Fu
{
public new T Value
{
get { return m_Value; }
set { m_Value = value; }
}
protected override object ValueInternal
{
get { return Value; }
}
}
This obviously allows both strong typed functionality with generics
and easy polymorphism with the base non-generic class.

Well, I'd say that once you allow the polymorphism with the base
non-generic class, you've lost the main advantage of using generics.
That is, you no longer have compile-time type checking for instances of
that class. Why bother with a derived generic in that case?
In one place I have used this pattern is where I have a base class I use
for reporting criteria.
I have a form that can add/edit/remove criteria and only knows about the
base class.
I use this base form for many report types and only the list of
available criteria changes.
One of the generic parameters (there are 2) denotes the "Field" which is
being targeted by the criteria (ie customer surname) which is redefined
in the generic class from object to <Fand in the "real" concrete class
to an enum type and returns the actual enum item the criteria is targetting.
In the concrete report forms, the criteria are then treated as their
"real" types.

An example is below my signature.

Its kind of hard to explain without an example but the "example" is not
easily extracted out of the whole application.
I also don't understand the point of making the base property virtual,
if you're just going to hide it anyway. Who's going to override it?
True, just habit of making all members virtual in abstract classes
(unless specifically necessary not to).
If one _is_ going that route, I'd say you might as well just use a
different name for the typed (generic) form of the property. Like
"TypedValue" or something like that. You'll still want to override the
base Value property, but then at least there'd be a point to having the
base class property be "virtual" and the code wouldn't seem so
circular. :)
Fair comment, I'm not really a fan though of this though, as I would
automatically type instance.Value which would then bite me as I'd get a
compile error saying ...object cant be converted to X and i'd have to
change it to instance.TypedValue so I'd have to remember to always use
TypedValue.
Obviously I won't go so far as to say hiding a member is _never_ the
right thing to do, but I don't agree that your example is proof of
that. :) It's my opinion that there are better ways to approach a
situation like that.
Quite possible that there are better ways than this. Unfortunately I'm
the only input on this so that limits me to my own ideas :)

JB

enum CustomerFields
{
Surname = 1,
FirstName,
Postcode,
}

abstract class Criteria
{
public object Field { get { return FieldInternal; } }
protected abstract object FieldInternal { get; }
public object Value { get { return ValueInternal; } }
protected abstract object ValueInternal { get; }
}

abstract class Criteria<F: Criteria
{
public abstract new F Field { get; }
protected override object FieldInternal
{
get { return Field; }
}
}

abstract class Criteria<F, V: Criteria<F>
{
V m_Value;

public new V Value
{
get { return m_Value; }
set { m_Value = value; }
}

protected override object ValueInternal
{
get { return Value; }
}
}

class CustomerSurnameCriteria : Criteria<CustomerFields, string>
{
public override CustomerFields Field
{
get { return CustomerFields.Surname; }
}
}
Jun 27 '08 #8
>Any idea?
>
Well, I admit...my previous reply made an assumption, which Jeroen pointed
out was wrong. That is, I assumed that you had actually tried using the
"base." notation and that had failed. Obviously you hadn't, because it
does work and as near as I can tell does just what you want it to.
No, "base." does not what I want, because the immediate base class has no
such method.
That is exactly the point why I asked.
I would need something like "base.base." ;-)

regards, Robert

Jun 27 '08 #9
Robert Fuchs wrote:
>>Any idea?

Well, I admit...my previous reply made an assumption, which Jeroen
pointed out was wrong. That is, I assumed that you had actually
tried using the "base." notation and that had failed. Obviously you
hadn't, because it does work and as near as I can tell does just
what you want it to.

No, "base." does not what I want, because the immediate base class
has no such method.
Do you have a test to prove this? Show source, so we know what you're
talking about.

You cannot access a protected method that is hidden in your base class, only
if your own class is the one hiding it. Then it matters not how deep in the
hierarchy the definition was provided.
That is exactly the point why I asked.
I would need something like "base.base." ;-)

regards, Robert

Jun 27 '08 #10
On Wed, 23 Apr 2008 04:54:49 -0700, Robert Fuchs <ni*@nix.atwrote:
>>Any idea?

Well, I admit...my previous reply made an assumption, which Jeroen
pointed out was wrong. That is, I assumed that you had actually tried
using the "base." notation and that had failed. Obviously you hadn't,
because it does work and as near as I can tell does just what you want
it to.

No, "base." does not what I want, because the immediate base class has
no such method.
See Ben's reply. As Jeroen already pointed out, and now Ben and I both
have as well, "base." will provide access to the nearest base
implementation. It doesn't have to be in the immediately-inherited class.

Pete
Jun 27 '08 #11
You cannot access a protected method that is hidden in your base class,
only if your own class is the one hiding it.
it is in fact my own class which is hiding it, but it does not work, when it
is protected.
Then it matters not how deep in the hierarchy the definition was provided.
that's not true - it matters.

regards, Robert

Jun 27 '08 #12
Robert Fuchs <ni*@nix.atwrote:
You cannot access a protected method that is hidden in your base class,
only if your own class is the one hiding it.

it is in fact my own class which is hiding it, but it does not work, when it
is protected.
Then it matters not how deep in the hierarchy the definition was provided.

that's not true - it matters.
You have ignored Ben's request for code. Here's an example which
matches your original code but with BaseC.Invoke protected and with a
call to base.Invoke() from DerivedDerivedC.

using System;

public class BaseC
{
public int x;
protected void Invoke()
{
Console.WriteLine("BaseC.Invoke");
}
}

public class DerivedC : BaseC
{
public int y;
}

public class DerivedDerivedC : DerivedC
{
public int z;
new public void Invoke()
{
Console.WriteLine("DerivedDeriveC.Invoke");
base.Invoke();
}
}

class Test
{
static void Main()
{
new DerivedDerivedC().Invoke();
}
}

This compiles and runs without problem. Now, to repeat Ben's request,
could you post the code using "base." notation which *doesn't* work?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jun 27 '08 #13

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

Similar topics

3
333
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
6
2500
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(); ?
18
8012
by: Leslaw Bieniasz | last post by:
Cracow, 28.10.2004 Hello, I have a program that intensively allocates and deletes lots of relatively small objects, using "new" operator. The objects themselves are composed of smaller...
1
1558
by: Jean Stax | last post by:
Hi ! A couple of pretty basic questions: Value types: As far as I understand, when I create value type without "new" syntax the object is considered as unutilized. Consequently, I have to...
12
1895
by: Ola Johansson | last post by:
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...
8
13289
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...
1
3847
by: scartin | last post by:
I'm fairly new to working with ASP web controls, and am running into what seems to be a ridiculous problem that I'm hoping will be a breeze for an experienced ASP developer. I have a GridView...
30
3787
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
0
7231
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,...
0
7336
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,...
0
7401
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...
1
7063
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...
0
5640
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3211
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
432
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...

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.