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
13 3827
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?
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
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
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 - public enum Apertures : uint
- 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;
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.
Must be something new in later versions of .NET.
My enum classes in .net2.0 have ToString() functions. -
public enum fakeenum
-
{
-
bill, george, dan
-
}
-
-
fakeenum ddd = fakeenum.bill;
-
string s=ddd.ToString();
-
Look closely at the enum call. - 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.
T, I just tried it and I get ToString on a uint enum... - 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 }
-
}
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
Ok. That is too weird.
Here is the actual enum that does NOT allow the ToString function as pictured above. - 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
-
}
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
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... - // NOT VALID
-
Aperatures.ToString();
-
-
// VALID
-
Aperatures a = Aperatures.Av_1p0;
-
a.ToString();
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? -
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.
-
}
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.
Hmm... I hadn't considered that. I do have ReSharper.
Yes, the enum class itself does not have .ToString(), which is the same bahvior a class has. -
public class fred
-
{
-
//..
-
}
-
You cannot go: fred.ToString(), however an instanciated instance of fred will have a .ToString()
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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....
|
by: AWHK |
last post by:
How can I force anyone who subclasses my class to override the ToString()
method?
Andreas :-)
|
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...
|
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...
|
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...
|
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;
...
|
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 =...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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)...
|
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....
|
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
|
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...
|
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...
| |