Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Fri Jun 22, 2018 12:42 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Question about objects and scope
PostPosted: Thu Nov 15, 2012 11:57 am 
Offline

Joined: Thu Nov 15, 2012 10:47 am
Posts: 2
I'm very new to RB but I have done a fair amount of programming using NeXTSTEP, Xcode and Delphi.

I'm confused about where to put my classes, how to instaniate them and the scope.

For example, in Xcode I usually instantiate my objects in the ApplicationDidFinishLaunching method

In RB, I created module, added my object with a few methods but I'm doing something wrong because when I try to access the object from a button on a window:

textfield1.text =str( myObject.internalYield)

I get an error message

However, if I declare everything in the mousedown function:
dim myobject As new sst.sstPipe
dim myobject as sst.sstPipe
myobject=new sst.sstPipe


myobject.initdp(5.875,4.875,135000,1.0)

textfield1.text =str( myObject.internalYield)

it works fine.

Thanks in advance,


Top
 Profile  
Reply with quote  
 Post subject: Re: Question about objects and scope
PostPosted: Thu Nov 15, 2012 12:43 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
For global objects (one defined in a module and set to Global or Public), you can instantiate them in either

app.Open (before the window is created)
window.Open (after the window is created)


Top
 Profile  
Reply with quote  
 Post subject: Re: Question about objects and scope
PostPosted: Fri Nov 16, 2012 7:58 am 
Offline

Joined: Thu Nov 15, 2012 10:47 am
Posts: 2
Thanks.


Top
 Profile  
Reply with quote  
 Post subject: Re: Question about objects and scope
PostPosted: Fri Nov 16, 2012 10:54 am 
Offline

Joined: Fri Sep 16, 2011 5:23 pm
Posts: 50
Location: Ft. Collins, CO
bjv wrote:
For example, in Xcode I usually instantiate my objects in the ApplicationDidFinishLaunching method

The equivalent in RB would be to do it in App.Open. I rarely have any need for that, however... what sort of global objects are you instantiating?

bjv wrote:
In RB, I created module, added my object with a few methods but I'm doing something wrong because when I try to access the object from a button on a window:

textfield1.text =str( myObject.internalYield)

I get an error message

OK, what do you mean by "added my object" to your module? What exactly did you type or press? What error message do you get? Is "myObject" the name of your module, or some property inside your module? If the latter, did you be sure to make it a global module property? Some specifics would help us help you better.

Without specifics, I can only guess that myObject is a module property, and either you didn't make it global (and so have no access to it outside the module, at least not without the module prefix), or never instantiated it (so its value is nil, and trying to access the internalYield property of a nil object results in a NilObjectException).

If you really do have modules you want to initialize on startup, then App.Open is a reasonable place to do that. Just call SomeModule.Init, where "Init" is a public method in SomeModule, which you would write to do whatever initialization you need (such as instantiating a sst.sstPipe and stuffing it into myObject).

Also, as a general rule, let me suggest you avoid global properties and methods in modules, and instead make them public — they're still accessible everywhere, but only via the module name (e.g. SomeModule.myObject rather than just myObject). This avoids cluttering the global namespace.

HTH,
- Joe

_________________
Image
Joe Strout — Luminary Apps
custom desktop & mobile software


Top
 Profile  
Reply with quote  
 Post subject: Re: Question about objects and scope
PostPosted: Fri Nov 16, 2012 11:53 am 
Offline

Joined: Wed May 20, 2009 4:43 pm
Posts: 958
bjv wrote:
...However, if I declare everything in the mousedown function:
dim myobject As new sst.sstPipe
dim myobject as sst.sstPipe
myobject=new sst.sstPipe...

If you declare these as variables within an event or method, their scope will be limited to that method. Declare MyObject as a property of the window, and it will be available to any method/event of that window (and if you declare it as a public property, it can be accessed outside the window). You can instantiate the object in the window's 'Open' event, or create a Constructor method for the window, and instantiate it there.

_________________
RB 2009r4 Windows XP


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group