Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. New Methods for existing types?

New Methods for existing types?

Scheduled Pinned Locked Moved C#
question
14 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P ptr2void

    Why dont you just test this instead of wondering?? ;) ;)

    R Offline
    R Offline
    Reelix
    wrote on last edited by
    #3

    public static string add RemoveNum(string num) { return string.Replace(num, ""); } obviously doesnt work (Seeing as I made it up on the spot :p) Was googling around, and couldn't find anything particularly useful... You can't override, since it doesnt exist... I'm just wondering if there's a way TO do it :)

    -= Reelix =-

    1 Reply Last reply
    0
    • R Reelix

      I was wondering something... Is it possible do do something like:

      string s1 = "12345";
      string s2 = s1.RemoveNum("5");
      MessageBox.Show(s2);

      Where further down you add the RemoveNum() method ONTO the string type? something like...

      public static string add RemoveNum(string num)
      {
      return string.Replace(num, "");
      }

      ? Just wondering... :)

      -= Reelix =-

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #4

      OOP way to augment functionality is inheritance, but no luck this time: String class is sealed. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

      R 1 Reply Last reply
      0
      • C CPallini

        OOP way to augment functionality is inheritance, but no luck this time: String class is sealed. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

        R Offline
        R Offline
        Reelix
        wrote on last edited by
        #5

        Dang :/ No evil way to hack through it? :p

        -= Reelix =-

        C 1 Reply Last reply
        0
        • R Reelix

          Dang :/ No evil way to hack through it? :p

          -= Reelix =-

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #6

          Of course there are ways, evil, but you have to ask the devil. :-D

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

          R 1 Reply Last reply
          0
          • C CPallini

            Of course there are ways, evil, but you have to ask the devil. :-D

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

            R Offline
            R Offline
            Reelix
            wrote on last edited by
            #7

            *Decompiles the .NET Framework into Assembly* *Inserts a new String Method* *Recompiles* *Looks Innocent* *Thinks...* Dang... Will only work on my PC :laugh:

            -= Reelix =-

            1 Reply Last reply
            0
            • R Reelix

              I was wondering something... Is it possible do do something like:

              string s1 = "12345";
              string s2 = s1.RemoveNum("5");
              MessageBox.Show(s2);

              Where further down you add the RemoveNum() method ONTO the string type? something like...

              public static string add RemoveNum(string num)
              {
              return string.Replace(num, "");
              }

              ? Just wondering... :)

              -= Reelix =-

              L Offline
              L Offline
              laserbaronen
              wrote on last edited by
              #8

              check out extension methods


              betonglasermur.FeedDwarf(pur_is, 17);
              ProcessStartupInfo.AintNotCreateNoWindow = (false && !true) != (true || false) ? false == true ? true : false : (true != false && false);

              Morgonen är tröttmans mecka

              L R 2 Replies Last reply
              0
              • L laserbaronen

                check out extension methods


                betonglasermur.FeedDwarf(pur_is, 17);
                ProcessStartupInfo.AintNotCreateNoWindow = (false && !true) != (true || false) ? false == true ? true : false : (true != false && false);

                Morgonen är tröttmans mecka

                L Offline
                L Offline
                laserbaronen
                wrote on last edited by
                #9

                http://www.codeproject.com/KB/cs/Using_Extension_Methods.aspx[^]


                betonglasermur.FeedDwarf(pur_is, 17);
                ProcessStartupInfo.AintNotCreateNoWindow = (false && !true) != (true || false) ? false == true ? true : false : (true != false && false);

                Morgonen är tröttmans mecka

                1 Reply Last reply
                0
                • R Reelix

                  I was wondering something... Is it possible do do something like:

                  string s1 = "12345";
                  string s2 = s1.RemoveNum("5");
                  MessageBox.Show(s2);

                  Where further down you add the RemoveNum() method ONTO the string type? something like...

                  public static string add RemoveNum(string num)
                  {
                  return string.Replace(num, "");
                  }

                  ? Just wondering... :)

                  -= Reelix =-

                  B Offline
                  B Offline
                  buchstaben
                  wrote on last edited by
                  #10

                  there is a way, using extension methods. but this requires c# 3.0 .. Extension methods are static methods that can be invoked using instance method syntax. In effect, extension methods make it possible to extend existing types and constructed types with additional methods.

                  P 1 Reply Last reply
                  0
                  • L laserbaronen

                    check out extension methods


                    betonglasermur.FeedDwarf(pur_is, 17);
                    ProcessStartupInfo.AintNotCreateNoWindow = (false && !true) != (true || false) ? false == true ? true : false : (true != false && false);

                    Morgonen är tröttmans mecka

                    R Offline
                    R Offline
                    Reelix
                    wrote on last edited by
                    #11

                    VICTORY!!!

                    using System;

                    class String
                    {
                        public string RemoveFirst(string num)
                        {
                            //num = num.Remove(num);
                            string toReturn = num.Remove(0, 1);
                            return toReturn;
                            //Console.WriteLine("Hackz0red!");
                        }
                    }
                    
                    class ExtMethodDemo
                    {
                        static void Main(string\[\] args)
                        {
                            String r1 = new String();
                            string r2 = r1.RemoveFirst("12345");
                            Console.WriteLine(r2);
                            Console.ReadLine();
                        }
                    }
                    

                    Thanks laserbaronen!!! :D

                    -= Reelix =-

                    L 1 Reply Last reply
                    0
                    • R Reelix

                      VICTORY!!!

                      using System;

                      class String
                      {
                          public string RemoveFirst(string num)
                          {
                              //num = num.Remove(num);
                              string toReturn = num.Remove(0, 1);
                              return toReturn;
                              //Console.WriteLine("Hackz0red!");
                          }
                      }
                      
                      class ExtMethodDemo
                      {
                          static void Main(string\[\] args)
                          {
                              String r1 = new String();
                              string r2 = r1.RemoveFirst("12345");
                              Console.WriteLine(r2);
                              Console.ReadLine();
                          }
                      }
                      

                      Thanks laserbaronen!!! :D

                      -= Reelix =-

                      L Offline
                      L Offline
                      laserbaronen
                      wrote on last edited by
                      #12

                      hmm o_o

                      public static class ExtMethods
                      {
                      public static string RemoveFirst(this string str)
                      {
                      return str.Remove(0,1);
                      }
                      }
                      class ExtMethodDemo
                      {
                      static void Main(string[] args)
                      {
                      string r1 = "12345".RemoveFirst();
                      Console.WriteLine(r1);
                      Console.ReadLine();
                      }
                      }

                      was thinking something like this


                      betonglasermur.FeedDwarf(pur_is, 17);
                      ProcessStartupInfo.AintNotCreateNoWindow = (false && !true) != (true || false) ? false == true ? true : false : (true != false && false);

                      Morgonen är tröttmans mecka

                      modified on Tuesday, May 20, 2008 10:05 AM

                      1 Reply Last reply
                      0
                      • B buchstaben

                        there is a way, using extension methods. but this requires c# 3.0 .. Extension methods are static methods that can be invoked using instance method syntax. In effect, extension methods make it possible to extend existing types and constructed types with additional methods.

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #13

                        buchstaben wrote:

                        In effect, extension methods make it possible

                        make it appear possible, when in fact it isn't. :-D

                        B 1 Reply Last reply
                        0
                        • P PIEBALDconsult

                          buchstaben wrote:

                          In effect, extension methods make it possible

                          make it appear possible, when in fact it isn't. :-D

                          B Offline
                          B Offline
                          buchstaben
                          wrote on last edited by
                          #14

                          PIEBALDconsult wrote:

                          buchstaben wrote: In effect, extension methods make it possible make it appear possible, when in fact it isn't.

                          that's just what c# 3.0 specification says.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups