473,327 Members | 2,016 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,327 software developers and data experts.

Can i Hide .ToString() Method

I want to create a class and when ever i create an object of that class i want that obj.ToString() will not display and cant use this method
Mar 30 '10 #1
13 3827
GaryTexmo
1,501 Expert 1GB
I'm not sure you can hide it because ToString is a virtual method. If it weren't, you could just new/override it as a private method in your class and it would be hidden, but you're apparently not allowed to do that with virtual methods.

However, you can override it to make it do nothing at all, which is part of what you want.

I did some digging around via google and there were some suggestions, but they didn't work when I tried them. The ToString method still showed up on intellisense and was still accessible. Why do you need it to be hidden?
Mar 30 '10 #2
Plater
7,872 Expert 4TB
There is some sort of keyword you can put inside [ ] brackets before a function to hide it from intelisense, not sure it will work with the basic required .ToString() function
Mar 30 '10 #3
Frinavale
9,735 Expert Mod 8TB
I was just having a conversation with someone the other day about their problems using reflection with a method that was marked as..."something"...which made the method "invisible".

As Plater said, You can indicate this using some attribute that is specified in "[]" that comes before the method....I just can't remember the name of the attribute.

Have you tried just changing the Public scope declaration to Private when you override the method?

-Frinny
Mar 30 '10 #4
tlhintoq
3,525 Expert 2GB
I noticed this the other day in a project I was working on, where the ToString was not available and lined through


In this case "Result" was a simple enum of type uint
Expand|Select|Wrap|Line Numbers
  1. public enum Apertures : uint
Expand|Select|Wrap|Line Numbers
  1. Apertures Result = Apertures.Av_INVALID;
  2.                 Camera temp = getSelectedCamera();
  3.  
  4.                 if (temp == null) ;
  5.                 else Result = temp.Aperture;
  6.                 //tbAperture.Text = Result;
  7.                 cbApertures.SelectedItem = Enum.GetName(typeof(Apertures), Result);
  8.                 return Result;
Enum does support ToString, so I think the magic happening here is that this enum specifies it is of type uint. I tried to make a class that inherits from enum, but it is sealed so you can't. But this does show it can be done. It also looks like it is somehow done by turning off the iformatProvider to a string.

I hope this helps you on your research. I am just too swamped right now to hunt this down any further... but might try to thread it in through my day as part of my 'duties' for future needs in my own project.

In the mean time, you could just override ToString so it does nothing, and provide a description saying that it does nothing.
Mar 30 '10 #5
Plater
7,872 Expert 4TB
Must be something new in later versions of .NET.
My enum classes in .net2.0 have ToString() functions.

Expand|Select|Wrap|Line Numbers
  1. public enum fakeenum
  2. {
  3.  bill, george, dan
  4. }
  5.  
Expand|Select|Wrap|Line Numbers
  1. fakeenum ddd = fakeenum.bill;
  2. string s=ddd.ToString();
  3.  
Mar 30 '10 #6
tlhintoq
3,525 Expert 2GB
Look closely at the enum call.
Expand|Select|Wrap|Line Numbers
  1. public enum Apertures : uint
It specifies the type to be of uint

Try that and tell us if you still have ToString available on your system, please.
Mar 30 '10 #7
GaryTexmo
1,501 Expert 1GB
T, I just tried it and I get ToString on a uint enum...

Expand|Select|Wrap|Line Numbers
  1. namespace ConsoleApplication1
  2. {
  3.     class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             MyEnum a = MyEnum.b;
  8.             Console.WriteLine(a.ToString());
  9.         }
  10.     }
  11.  
  12.     public enum MyEnum : uint { a, b, c, d }
  13. }
Weird.

Also, in response to Frinny, I tried a private new of the ToString method but the public base one still showed up (and was callable). Does it do something different from you?

Ooooh, coding mysteries are fun :D
Mar 30 '10 #8
tlhintoq
3,525 Expert 2GB
Ok. That is too weird.
Here is the actual enum that does NOT allow the ToString function as pictured above.
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Tlhintoq.Controls.Cameras.Canon.EOS;
  5.  
  6. namespace Tlhintoq.Controls.Cameras.Canon.EOS
  7. {
  8.     public enum Apertures : uint 
  9.     {
  10.         Av_1p0 = 0x08,
  11.         Av_1p1 = 0x0b,
  12.         Av_1p2 = 0x0c,
  13.         Av_1p23 = 0x0d,
  14.         Av_1p4 = 0x010,
  15.         Av_1p6 = 0x13,
  16.         Av_1p8 = 0x14,
  17.         Av_1p83 = 0x15,
  18.         Av_2p0 = 0x18,
  19. // goes on for a mile
  20.     }
This is in a windows forms application of framework 2.0
Anyone else care to see how an instance of that behaves in their application
Mar 30 '10 #9
GaryTexmo
1,501 Expert 1GB
Oh wait, I see the difference here. No, there is no ToString method on the enum itself, but there *is* a ToString method on an object that is that enum.

ie...
Expand|Select|Wrap|Line Numbers
  1. // NOT VALID
  2. Aperatures.ToString();
  3.  
  4. // VALID
  5. Aperatures a = Aperatures.Av_1p0;
  6. a.ToString();
Mar 30 '10 #10
tlhintoq
3,525 Expert 2GB
Sure you can type it. It will come up in the code and not throw any compile errors. But do you notice how Intellisense tries to warn you off of it?


Expand|Select|Wrap|Line Numbers
  1. get
  2. {
  3.    Apertures Result = Apertures.Av_INVALID;
  4.  
  5. /* // GUI stuff not related to the discussion thread
  6.    Camera temp = getSelectedCamera();
  7.    if (temp == null) ;
  8.    else Result = temp.Aperture;
  9.    cbApertures.SelectedItem = Enum.GetName(typeof(Apertures), Result);
  10. */
  11.    return Result;
  12.    // Then for giggles and testing start typing this:
  13.    Result.
  14. }

Mar 30 '10 #11
GaryTexmo
1,501 Expert 1GB
Actually I don't get that strikethrough at all. You must have some kind of add-in though because my intellisense doesn't look like yours in the expansion part either.

Using Visual C# 2008 Express Edition btw.
Mar 30 '10 #12
tlhintoq
3,525 Expert 2GB
Hmm... I hadn't considered that. I do have ReSharper.
Mar 30 '10 #13
Plater
7,872 Expert 4TB
Yes, the enum class itself does not have .ToString(), which is the same bahvior a class has.
Expand|Select|Wrap|Line Numbers
  1. public class fred
  2. {
  3. //..
  4. }
  5.  
You cannot go: fred.ToString(), however an instanciated instance of fred will have a .ToString()
Apr 5 '10 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: qazmlp | last post by:
I have a class containing 5-6 member data. I want to provide a toString() method as a part of this class to help the users to do tracing. const std::string& toString() { std::string objectData...
2
by: Suresh | last post by:
Hi, I need to add a custom ToString method on an Enum Property. The default ToString method expands the whole name. But, I want only an short associated code with the long name of the enum type....
7
by: AWHK | last post by:
How can I force anyone who subclasses my class to override the ToString() method? Andreas :-)
8
by: John Cobb | last post by:
Excuse me if I use some terminology incorrectly as I am not well versed in Object Orientation. In short I want to create an Enumerated type similar to the following Enum Monitor as Byte Small...
2
by: Richard Bysouth | last post by:
I'm sure this is something pretty trivial but can't seem to figure out how to do it. I'm using Generic Lists in quite a few places in my app and for some of them want to be able to create a...
5
by: Don | last post by:
I have a project wherein I derive a combobox and create a custom Populate() method for it to fill it with objects of a custom class I also created in a second project that the combobox project...
4
by: J Miro | last post by:
When I use ToString method to format the value of a nullable numeric variable, I get "No overload for method 'ToString' takes '1' arguments" error message. Example: Int32? myNum = 12345; ...
3
by: =?Utf-8?B?RGFycmVsbCBXZXNsZXk=?= | last post by:
1) Where can I find the valid set of formatting codes that can be used with the tostring method? 2) When using the following statement I get an invalid cast error --- why? X0SS =...
9
by: Hans-Jürgen Philippi | last post by:
Hi group, let's say I have a 'Person' class with properties like 'FirstName', 'LastName', 'Birthday' and so on. Now I overload the 'Person' ToString() method with an implementation...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.