473,395 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Hiding Equals and ReferenceEquals?

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 E. Stemplewski
GarXface OCX and C++ Class Library for the Garmin GPS
www.stempsoft.com
Jan 1 '07 #1
10 4170
"Bruce" <Br***@nospam.coma écrit dans le message de news:
OO**************@TK2MSFTNGP06.phx.gbl...

| 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 Carter [TeamB]
Consultant Software Engineer
Jan 1 '07 #2
Joanna Carter [TeamB] wrote:
"Bruce" <Br***@nospam.coma écrit dans le message de news:
OO**************@TK2MSFTNGP06.phx.gbl...

| 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 E. Stemplewski
GarXface OCX and C++ Class Library for the Garmin GPS
www.stempsoft.com
Jan 1 '07 #3
"Bruce" <Br***@nospam.coma écrit dans le message de news:
%2***************@TK2MSFTNGP04.phx.gbl...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 2 '07 #4
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 Sexton
http://davesexton.com/blog

"Bruce" <Br***@nospam.comwrote in message
news:%2***************@TK2MSFTNGP04.phx.gbl...
Joanna Carter [TeamB] wrote:
>"Bruce" <Br***@nospam.coma écrit dans le message de news:
OO**************@TK2MSFTNGP06.phx.gbl...

| 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 E. Stemplewski
GarXface OCX and C++ Class Library for the Garmin GPS
www.stempsoft.com

Jan 2 '07 #5
Dave Sexton wrote:
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 E. Stemplewski
GarXface OCX and C++ Class Library for the Garmin GPS
www.stempsoft.com
Jan 2 '07 #6
Hi Bruce,

System.ComponentModel.EditorBrowsableAttribute

EditorBrowsableAttribute Class
http://msdn2.microsoft.com/en-gb/lib...attribute.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" <Br***@nospam.comwrote in message
news:Oq**************@TK2MSFTNGP06.phx.gbl...
Dave Sexton wrote:
>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 E. Stemplewski
GarXface OCX and C++ Class Library for the Garmin GPS
www.stempsoft.com

Jan 2 '07 #7
"Bruce" <Br***@nospam.coma écrit dans le message de news:
Oq**************@TK2MSFTNGP06.phx.gbl...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 2 '07 #8
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 ;)

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

"Joanna Carter [TeamB]" <jo****@not.for.spamwrote in message
news:uZ**************@TK2MSFTNGP03.phx.gbl...
"Bruce" <Br***@nospam.coma écrit dans le message de news:
Oq**************@TK2MSFTNGP06.phx.gbl...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer


Jan 2 '07 #9
Bruce wrote:
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.
Jan 2 '07 #10
Dave Sexton wrote:
Hi Bruce,

System.ComponentModel.EditorBrowsableAttribute

EditorBrowsableAttribute Class
http://msdn2.microsoft.com/en-gb/lib...attribute.aspx

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

I did search the msdn help file on my PC. It was not in there.

I need to learn to use the Browser instead of relying on help.

--
Bruce E. Stemplewski
GarXface OCX and C++ Class Library for the Garmin GPS
www.stempsoft.com
Jan 3 '07 #11

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

Similar topics

1
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object.cs in rotor. What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What I don't...
2
by: emma middlebrook | last post by:
Hi Having difficulty getting myself clear on how a type's operator== fits in with Object.Equals. Let's just consider reference types. The default operator== tests for object identity...
2
by: Kevin B Ebert | last post by:
Today, I ran into something I didn't quite expect. I'm sure there is a logical explanation for it so I want to post it and see if anyone can explain the difference to me. I came across a...
17
by: Zeng | last post by:
I'm trying to comparing 2 objects (pointer to object) to see if they are the "same" as each other. Here is what the definition of being the "same" object type for both objects, object 1, ...
3
by: Fei Li | last post by:
Hi, take string class as an example, who can explain the difference? Thanks
6
by: Joey Callisay | last post by:
Is there a difference between Object.ReferenceEquals() method and the == operator comparing two reference types?
8
by: Kenneth Baltrinic | last post by:
When one overrides the Equals() method of an object, one is supposed to override GetHashCode() as well and this makes good sense. But I have seen lots of people who do this and do not override the...
12
by: cody | last post by:
Why can I overload operator== and operator!= separately having different implementations and additionally I can override equals() also having a different implementation. Why not forbid...
0
by: Bruce | last post by:
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 E. Stemplewski...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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
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...

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.