Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Fri Nov 16, 2018 9:14 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: [SOLVED]Global Instance
PostPosted: Mon Sep 10, 2012 2:56 pm 
Offline

Joined: Wed Sep 17, 2008 2:45 pm
Posts: 220
Hello

I know how to make global variables, but how can I create global instances?

Thanks in advance.

_________________
My native language is not English.


Last edited by Carharttguy on Tue Sep 11, 2012 5:51 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Global Instance
PostPosted: Mon Sep 10, 2012 4:23 pm 
Offline

Joined: Mon Aug 14, 2006 9:33 pm
Posts: 1774
The same way you make a global variable: Declare the instance of your class where it has global scope.
i.e. a Module, the App Class. etc.

_________________
Roger Clary
Class One Software
Educational Software for Lifelong Learning
http://www.classonesoftware.com


Top
 Profile  
Reply with quote  
 Post subject: Re: Global Instance
PostPosted: Mon Sep 10, 2012 4:29 pm 
Offline

Joined: Wed Sep 17, 2008 2:45 pm
Posts: 220
Well, I tried that, but where should I use the new keyword? I can do it without, but obviously I'm getting a nilobject error.

_________________
My native language is not English.


Top
 Profile  
Reply with quote  
 Post subject: Re: Global Instance
PostPosted: Mon Sep 10, 2012 4:47 pm 
Offline
User avatar

Joined: Mon Apr 02, 2007 2:08 am
Posts: 1225
Location: San Francisco, CA, USA
It all depends on whether or not the global variable is a class. The New operator must be used to instantiate a class; primitive types like Integer or String do not need to be instantiated.

_________________
Boredom Software


Top
 Profile  
Reply with quote  
 Post subject: Re: Global Instance
PostPosted: Mon Sep 10, 2012 4:50 pm 
Offline

Joined: Wed May 20, 2009 4:43 pm
Posts: 958
Carharttguy wrote:
Well, I tried that, but where should I use the new keyword? I can do it without, but obviously I'm getting a nilobject error.

Put it in the App.Open event to initialize when the application starts.

Or, you could initialize it wherever it is used if it is nil:

// Include this wherever myGlobalClassVariable is accessed
if (myGlobalClassVariable is nil) Then
myGlobalClassVariable = new myClass
end if

if NOT (myGlobalClassVariable is nil) Then

... use your variable

end if

_________________
RB 2009r4 Windows XP


Top
 Profile  
Reply with quote  
 Post subject: Re: Global Instance
PostPosted: Mon Sep 10, 2012 5:22 pm 
Offline

Joined: Wed Sep 17, 2008 2:45 pm
Posts: 220
Thanks a lot everybody for thinking with me!

Thanks markwalsh, that App.Open event was very clever, didn't know about that.
Other option is also nice, but it will be used at many places, so it's better if it's initialized on startup :)

Thanks!

_________________
My native language is not English.


Top
 Profile  
Reply with quote  
 Post subject: Re: Global Instance
PostPosted: Mon Sep 10, 2012 5:36 pm 
Offline

Joined: Wed Sep 17, 2008 2:45 pm
Posts: 220
Doesn't seem to work just yet.

Ok, I have a Module name Public_Declarations, containing 1 property: VTC as VirtualTabCollector
In the App.Open event I have
VTC = new VirtualTabCollector


On a Tabpanel I have this code in the Open Event:
Dim VT as new VirtualNewsTab(0, "")
VTC.Tabs.add(VT)


And yet again, a NilObjectException, I have no idea what is wrong.
I just want it to be globally instantiated. Didn't know that was so hard. Or am I asking something superrare now?

Thanks again everybody.
Mathias

_________________
My native language is not English.


Top
 Profile  
Reply with quote  
 Post subject: Re: Global Instance
PostPosted: Mon Sep 10, 2012 5:51 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
You initialize VTC, but what is VTC.Tabs? Is that another object reference within VTC? If so, it must be instantiated, most likely in the Constructor of the VirtualTabCollector class.


Top
 Profile  
Reply with quote  
 Post subject: Re: Global Instance
PostPosted: Mon Sep 10, 2012 6:01 pm 
Offline

Joined: Wed Sep 17, 2008 2:45 pm
Posts: 220
Sorry for not mentioning that.

'Tabs' is the name of a Collection within VTC.
(So it 'collects' virtual tabs, hence the name VirtualTabCollector)

That is (till now) the only property that VTC has.

_________________
My native language is not English.


Top
 Profile  
Reply with quote  
 Post subject: Re: Global Instance
PostPosted: Mon Sep 10, 2012 6:05 pm 
Offline
User avatar

Joined: Sun Aug 05, 2007 10:46 am
Posts: 4931
Location: San Diego, CA
probably becuase the Window containing TAB is intialized BEFORE the APP.OPEN

(never depend on a given order of initialization events)

try this

Dim VT as new VirtualNewsTab(0, "")
If VTC<>NIL then VTC.Tabs.add(VT)

_________________
Dave Sisemore
iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1
Note : I am not interested in any solutions that involve custom Plug-ins of any kind


Top
 Profile  
Reply with quote  
 Post subject: Re: Global Instance
PostPosted: Mon Sep 10, 2012 6:10 pm 
Offline
User avatar

Joined: Fri Nov 10, 2006 4:10 pm
Posts: 1855
Location: Michigan, USA
On a side note, I suggest using a Dictionary instead of a Collection. There's nothing a Collection can do that a Dictionary can't do, plus with benefits — Dictionaries use a hash table, and are therefore faster; and allow for a variant Key, as well as Value.

Hope this helps. :)

_________________
Windows 8 x64
Windows XP Pro SP3
Ubuntu 11.04 via Virtual Box
RS Enterprise 2012r1.1

Programming Tutorials & Free Projects: http://www.JasonTheAdams.com
"Christianity has not been tried and found wanting; it has been found difficult and not tried." - G.K. Chesterton


Top
 Profile  
Reply with quote  
 Post subject: Re: Global Instance
PostPosted: Mon Sep 10, 2012 6:36 pm 
Offline

Joined: Wed Sep 17, 2008 2:45 pm
Posts: 220
After some more testing, and replacing code from the one place to another. I found out what the real problem is.

The problem is not that VTC is not instantiated, but the collection within that class! Ofcourse, a collection is an object, so it should have a New keyword (just like when I use a Dictionary!). So, if I should use an array, there would be absolutely no problem. (But I don't want to use that.)

But, now is the question, how to create an instance from a dictionary or a collection as a property of a class?

I now have something like this:
Image

So, I have no place to put the 'new' keyword somewhere. Anyone has an idea?

Sorry for wasting your time, I don't think the RS debugger is as clear as other debuggers, took me a while before I know how to look what the problem was!

_________________
My native language is not English.


Top
 Profile  
Reply with quote  
 Post subject: Re: Global Instance
PostPosted: Mon Sep 10, 2012 6:39 pm 
Offline
User avatar

Joined: Mon Apr 02, 2007 2:08 am
Posts: 1225
Location: San Francisco, CA, USA
Create a method in the class called Constructor, and put your instantiation code in there. This method will get called whenever you instantiate an instance of that class:

Class VirtualTabCollector
Sub Constructor()
Me.Tabs = New Collection
End Sub
...

_________________
Boredom Software


Top
 Profile  
Reply with quote  
 Post subject: Re: Global Instance
PostPosted: Tue Sep 11, 2012 4:47 am 
Offline

Joined: Wed Sep 17, 2008 2:45 pm
Posts: 220
Thanks a lot! That indeed worked!

@Jason_Adams: I used a Collection, because a Dictionary uses hashes to keep values, hashing is slower than non hashing. I know dictionaries are faster if you want to find something, but in my program, values are always searched sequentially. So it's no profit for me, the contrary actually.

_________________
My native language is not English.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 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