473,667 Members | 2,703 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 4808
On Mon, 21 Apr 2008 15:34:04 -0700, Robert Fuchs <ni*@nix.atwrot e:
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)thi s).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.atwrot e:
>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{g et;}
}

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.atwrot e:
>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)thi s).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.WriteLi ne("A.MethodA") ; }
protected void MethodC() { Console.WriteLi ne("A.MethodC") ; }
}

class B : A
{
}

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

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

base.MethodA(); // this works
c.MethodC(); // this works
// ((A)this).Metho dA(); // 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{g et;}
}

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{g et;}
}

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.TypedV alue 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 CustomerSurname Criteria : Criteria<Custom erFields, 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

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
2508
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
8055
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 objects, again allocated using "new". From my tests I deduce that a considerable part of the computational time is spent on the memory allocation, which makes the program substantially slower compared to the equivalent code written using
1
1565
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 initialize its member variables manually; otherwise I would get an exception while accessing them.
12
1907
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 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?
8
13296
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 that is being hidden in the base class. In VB.NET, the shadows keyword hides all the inherited functions, regardless of signature. I need this functionality in C#.
1
3876
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 and a FormView on the same page, where the GridView is visible at page load, and the FormView is set to Edit mode by default, but is hidden on the page. I have the Select link enabled on my GridView, and I use the SelectedIndexChanged event to hide...
30
3811
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 the end of the app for example: class test { public: int x;
0
8367
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8889
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8650
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7391
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4202
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2781
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 we have to send another system
2
2017
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1779
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.