Xojo Conferences
XDCMay2019MiamiUSA

Typecasting not working? (Real Studio network user group Mailinglist archive)

Back to the thread list
Previous thread: Contextual Menu in ToolBar?
Next thread: TempSaveFile sprawl - any way to clean up ?


FW: Special Valentine's Offer for Valentina - Beat the Price Increase!   -   Lynn Fredricks
  Typecasting not working?   -   William Squires
   Re: Typecasting not working?   -   joe strout.net
    Re: Typecasting not working?   -   William Squires
     Re: Typecasting not working?   -   joe strout.net

Typecasting not working?
Date: 05.08.07 02:02 (Sat, 4 Aug 2007 20:02:59 -0500)
From: William Squires
Hi!
I'll try to make this simple as I can...
Three classes, Thing, Wall, and Door. Thing is the base class,
Wall inherits from Thing, and Door inherits from Wall.
All three define a public method, FromCSV(s As String), and a
protected method, FromCSV2(params() As String,ns As Integer).
The FromCSV() methods (with the exception of the Thing::FromCSV()
method) have the following structure:

Sub FromCSV(s As String)
Dim ns As Integer,np As Integer
Dim params(-1) As String
Dim o As <classname> // here <classname> matches the name of the
class we're in.

ns = Super.NumParams() // number of CSV fields the immediate
superclass defines
np = k<classname>Params // a call to a method in a module
disguised as a constant.
// returns number of fields used only by
the class this method is in.
Redim params(ns + np)
If (Trim(Uppercase(params(1))) =.e.ClassName) Then
// We're deserializing the proper type of data.
Super.FromCSV s

o = <classname>(Me) // I'm trying to cast o as a <classname>
reference here... :(
o.FromCSV2 params, ns
End If
End Sub

The Thing::FromCSV() method differs in that; 1 it only utilizes
the 'np' variable and, 2 it doesn't call Super.FromCSV().
Each FromCSV2() method is specific to the particular class it's
in, and is supposed to extract just those fields from the string that
the current class is interested in.
Unfortunately, it doesn't work as the line

o.FromCSV2 params, ns

still calls the FromCSV2() method of the class whose FromCSV()
method was called; so if I have (somewhere else)

Dim d As Door
Dim f As FolderItem
Dim i As TextInputStream
Dim theLine As String

// set up f,i on a test file, "Test.dat" which contain serialized
Thing, Wall, and Door objects.
theLine = i.ReadLine()
d = New Door()
d.FromCSV theLine

then the Door::FromCSV2() method gets called when I get down to
Thing::FromCSV() in the chain of calls to Super.FromCSV()! :(
This is driving me mad!! How can I get it to call Thing::FromCSV2
() within the Thing::FromCSV() method instead of the Door::FromCSV2()
method?!?
Even though I make a variable 'o' of type <reference-to-Thing> and
typecast Me to a Thing when assigning it to 'o', it still calls the
bloody Door::FromCSV2() method - how can I get it to call
Thing::FromCSV2() in this case!?!
Using RB 5.2.4 Win. And, no, I can't afford to upgrade to RB2007
(Win) at this time.

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Typecasting not working?
Date: 05.08.07 02:27 (Sat, 4 Aug 2007 19:27:09 -0600)
From: joe strout.net
On Aug 05, 2007, at 01:02 UTC, William Squires wrote:

> I'll try to make this simple as I can...

I found myself wishing you'd tried harder. :) But if I understand you
correctly, you've discovered polymorphism: it doesn't matter what the
static type of an object reference is; the method actually invoked will
be the one that belongs to the actual class of the object at runtime.
Typecasting merely changes the static type of the reference. You
expected this to cause it to invoke the superclass method, but that's
just not how it works.

I didn't follow your example well enough to glean exactly why you'd
want to be overriding the overriding of the method, as it were. In
general, though, that smells like a design problem -- it may indicate a
need to take a step back and rethink your approach.

Or, if you're only trying to invoke the superclass method from the
subclass code, you can do this by prefixing the method name with the
class name (e.g. Thing.Foo instead of just Foo).

Best,
- Joe

Re: Typecasting not working?
Date: 06.08.07 00:25 (Sun, 5 Aug 2007 18:25:22 -0500)
From: William Squires
Isn't that they syntax for calling a class (shared) method?
(introduced sometime after 5.5 IIRC) Unfortunately, I can't try it
right now as it's at work... :(

On Aug 4, 2007, at 8:27 PM, <email address removed> wrote:

> On Aug 05, 2007, at 01:02 UTC, William Squires wrote:
>
>> I'll try to make this simple as I can...
>
> I found myself wishing you'd tried harder. :) But if I understand
> you
> correctly, you've discovered polymorphism: it doesn't matter what the
> static type of an object reference is; the method actually invoked
> will
> be the one that belongs to the actual class of the object at runtime.
> Typecasting merely changes the static type of the reference. You
> expected this to cause it to invoke the superclass method, but that's
> just not how it works.
>
> I didn't follow your example well enough to glean exactly why you'd
> want to be overriding the overriding of the method, as it were. In
> general, though, that smells like a design problem -- it may
> indicate a
> need to take a step back and rethink your approach.
>
> Or, if you're only trying to invoke the superclass method from the
> subclass code, you can do this by prefixing the method name with the
> class name (e.g. Thing.Foo instead of just Foo).
>
> Best,
> - Joe
>
> --
> Joe Strout -- <email address removed>
> Strout Custom Solutions, LLC
>
> _______________________________________________
> Unsubscribe or switch delivery mode:
> <http://www.realsoftware.com/support/listmanager/>
> Search the archives:
> <http://support.realsoftware.com/listarchives/lists.html>

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Typecasting not working?
Date: 06.08.07 16:21 (Mon, 6 Aug 2007 09:21:03 -0600)
From: joe strout.net
On Aug 05, 2007, at 23:25 UTC, William Squires wrote:

> Isn't that they syntax for calling a class (shared) method?

Yes. It also works from a subclass to invoke a superclass instance
method.

Best,
- Joe