Adding a function to the String class?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rob Meade

    Adding a function to the String class?

    Hi all,

    I have written a small ProperCase function which I would like to make
    available to our team at work through our common class library.

    A colleague mentioned that I could write a new class which derived from the
    String class, add the function there and then we would all use the new class
    which contained my function and all of the others etc....sounded relatively
    straight forward...

    My problem is that I figured immediately after creating a new class I'd need
    a line that Inherits...howe ver, having check the documentation for the
    String class, when I try adding:

    Inherits System.String

    I just a wiggly line in Visual Studio....If I add it with the Imports
    statement it works fine...

    Can anyone suggest what I might be doing wrong please...

    My aim is to be able to do something like this:

    Dim myString as String

    myString = "HELLO WORLD"

    myString = myString.ToProp erCase()

    Response.Write myString


    The end result will appear as

    Hello World


    Please note I've done the function, just need to tie it into something...any
    help appreciated..

    Regards

    Rob


  • Tarun Upadhyaya

    #2
    RE: Adding a function to the String class?

    Hi,

    What i understand is that you dont want to use import statement and still
    wish that ur string class should work. (May be i am wrong)

    With respect to what i Understand,I think you should create a seperate class
    library containing your string class and try adding your assembly in GAC and
    then give it a try.

    Hope that helps

    "Rob Meade" wrote:
    [color=blue]
    > Hi all,
    >
    > I have written a small ProperCase function which I would like to make
    > available to our team at work through our common class library.
    >
    > A colleague mentioned that I could write a new class which derived from the
    > String class, add the function there and then we would all use the new class
    > which contained my function and all of the others etc....sounded relatively
    > straight forward...
    >
    > My problem is that I figured immediately after creating a new class I'd need
    > a line that Inherits...howe ver, having check the documentation for the
    > String class, when I try adding:
    >
    > Inherits System.String
    >
    > I just a wiggly line in Visual Studio....If I add it with the Imports
    > statement it works fine...
    >
    > Can anyone suggest what I might be doing wrong please...
    >
    > My aim is to be able to do something like this:
    >
    > Dim myString as String
    >
    > myString = "HELLO WORLD"
    >
    > myString = myString.ToProp erCase()
    >
    > Response.Write myString
    >
    >
    > The end result will appear as
    >
    > Hello World
    >
    >
    > Please note I've done the function, just need to tie it into something...any
    > help appreciated..
    >
    > Regards
    >
    > Rob
    >
    >
    >[/color]

    Comment

    • Eliyahu Goldin

      #3
      Re: Adding a function to the String class?

      Rob,

      The very idea doesn't sound right to me. String is probably the most common
      class in dotnet. It is used in all sorts of scenarios all places around. To
      replace is with another class just for the sake of one little method??? How
      are you going to enforce using your new class by everyone in all cases?
      Also, you will have to care about type casting. May be you should consider
      having a simple utility class with static method ProperCase at apply it as
      needed?

      Eliyahu

      "Rob Meade" <robert.meade@N OSPAMubht.swest .nhs.uk> wrote in message
      news:e7mRj5ODFH A.3540@TK2MSFTN GP14.phx.gbl...[color=blue]
      > Hi all,
      >
      > I have written a small ProperCase function which I would like to make
      > available to our team at work through our common class library.
      >
      > A colleague mentioned that I could write a new class which derived from[/color]
      the[color=blue]
      > String class, add the function there and then we would all use the new[/color]
      class[color=blue]
      > which contained my function and all of the others etc....sounded[/color]
      relatively[color=blue]
      > straight forward...
      >
      > My problem is that I figured immediately after creating a new class I'd[/color]
      need[color=blue]
      > a line that Inherits...howe ver, having check the documentation for the
      > String class, when I try adding:
      >
      > Inherits System.String
      >
      > I just a wiggly line in Visual Studio....If I add it with the Imports
      > statement it works fine...
      >
      > Can anyone suggest what I might be doing wrong please...
      >
      > My aim is to be able to do something like this:
      >
      > Dim myString as String
      >
      > myString = "HELLO WORLD"
      >
      > myString = myString.ToProp erCase()
      >
      > Response.Write myString
      >
      >
      > The end result will appear as
      >
      > Hello World
      >
      >
      > Please note I've done the function, just need to tie it into[/color]
      something...any[color=blue]
      > help appreciated..
      >
      > Regards
      >
      > Rob
      >
      >[/color]


      Comment

      • Rob Meade

        #4
        Re: Adding a function to the String class?

        "Tarun Upadhyaya" wrote ...
        [color=blue]
        > With respect to what i Understand,I think you should create a seperate
        > class
        > library containing your string class and try adding your assembly in GAC
        > and
        > then give it a try.[/color]

        We have a seperate class library already, which already has various classes
        and common functionality in it - we all incorporate this into each of our
        applications. It is also already in the GAC, my question is simply how I
        get a new class which includes all of the existing functionality from the
        String class, and adds my own ProperCase method.

        Regards

        Rob


        Comment

        • Rob Meade

          #5
          Re: Adding a function to the String class?

          "Eliyahu Goldin" wrote ...
          [color=blue]
          > The very idea doesn't sound right to me.[/color]

          ok
          [color=blue]
          > String is probably the most common class in dotnet. It is used in all
          > sorts of scenarios all places around. To
          > replace is with another class just for the sake of one little method???[/color]

          I will admit I hadn't given any thought to the size of the existing class.
          [color=blue]
          > How are you going to enforce using your new class by everyone in all
          > cases?[/color]

          We have a common class library which we all use (only 7 of us here in the
          team), we use this on each project, so by adding my new string class to it
          we'd all have access to it.
          [color=blue]
          > Also, you will have to care about type casting.[/color]

          I dont know what that means?
          [color=blue]
          > May be you should consider having a simple utility class with static
          > method ProperCase at apply it as
          > needed?[/color]

          Based on what you've suggested above about the string class that might well
          be the way we have to go...I was thinking mainly from a costmetic
          perspective when I asked the question, I was hoping that my colleagues would
          be able to use it like:

          myString = myString.ToProp erCase

          Instead of

          myString = ProperCase(mySt ring)

          etc

          Regards

          Rob


          Comment

          • Tarun Upadhyaya

            #6
            Re: Adding a function to the String class?

            Use inheritance.

            "Rob Meade" wrote:
            [color=blue]
            > "Tarun Upadhyaya" wrote ...
            >[color=green]
            > > With respect to what i Understand,I think you should create a seperate
            > > class
            > > library containing your string class and try adding your assembly in GAC
            > > and
            > > then give it a try.[/color]
            >
            > We have a seperate class library already, which already has various classes
            > and common functionality in it - we all incorporate this into each of our
            > applications. It is also already in the GAC, my question is simply how I
            > get a new class which includes all of the existing functionality from the
            > String class, and adds my own ProperCase method.
            >
            > Regards
            >
            > Rob
            >
            >
            >[/color]

            Comment

            • Eliyahu Goldin

              #7
              Re: Adding a function to the String class?

              Rob,

              Class String is sealed. This means you can't derive from it.

              Eliyahu

              "Rob Meade" <robert.meade@N OSPAMubht.swest .nhs.uk> wrote in message
              news:e7mRj5ODFH A.3540@TK2MSFTN GP14.phx.gbl...[color=blue]
              > Hi all,
              >
              > I have written a small ProperCase function which I would like to make
              > available to our team at work through our common class library.
              >
              > A colleague mentioned that I could write a new class which derived from[/color]
              the[color=blue]
              > String class, add the function there and then we would all use the new[/color]
              class[color=blue]
              > which contained my function and all of the others etc....sounded[/color]
              relatively[color=blue]
              > straight forward...
              >
              > My problem is that I figured immediately after creating a new class I'd[/color]
              need[color=blue]
              > a line that Inherits...howe ver, having check the documentation for the
              > String class, when I try adding:
              >
              > Inherits System.String
              >
              > I just a wiggly line in Visual Studio....If I add it with the Imports
              > statement it works fine...
              >
              > Can anyone suggest what I might be doing wrong please...
              >
              > My aim is to be able to do something like this:
              >
              > Dim myString as String
              >
              > myString = "HELLO WORLD"
              >
              > myString = myString.ToProp erCase()
              >
              > Response.Write myString
              >
              >
              > The end result will appear as
              >
              > Hello World
              >
              >
              > Please note I've done the function, just need to tie it into[/color]
              something...any[color=blue]
              > help appreciated..
              >
              > Regards
              >
              > Rob
              >
              >[/color]


              Comment

              • Scott Simons

                #8
                RE: Adding a function to the String class?

                Did you know that there already is a function in the framework to convert to
                title case? It's ToTitleCase(), I'll leave it up to you to find the
                namespace that it's in.

                Comment

                • Rob Meade

                  #9
                  Re: Adding a function to the String class?

                  "Tarun Upadhyaya" wrote ...
                  [color=blue]
                  > Use inheritance.[/color]

                  You cant - the String class is not inheritable..

                  Regards

                  Rob


                  Comment

                  • Rob Meade

                    #10
                    Re: Adding a function to the String class?

                    "Eliyahu Goldin" wrote ...
                    [color=blue]
                    > Class String is sealed. This means you can't derive from it.[/color]

                    Hi Eliyahu,

                    Yeah, tis a shame :o/ I guess there's a good reason why - be nice to know
                    what that was - lol - oh well...

                    Cheers

                    Rob


                    Comment

                    • bruce barker

                      #11
                      Re: Adding a function to the String class?

                      because the string class is sealed (no inheritance allowed - for
                      performance), the best you can do is a class of static (public shared in vb)
                      functions (like vb does with left, right, mid) of your new routines:

                      -- bruce (sqlwork.com)


                      "Rob Meade" <robert.meade@N OSPAMubht.swest .nhs.uk> wrote in message
                      news:e7mRj5ODFH A.3540@TK2MSFTN GP14.phx.gbl...
                      | Hi all,
                      |
                      | I have written a small ProperCase function which I would like to make
                      | available to our team at work through our common class library.
                      |
                      | A colleague mentioned that I could write a new class which derived from
                      the
                      | String class, add the function there and then we would all use the new
                      class
                      | which contained my function and all of the others etc....sounded
                      relatively
                      | straight forward...
                      |
                      | My problem is that I figured immediately after creating a new class I'd
                      need
                      | a line that Inherits...howe ver, having check the documentation for the
                      | String class, when I try adding:
                      |
                      | Inherits System.String
                      |
                      | I just a wiggly line in Visual Studio....If I add it with the Imports
                      | statement it works fine...
                      |
                      | Can anyone suggest what I might be doing wrong please...
                      |
                      | My aim is to be able to do something like this:
                      |
                      | Dim myString as String
                      |
                      | myString = "HELLO WORLD"
                      |
                      | myString = myString.ToProp erCase()
                      |
                      | Response.Write myString
                      |
                      |
                      | The end result will appear as
                      |
                      | Hello World
                      |
                      |
                      | Please note I've done the function, just need to tie it into
                      something...any
                      | help appreciated..
                      |
                      | Regards
                      |
                      | Rob
                      |
                      |


                      Comment

                      • Tarun Upadhyaya

                        #12
                        Re: Adding a function to the String class?

                        Hi,
                        I am sorry, I forgot that String class is not inheritable.
                        In such a case you may try the following.

                        1. Create a new class with static methods of your requirement
                        2. Take time and effort to create your own version of string class with same
                        function names and use the built in functions inside it. (Its tedious , I
                        say) plus your own functions like ProperCase.

                        Hope that helps

                        "Rob Meade" wrote:
                        [color=blue]
                        > "Tarun Upadhyaya" wrote ...
                        >[color=green]
                        > > Use inheritance.[/color]
                        >
                        > You cant - the String class is not inheritable..
                        >
                        > Regards
                        >
                        > Rob
                        >
                        >
                        >[/color]

                        Comment

                        • Eliyahu Goldin

                          #13
                          Re: Adding a function to the String class?

                          Rob,

                          In some cases classes are sealed for performance reasons. Since String is a
                          very common class, it makes sense to optimize it's performance. Not sure if
                          it is the real reason in the case though.

                          Eliyahu

                          "Rob Meade" <robb.meade@N O-SPAM.kingswoodw eb.net> wrote in message
                          news:MMONd.9495 $8B3.9433@text. news.blueyonder .co.uk...[color=blue]
                          > "Eliyahu Goldin" wrote ...
                          >[color=green]
                          > > Class String is sealed. This means you can't derive from it.[/color]
                          >
                          > Hi Eliyahu,
                          >
                          > Yeah, tis a shame :o/ I guess there's a good reason why - be nice to[/color]
                          know[color=blue]
                          > what that was - lol - oh well...
                          >
                          > Cheers
                          >
                          > Rob
                          >
                          >[/color]


                          Comment

                          • Rob Meade

                            #14
                            Re: Adding a function to the String class?

                            "Scott Simons" wrote ...
                            [color=blue]
                            > Did you know that there already is a function in the framework to convert
                            > to
                            > title case? It's ToTitleCase(), I'll leave it up to you to find the
                            > namespace that it's in.[/color]

                            OMG! Hello Scott,

                            Thanks for the info - I've just tried this, seems a bit overly complex to
                            use, but the results are quite good...

                            The thing with mine was that it contains a set of ignore words, because this
                            is going to be used in a medical profession I have set a series of common
                            words/abbreviations that should always appear a certain way - ToTitleCase
                            came a little unstuck on some of these, although NHS was ok...NPfIT for
                            example was not..

                            Thanks for the info though.

                            Regards

                            Rob


                            Comment

                            Working...