Extending intrinsic classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • A Traveler

    Extending intrinsic classes

    Hello all,

    i was just curious if anyone whos been playing with VS2005 could tell me...

    In javascript (and java??) you can alter the prototypes for an object in
    your project. I dont remember the syntax exactly, but basically you do
    something like:

    function String.prototyp e.mySplit(myArg s){...do something...}

    and then any instance of a String object in that project has a new method
    called "mySplit".
    This isnt something by some chance that we will be able to do with intrinsic
    objects in the framework in VS2005, is it?

    I know it can be done by inheriting the intrinsic and extending it with a
    new class in my project, but thats a lot heavier and more convoluted.

    Cheers,
    - Arthur Dent.


  • John Saunders

    #2
    Re: Extending intrinsic classes

    "A Traveler" <hitchhikersgui deto-news@yahoo.com> wrote in message
    news:OjPKTV3tEH A.2596@TK2MSFTN GP15.phx.gbl...[color=blue]
    > Hello all,
    >
    > i was just curious if anyone whos been playing with VS2005 could tell
    > me...
    >
    > In javascript (and java??) you can alter the prototypes for an object in
    > your project. I dont remember the syntax exactly, but basically you do
    > something like:
    >
    > function String.prototyp e.mySplit(myArg s){...do something...}
    >
    > and then any instance of a String object in that project has a new method
    > called "mySplit".
    > This isnt something by some chance that we will be able to do with
    > intrinsic objects in the framework in VS2005, is it?
    >
    > I know it can be done by inheriting the intrinsic and extending it with a
    > new class in my project, but thats a lot heavier and more convoluted.[/color]

    Most people would disagree and say that the prototype mechanism used in
    JavaScript (but not Java) is the convoluted method they used of avoiding the
    "normal" OO technique of inheritance.

    ----
    John Saunders

    BTW, you are probably aware that there is no relationship between Java and
    JavaScript (nee LiveScript) except for the name, which sounded "cool", so
    they changed it.


    Comment

    • steve

      #3
      Re: Extending intrinsic classes

      | I know it can be done by inheriting the intrinsic and extending it with a
      | new class in my project, but thats a lot heavier and more convoluted.

      actually, the former is a lot heavier since *all* string objects will now
      carry the prototype extentions rather that just the one's you declare as the
      new extended type class that inherits from string. as far as convolution,
      again the former looses out. ever try and track down which js file actually
      contains the prototype definition when there are several js files
      included/referenced in a web page? add to that, prototypes of your prototype
      of the original string object (spread among multiple files)...or a prototype
      of a prototype of a prototype of the original string object (spread among
      multiple files)...ad nausium.

      i don't know why you'd want to do this in vb.net and don't think that you
      can. it would be a maintainence nightmare.


      Comment

      • A Traveler

        #4
        Re: Extending intrinsic classes

        > ever try and track down which js file actually[color=blue]
        > contains the prototype definition when there are several js files[/color]

        True, anything can be misused, and im not saying its something to go crazy
        with.
        But sometimes it could be useful. For example. Something i do Quite
        frequently with
        my strings is a port of Oralce's NVL function, but for strings so
        essentially it pseudo-codes as:

        If strVal = "" Then Return NullValue Else Return strVal

        This is something i do very frequently in my code, and it would be nice
        instead of having to make a
        new class (or module), and writing this function, then having to imports the
        module when i need it,
        to just extend the String object for the project scope to have a new method
        NVL(NullValue as String)

        But, its not a big deal. I was just curious.

        Thanks.

        "steve" <a@b.com> wrote in message
        news:10nfgvkatp 0tpdd@corp.supe rnews.com...[color=blue]
        >| I know it can be done by inheriting the intrinsic and extending it with a
        > | new class in my project, but thats a lot heavier and more convoluted.
        >
        > actually, the former is a lot heavier since *all* string objects will now
        > carry the prototype extentions rather that just the one's you declare as
        > the
        > new extended type class that inherits from string. as far as convolution,
        > again the former looses out. ever try and track down which js file
        > actually
        > contains the prototype definition when there are several js files
        > included/referenced in a web page? add to that, prototypes of your
        > prototype
        > of the original string object (spread among multiple files)...or a
        > prototype
        > of a prototype of a prototype of the original string object (spread among
        > multiple files)...ad nausium.
        >
        > i don't know why you'd want to do this in vb.net and don't think that you
        > can. it would be a maintainence nightmare.
        >
        >[/color]


        Comment

        • steve

          #5
          Re: Extending intrinsic classes

          | If strVal = "" Then Return NullValue Else Return strVal
          |
          | This is something i do very frequently in my code, and it would be nice
          | instead of having to make a
          | new class (or module), and writing this function, then having to imports
          the
          | module when i need it,
          | to just extend the String object for the project scope to have a new
          method
          | NVL(NullValue as String)

          this is a big deal when you have multiple developers on a project expecting
          strings to behave like strings but now have to track down why they just set
          strVal = "" and the next line calls to get strVal and it returns something
          other than "". this is why there are well defined oop standards that most
          languages follow...even php. all objects should build off of a base
          definition where the new derivative must be referenced to get the new
          functionality. "" is not nothing/null anyway...and, what happens when the
          NullValue is itself "" (watch your processor go crazy on that one)! if you
          could prototype your string object in this manner, then setting a string to
          "" would cause your nvl function to validate and reset the variable to a nvl
          of "" (were that the NullValue)...th is would start the validate/reset loop
          again...and again...and again...those are the types of issues you get when
          considering the alteration of an object by javascript methodology where the
          actual base class is altered and not a derivative thereof.

          hth,

          steve


          Comment

          • Jay B. Harlow [MVP - Outlook]

            #6
            Re: Extending intrinsic classes

            Arthur,
            Prototypes as found in javascript (but not java) are not in VB.NET 2005, nor
            C# 2005.
            [color=blue]
            > I know it can be done by inheriting the intrinsic and extending it with a
            > new class in my project, but thats a lot heavier and more convoluted.[/color]
            You do realize that all the "intrinsic" types are either NotInheritable
            (String) or value types (which are inherently NotInheritable) so this is not
            an other either.

            Hope this helps
            Jay

            "A Traveler" <hitchhikersgui deto-news@yahoo.com> wrote in message
            news:OjPKTV3tEH A.2596@TK2MSFTN GP15.phx.gbl...[color=blue]
            > Hello all,
            >
            > i was just curious if anyone whos been playing with VS2005 could tell
            > me...
            >
            > In javascript (and java??) you can alter the prototypes for an object in
            > your project. I dont remember the syntax exactly, but basically you do
            > something like:
            >
            > function String.prototyp e.mySplit(myArg s){...do something...}
            >
            > and then any instance of a String object in that project has a new method
            > called "mySplit".
            > This isnt something by some chance that we will be able to do with
            > intrinsic objects in the framework in VS2005, is it?
            >
            > I know it can be done by inheriting the intrinsic and extending it with a
            > new class in my project, but thats a lot heavier and more convoluted.
            >
            > Cheers,
            > - Arthur Dent.
            >[/color]


            Comment

            • A Traveler

              #7
              Re: Extending intrinsic classes

              Nah, you misunderstood my code. It wouldnt be to reset the value of the
              object if it went 'null'. It would more likely be a method or property,
              something like

              Class String
              ...........
              Public ReadOnly Property NVL(NullValue As String) As String
              Get
              If Me.Value = "" Then Return NullValue Else Return Me.Value
              End Get
              End Function
              ...........
              End Class

              But since its not possible, its not really an issue anyway.
              Thanks. :)

              "steve" <a@b.com> wrote in message
              news:10nfk9dkhj mm345@corp.supe rnews.com...[color=blue]
              >| If strVal = "" Then Return NullValue Else Return strVal
              > |
              > | This is something i do very frequently in my code, and it would be nice
              > | instead of having to make a
              > | new class (or module), and writing this function, then having to imports
              > the
              > | module when i need it,
              > | to just extend the String object for the project scope to have a new
              > method
              > | NVL(NullValue as String)
              >
              > this is a big deal when you have multiple developers on a project
              > expecting
              > strings to behave like strings but now have to track down why they just
              > set
              > strVal = "" and the next line calls to get strVal and it returns something
              > other than "". this is why there are well defined oop standards that most
              > languages follow...even php. all objects should build off of a base
              > definition where the new derivative must be referenced to get the new
              > functionality. "" is not nothing/null anyway...and, what happens when the
              > NullValue is itself "" (watch your processor go crazy on that one)! if you
              > could prototype your string object in this manner, then setting a string
              > to
              > "" would cause your nvl function to validate and reset the variable to a
              > nvl
              > of "" (were that the NullValue)...th is would start the validate/reset loop
              > again...and again...and again...those are the types of issues you get when
              > considering the alteration of an object by javascript methodology where
              > the
              > actual base class is altered and not a derivative thereof.
              >
              > hth,
              >
              > steve
              >
              >[/color]


              Comment

              • steve

                #8
                Re: Extending intrinsic classes

                gotcha.

                good luck.


                Comment

                Working...