Hiding Equals and ReferenceEquals?

  • Thread starter Thread starter Bruce
  • Start date Start date
B

Bruce

I posted this on the dotnet.languages.vc group but did not get a
response. I figure that I could apply what I learn from CSharp to VC,
so here goes.

I have a class in a class assembly, that just has a bunch of static
methods. How can I hide the Equals and ReferenceEquals properties
from my end user programmer?
 
"Bruce" <[email protected]> a écrit dans le message de (e-mail address removed)...

| I have a class in a class assembly, that just has a bunch of static
| methods. How can I hide the Equals and ReferenceEquals properties
| from my end user programmer?

Anything that is marked public cannot be hidden from other users of that
class. The only way to hide something is to mark it as private, protected or
internal, depending on the degreee of hiding desired.

Joanna
 
Joanna said:
"Bruce" <[email protected]> a écrit dans le message de (e-mail address removed)...

| I have a class in a class assembly, that just has a bunch of static
| methods. How can I hide the Equals and ReferenceEquals properties
| from my end user programmer?

Anything that is marked public cannot be hidden from other users of that
class. The only way to hide something is to mark it as private, protected or
internal, depending on the degreee of hiding desired.

Joanna


Is there any way to derive my class from something other than Object
then? How does Microsoft do it with Microsoft.VisualBasic.ChDir() for
example? I take it VisualBasic is a namespace? If so, how do they
export Chdir, doesn't it have to be a part of a class?
 
"Bruce" <[email protected]> a écrit dans le message de %[email protected]...

| Is there any way to derive my class from something other than Object
| then?

No, all .NET types derive from System.Object.

| How does Microsoft do it with Microsoft.VisualBasic.ChDir() for
| example? I take it VisualBasic is a namespace? If so, how do they
| export Chdir, doesn't it have to be a part of a class?

ChDir is an unmanaged function in a DLL, it is not part of the .NET
framework classes. VB seems to be a bit different from C# in that it allows
module functions unconnected with classes. Personally, I think this causes
confusion and is more than likely there only for legacy compatibility.

What are you trying to achieve and why ?

Joanna
 
Hi Bruce,

Actually, you can hide any member from the IDE and IntelliSense by applying
EditorBrowsableAttribute to its override or "new" implementation:

[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object obj)
{
return base.Equals(obj);
}

[EditorBrowsable(EditorBrowsableState.Never)]
public static new bool ReferenceEquals(object objA, object objB)
{
return object.ReferenceEquals(objA, objB);
}

The IDE ignores EditorBrowsableAttribute from within the same project so it
only has an affect on referenced projects. Also, this doesn't prevent
consumption of the attributed members, only their presence in IntelliSense.
That means that you should still provide an implementation (as I've done
above) so that callers can use the methods if they choose. i.e., Even with
the Equals method attributed as above callers can still write:

bool equals = yourObjInst.Equals(anotherObj);

although they won't get any editor support for calling the method.

To be perfectly honest though, I can't see why you would want to hide either
of these methods in the first place, so I'd recommend that you don't do this
unless you have a really good reason to.

This works in C# and VB.NET, but I don't know about managed C++.
 
Dave said:
Hi Bruce,

Actually, you can hide any member from the IDE and IntelliSense by applying
EditorBrowsableAttribute to its override or "new" implementation:

[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object obj)
{
return base.Equals(obj);
}

[EditorBrowsable(EditorBrowsableState.Never)]
public static new bool ReferenceEquals(object objA, object objB)
{
return object.ReferenceEquals(objA, objB);
}

The IDE ignores EditorBrowsableAttribute from within the same project so it
only has an affect on referenced projects. Also, this doesn't prevent
consumption of the attributed members, only their presence in IntelliSense.
That means that you should still provide an implementation (as I've done
above) so that callers can use the methods if they choose. i.e., Even with
the Equals method attributed as above callers can still write:

bool equals = yourObjInst.Equals(anotherObj);

although they won't get any editor support for calling the method.

To be perfectly honest though, I can't see why you would want to hide either
of these methods in the first place, so I'd recommend that you don't do this
unless you have a really good reason to.

This works in C# and VB.NET, but I don't know about managed C++.


Thanks!

This is exactly what I want to do but it does not seem to be working in
C++. I can't seem to find EditorBrowsableAttribute in the C++
documentation. Is it a part of any namespace?
 
Hi Bruce,

System.ComponentModel.EditorBrowsableAttribute

EditorBrowsableAttribute Class
http://msdn2.microsoft.com/en-gb/library/system.componentmodel.editorbrowsableattribute.aspx

A quick search on MSDN (above), Reflector or the Visual Studio Object
Browser could have shown you that :)

--
Dave Sexton
http://davesexton.com/blog

Bruce said:
Dave said:
Hi Bruce,

Actually, you can hide any member from the IDE and IntelliSense by
applying EditorBrowsableAttribute to its override or "new"
implementation:

[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object obj)
{
return base.Equals(obj);
}

[EditorBrowsable(EditorBrowsableState.Never)]
public static new bool ReferenceEquals(object objA, object objB)
{
return object.ReferenceEquals(objA, objB);
}

The IDE ignores EditorBrowsableAttribute from within the same project so
it only has an affect on referenced projects. Also, this doesn't prevent
consumption of the attributed members, only their presence in
IntelliSense. That means that you should still provide an implementation
(as I've done above) so that callers can use the methods if they choose.
i.e., Even with the Equals method attributed as above callers can still
write:

bool equals = yourObjInst.Equals(anotherObj);

although they won't get any editor support for calling the method.

To be perfectly honest though, I can't see why you would want to hide
either of these methods in the first place, so I'd recommend that you
don't do this unless you have a really good reason to.

This works in C# and VB.NET, but I don't know about managed C++.


Thanks!

This is exactly what I want to do but it does not seem to be working in
C++. I can't seem to find EditorBrowsableAttribute in the C++
documentation. Is it a part of any namespace?
 
"Bruce" <[email protected]> a écrit dans le message de (e-mail address removed)...

| Thanks!
|
| This is exactly what I want to do but it does not seem to be working in
| C++.

Can I just reinforce that this trick only hides the methods in the
Intellisense popup in the editor. It does *not* stop a developer from
calling either method or overriding them in their own derived classes.

Please satisfy my curiosity and tell me what purpose you think you will
achieve by doing this ? Or is it that you think developers will not know
about the eveilable methods listed in the help ?

Joanna
 
Hi Joanna,

I'd just like to mention that I've used this technique before when designing
custom controls. It's useful to hide properties and methods that only throw
NotSupportedException. However, I've never used it to hide Equals or
ReferenceEquals. Interestingly enough, this request has come up in the
newsgroups before and I've given the same answer with the same caution that
you've stated here.

Again, if Bruce has a good reason to hide these well-known members, then I
see no reason why he shouldn't. It's not harming anything to hide them, I
just question its usefulness ;)
 
Bruce said:
I posted this on the dotnet.languages.vc group but did not get a
response. I figure that I could apply what I learn from CSharp to VC,
so here goes.

I have a class in a class assembly, that just has a bunch of static
methods. How can I hide the Equals and ReferenceEquals properties
from my end user programmer?

Can you declare a static class instead ?? The .Net framework 2.0
supports the static class, and if all you need is static methods, it may
make more sense that you simply declare a static class.
 
Back
Top