realbasic-nug
[Top] [All Lists]

Re: Instantiating object (Re: New 2K5 trial)

To: REALbasic NUG <realbasic-nug at lists dot realsoftware dot com>
Subject: Re: Instantiating object (Re: New 2K5 trial)
From: William Squires <wsquires at satx dot rr dot com>
Date: Mon, 29 Aug 2005 18:11:02 -0500
Cc:
Delivered-to: realbasic-nug at lists dot realsoftware dot com
References: <E66B2B85-5858-45C4-A338-42CDFF8ABC3D at innernine dot com>

On Aug 29, 2005, at 1:16 PM, Juergen Schreck wrote:


On Aug 29, 2005, at 9:24 AM, Joseph J. Strout wrote:
- This doesn't compile:

        Dim a As New Object , b As New Object

  Neither compiles this

    Dim a, b As New Object

I see a bug icon, but no explanation. Do we have a new syntax here?


I'm not sure what's going on there.

The bug explanation in r2 is: 'There is no class with this name' .

The LR sez:

"Object is the base class of all other classes. Any object can be assigned into a variable of type Object. The default value of a new instance of an object is Nil. The Object class has no properties, methods, or events."

Confusion here, I do detect... An instance variable (which is really a reference to some location in memory) can be nil, but you can't put the name of a variable as the operand to the "New" operator, only the name of a class (which "Object" certainly is). The problem is trying to "New" the objects in question - the class 'Object' is what they call abstract; you can't actually make one of these because there's not enough information for the compiler to completely construct one for you. The same thing is true of a Class Interface as well.
  What you want is just:

Dim a As Object, b As Object // Leave out the 'New'

So I'd say since the default instance has a value of NIL and you can't instantiate NIL, you therefore can't instantiate Object.

I'm curious as to why someone would want to create and object instance anyway?

Polymorphism. A method can declare the type of one of its arguments as 'Object' and that'll allow a reference (including nil) to any class to be valid. Let's say you have two classes, 'A' and 'B'. Neither is a subclass of the other, and you want to allow a method (sub or function) to take an instance of either and print it to the screen with the MsgBox function, after some fancy formatting, etc...
  If you say:

Sub MySub(obj As A)
  // Do stuff to 'obj'
End Sub

then you can't pass a type of 'B' to it, since a reference-to-A is NOT a reference-to-B. You'll have the same problem in reverse if you say:

Sub MySub(obj As B)
  // This still doesn't work!
End Sub

  However, if you say:

Sub MySub(obj As Object)
// Now we're getting somewhere; we just need to be able to tell what 'obj' is!
  If (obj IsA A) Then
    // obj is a reference-to-A, so treat it as such
  Else
    If (obj IsA B) Then
      // obj is a reference-to-B. Do whatever...
    Else
      MsgBox "Serious booboo - you didn't give me an A or B!"
    End If
  End If
End Sub

Now you CAN pass the "MySub()" method an instance that points (refers) to an object of type A, or of type B.
  HTH!

Regards,
Juergen



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

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>


A compiler is a tool for turning source code into error messages; generating machine language bytes is just a fortuitous by-product!

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

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

<Prev in Thread] Current Thread [Next in Thread>