Can i Hide .ToString() Method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NandlalKrishnia
    New Member
    • Mar 2010
    • 1

    #1

    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
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    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?

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      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

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        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

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          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
          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;
          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.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Must be something new in later versions of .NET.
            My enum classes in .net2.0 have ToString() functions.

            [code=C#]
            public enum fakeenum
            {
            bill, george, dan
            }
            [/code]
            [code=C#]
            fakeenum ddd = fakeenum.bill;
            string s=ddd.ToString( );
            [/code]

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              Look closely at the enum call.
              Code:
              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.

              Comment

              • GaryTexmo
                Recognized Expert Top Contributor
                • Jul 2009
                • 1501

                #8
                T, I just tried it and I get ToString on a uint enum...

                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 }
                }
                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

                Comment

                • tlhintoq
                  Recognized Expert Specialist
                  • Mar 2008
                  • 3532

                  #9
                  Ok. That is too weird.
                  Here is the actual enum that does NOT allow the ToString function as pictured above.
                  Code:
                  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

                  Comment

                  • GaryTexmo
                    Recognized Expert Top Contributor
                    • Jul 2009
                    • 1501

                    #10
                    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

                    • tlhintoq
                      Recognized Expert Specialist
                      • Mar 2008
                      • 3532

                      #11
                      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?


                      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.
                      }
                      [imgnothumb]http://files.me.com/tlhintoq/owfk08[/imgnothumb]

                      Comment

                      • GaryTexmo
                        Recognized Expert Top Contributor
                        • Jul 2009
                        • 1501

                        #12
                        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.

                        Comment

                        • tlhintoq
                          Recognized Expert Specialist
                          • Mar 2008
                          • 3532

                          #13
                          Hmm... I hadn't considered that. I do have ReSharper.

                          Comment

                          • Plater
                            Recognized Expert Expert
                            • Apr 2007
                            • 7872

                            #14
                            Yes, the enum class itself does not have .ToString(), which is the same bahvior a class has.
                            [code=c#]
                            public class fred
                            {
                            //..
                            }
                            [/code]
                            You cannot go: fred.ToString() , however an instanciated instance of fred will have a .ToString()

                            Comment

                            Working...