• Welcome to Theos PowerBasic Museum 2017.

Data type mismatch with class/interface/instance vars

Started by Bud Meyer, July 23, 2009, 06:41:33 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Bud Meyer

I'm getting my feet wet with objects by attempting to use a class instead of a UDT, but the built-in functions don't work with the vars. Is this normal behavior for objects?

#COMPILE EXE

CLASS TestClass
    INSTANCE mystring AS STRING
    INSTANCE mylong   AS LONG

    INTERFACE TestInterface: INHERIT IUNKNOWN
         PROPERTY GET mystring() AS STRING
             PROPERTY = mystring
         END PROPERTY
         PROPERTY SET mystring(BYVAL param AS STRING)
             mystring = param
         END PROPERTY

         PROPERTY GET mylong() AS LONG
             PROPERTY = mylong
         END PROPERTY
         PROPERTY SET mylong(BYVAL param AS LONG)
             mylong = param
         END PROPERTY
    END INTERFACE
END CLASS

FUNCTION PBMAIN() AS LONG
    LOCAL cTest AS TestInterface
    cTest = CLASS "TestClass"
    cTest.mystring = "test"
    cTest.mylong   = 5
    'replace "test" with "123" in cTest.mystring  '<----- Data type mismatch
    'incr cTest.mylong                            '<----- Data type mismatch
    ? cTest.mystring & $CRLF & FORMAT$(cTest.mylong)
END FUNCTION

Edwin Knoppert

The names conflict for members and instance vars..

José Roca

Quote
I'm getting my feet wet with objects by attempting to use a class instead of a UDT, but the built-in functions don't work with the vars. Is this normal behavior for objects?

Yes, it is. Replace requires a variable where to put the result. Otherwise, it would be like trying to do replace "test" with "123" in "test".

Bud Meyer

I guess I don't understand objects yet. Is it even possible to use the built-in commands with them?
Can something like this ever work?
INCR (class/interface/etc goes here)

José Roca


José Roca

 
Frankly, Bud, it looks like if you were expecting some sort of magic...

It is not a matter of objects. It also won't work if it was a function, e.g.


#COMPILE EXE
#DIM ALL

FUNCTION Foo () AS LONG
   STATIC x AS LONG
   x = 123
   FUNCTION = x
END FUNCTION

FUNCTION PBMAIN () AS LONG

   INCR Foo

END FUNCTION


These PB statements expect a variable. Otherwise, where are they going to store the result?

Bud Meyer

hmm.. yeah, I guess I'll need to rethink things. My eventual goal is to use a class as a super UDT, which would allow dynamic strings and adjustable arrays. And then I was hoping to just do a quick search & replace to update my existing code to use the object.

This is how my code currently looks (actually, I use string ptrs to achieve dynamic strings, but it's a bit messy). #COMPILE EXE

TYPE TestType
    mystring AS STRING * 10
    mylong   AS LONG
END TYPE

FUNCTION PBMAIN() AS LONG
    LOCAL tt AS TestType
    tt.mystring = "test"
    tt.mylong   = 5
    REPLACE "test" WITH "123" IN tt.mystring    '<-- works fine
    INCR tt.mylong                              '<-- works fine
    ? tt.mystring & $CRLF & FORMAT$(tt.mylong)
END FUNCTION

José Roca

#7
Quote
REPLACE "test" WITH "123" IN tt.mystring    '<-- works fine

Works fine because tt.mystring is a variable. But properties and methods are procedures, not members of an UDT.

Jeff Blakeney

You just need to do a little more work and possibly a few more replaces.  When you have this code:

INCR tt.mylong

and you want to replace the UDT with an object, you can simply replace it with:

ott.mylong = ott.mylong + 1

or you could write a method in your object that would perform an increment for you something like this:

METHOD Inc(BYREF lLongVar AS LONG)
   INCR lLongVar
END METHOD


and replace your INCR statement above with:

ott.Inc(ott.mylong)

Writing your own Find/Replace program would probably make the task of replacing things easier as you could look for words (ie. INCR) and then pull the variable name(s) associated with them (ie. tt.mylong) so that you can put either of the replacements above into the new code.

Theo Gottwald

#9
I have discussed this topic with Jeff from Powerbasic in the past, because I'd also prefer to be able to use Instance data "just like UDT's".
He is very polite and takes a lot of time to explain things. The problem here is thatour idea from a "Superloaded UDT" seems to conflict with the rules of encapsulation of data.
Data of the Object should normally be only accessible through the Methods and Properties of the Object.

Jeff wrote:
QuoteHi Theo,

You are not working on a variable directly when using a method or property, these are functions. You could not do an INCR on a function.
This is one of the benefits of an object, only the object itself can modify the instance data. Instance data is invisible to any code outside of the object.

Again with parameter passing you are trying to modify the value of instance data, which is invisible outside of the object.
You can only modify instance data by calling a method or property in the object.

We are trying not to break the rules of COM here, and these changes certainly would.

Means, a Object is not 100% of an Superloaded UDT, we have to go an extra mile at some places.

While i  like the idea with the Builtin INCR and DECR METHOD. It could be implemente automatically and easily using Macros, if ...

QuoteHi Theo,

R&D is looking into the possibility of allowing macro's to be used in a CLASS and/or INTERFACE block.
Currently macros are limited to use in a procedure only and cannot be used to create a procedure.

Which I am also waiting for.

After all tests, I can recommend to everybody the new PB as one of the best compilers - if not the best - i have ever used.

Using other compilers, i often had the feeling "something is missing here".
For example a good string engine, or support for Quads though all functions.

Not in PB. Let me say it like this. There may be a feature missing here and there, but nothing we can't live without.

All datatypes are complete (just the Super-UDT is missing). PB just a round thing.

Btw. did anybody ever use the Field Datatype for something useful?