Extending intrinsic classes

A

A Traveler

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.prototype.mySplit(myArgs){...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.
 
J

John Saunders

A Traveler said:
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.prototype.mySplit(myArgs){...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.

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.
 
S

steve

| 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.
 
A

A Traveler

ever try and track down which js file actually
contains the prototype definition when there are several js files

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.
 
S

steve

| 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)...this 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
 
J

Jay B. Harlow [MVP - Outlook]

Arthur,
Prototypes as found in javascript (but not java) are not in VB.NET 2005, nor
C# 2005.
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.
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

A Traveler

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. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top