Login
Username:

Password:

Remember me



Lost Password?

Register now!
Main Menu
Who is Online
23 user(s) are online (16 user(s) are browsing Forum)

Members: 0
Guests: 23

more...


Browsing this Thread:   1 Anonymous Users






Re: Want more knowledge about objects
#2
Home away from home
Home away from home


See User information
I hope this answers your question:

Quote:
<pre>OBJECT test
foo:ARRAY OF CHAR
bar
ENDOBJECT

PROC main()
DEF obj:PTR TO test
NEW obj
obj.foo := 'hello world'
obj.bar := 123
END obj
ENDPROC</pre>


Basically, objects have no methods, so you must do everything from procedures. You could make "helper procedures", but then classes with methods start to make more sense:

Quote:
<pre>PROC test_init(object:PTR TO test, foo:ARRAY OF CHAR, bar)
object.foo := foo
object.bar := bar
ENDPROC</pre>

Or you could do this, to make more like a method, but maybe you find it confusing:

Quote:
<pre>PROC test_init(self:PTR TO test, foo:ARRAY OF CHAR, bar)
self.foo := foo
self.bar := bar
ENDPROC</pre>


In either case, you could then do this:

Quote:
<pre>PROC main()
DEF obj:PTR TO test
NEW obj
test_init(obj, 'hello world', 123)
END obj
ENDPROC</pre>


Or for the constructor you could even do this:

Quote:
<pre>PROC test_new(foo:ARRAY OF CHAR, bar) RETURNS self:PTR TO test
NEW self
self.foo := foo
self.bar := bar
ENDPROC

PROC main()
DEF obj:PTR TO test
obj := test_new('hello world', 123)
END obj
ENDPROC</pre>


But as I said, classes with methods start to make more sense if you have many "helper procedures".

Posted on: 2014/2/22 18:24
ChrisH
--
Author of the PortablE programming language.
 Top  Twitter  Facebook  Google Plus  Linkedin  Del.icio.us  Digg  Reddit  Mr. Wong 


Want more knowledge about objects
#1
Just can't stay away
Just can't stay away


See User information
On forum we can find some good examples with class for beginners like this: example

But sometimes I want use only single object in program so probably is not necessary creating all class (useful if we want many objects).
So I want ask how defining or initialising objects without class. It probably possible create object without class definition. So how do it, how update read its fields and how initialise and ending this objects.

Posted on: 2014/2/22 11:01
 Top  Twitter  Facebook  Google Plus  Linkedin  Del.icio.us  Digg  Reddit  Mr. Wong 







You can view topic.
You cannot start a new topic.
You cannot reply to posts.
You cannot edit your posts.
You cannot delete your posts.
You cannot add new polls.
You cannot vote in polls.
You cannot attach files to posts.
You cannot post without approval.
You cannot use topic type.
You cannot use HTML syntax.
You cannot use signature.
You cannot create PDF files.
You cannot get print page.

[Advanced Search]


Search
Top Posters
1 paolone
paolone
4462
2 nikolaos
nikolaos
4206
3 magorium
magorium
4095
4 phoenixkonsole
phoenixkonsole
3942
5 deadwood
deadwood
2917
6 ncafferkey
ncafferkey
2810
7 mazze
mazze
2222
8 Kalamatee
Kalamatee
2212
9 clusteruk
clusteruk
2114
Powered by XOOPS © 2001-2025 The XOOPS Project