Qbasicnews.com
October 15, 2019, 11:13:07 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: FreeBasic OOP  (Read 5338 times)
fishstix
New Member

Posts: 2


« on: February 10, 2005, 04:03:09 PM »

I am trying to get freebasic to do a bit of oop by defining some variables in a typeset as functions. However, I do not know how to give these functions access to the other variables in my datatype.. Here's the functionless code..

Code:

type myOBJ
 setName as sub(xName as string)
 getName as function() as string
 name as string
end type

dim foo as myOBJ

declare sub setName(xName as string)
declare function getName() as string

'init
foo.setName = @setName
foo.getName = @getName


sub setName(xName as string)
end sub

function getName() as string

 getName = "hello"

end function


while it's not hooked up, what I want to do is call setName, and assign the incoming value to the 'name' variable of the myOBJ datatype. Is this possible to do? Below is an example of how you would do it in java.


Code:

class Foo
  String name;
 Foo() {
 }
 public void setName( String arg0) {
  this.name = new String();
  this.name = arg0;
 }


Thanks!
Logged
VonGodric
Ancient Guru
****
Posts: 674



WWW
« Reply #1 on: February 10, 2005, 04:24:43 PM »

FB isn't oop oriented, but this should work:
Code:
type myOBJ
 setName as sub(this as myOBJ ptr, xName as string)
 getName as function(this as myOBJ ptr) as string
 name as string
end type

dim foo as myOBJ

sub setName(this as myOBJ ptr, xName as string)
   this->name = xName
end sub
function getName(this as myOBJ ptr) as string
 getName = this->Name
end function

'init
foo.setName = @setName
foo.getName = @getName

'test
foo.SetName (@foo, "Hello world!")
print foo.GetName (@foo)
sleep
Logged

url]http://fbide.sourceforge.net/[/url]
fishstix
New Member

Posts: 2


« Reply #2 on: February 10, 2005, 04:30:23 PM »

Thanks for the help! Looks like I'm better off just creating functions and passing straight values.. that could get complicated hehe.
Logged
VonGodric
Ancient Guru
****
Posts: 674



WWW
« Reply #3 on: February 10, 2005, 04:32:18 PM »

nope, you just keep passing 'this' as an argument -c++ does that anyway thoo it is hidden from you.
Logged

url]http://fbide.sourceforge.net/[/url]
Joakim
Senior Member
**
Posts: 230



WWW
« Reply #4 on: February 10, 2005, 06:44:33 PM »

I very much doubt c++ internally stores function pointers in each class instance though Wink
Logged
VonGodric
Ancient Guru
****
Posts: 674



WWW
« Reply #5 on: February 10, 2005, 06:46:51 PM »

it passes "this" as an optional first argument when you call functions -this is what I meant.
Logged

url]http://fbide.sourceforge.net/[/url]
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!