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
Can i Hide .ToString() Method
Collapse
X
-
-
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? -
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?
-FrinnyComment
-
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
Code:public enum Apertures : uint
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.Code:Apertures Result = Apertures.Av_INVALID; Camera temp = getSelectedCamera(); if (temp == null) ; else Result = temp.Aperture; //tbAperture.Text = Result; cbApertures.SelectedItem = Enum.GetName(typeof(Apertures), Result); return Result;
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.Comment
-
T, I just tried it and I get ToString on a uint enum...
Weird.Code:namespace ConsoleApplication1 { class Program { static void Main(string[] args) { MyEnum a = MyEnum.b; Console.WriteLine(a.ToString()); } } public enum MyEnum : uint { a, b, c, d } }
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 :DComment
-
Ok. That is too weird.
Here is the actual enum that does NOT allow the ToString function as pictured above.
This is in a windows forms application of framework 2.0Code:using System; using System.Collections.Generic; using System.Text; using Tlhintoq.Controls.Cameras.Canon.EOS; namespace Tlhintoq.Controls.Cameras.Canon.EOS { public enum Apertures : uint { Av_1p0 = 0x08, Av_1p1 = 0x0b, Av_1p2 = 0x0c, Av_1p23 = 0x0d, Av_1p4 = 0x010, Av_1p6 = 0x13, Av_1p8 = 0x14, Av_1p83 = 0x15, Av_2p0 = 0x18, // goes on for a mile }
Anyone else care to see how an instance of that behaves in their applicationComment
-
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...
Code:// NOT VALID Aperatures.ToString(); // VALID Aperatures a = Aperatures.Av_1p0; a.ToString();
Comment
-
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?
[imgnothumb]http://files.me.com/tlhintoq/owfk08[/imgnothumb]Code:get { Apertures Result = Apertures.Av_INVALID; /* // GUI stuff not related to the discussion thread Camera temp = getSelectedCamera(); if (temp == null) ; else Result = temp.Aperture; cbApertures.SelectedItem = Enum.GetName(typeof(Apertures), Result); */ return Result; // Then for giggles and testing start typing this: Result. }Comment
Comment