Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Developer
Posts: 158
Registered: ‎01-11-2010
My Device: 9700 | 9900 | PlayBook | Dev Alpha
My Carrier: Many :)
Accepted Solution

accessing objects in other classes

 

MyApp.as

 

 

.
.

.
// make a MyClass class public var api:MyClass = new MyClass();


// make a MyOtherClass class
public var object:MyOtherClass = new MyOtherClass();


// make a MyThird class
public var object:MyThird = new MyThird();

.
.
.

 

 

MyOtherClass.as:

 

 

myButtonWasClicked(e:TypeOfEvent){

//run the method after the event!
MyThird.method();

}

 

 

 

 

MyThird.as is just a library i put to gether... it does nothing but process strings and things.

 

 

My question is how can i access an object (the instance of MyThird, called object in the 1st decleration ) inside of a DIFFERENT class like i am doing in MyOtherClass.

 

 

the idea is that there are seperate classes to my project (which are mainly UI elements) that all need access to this library.  i'd like to only have to make one instance of the library in the MAIN class rather than instances in EACH class.

 

thanks!

 

- - - - - - - - - -
Developer
Posts: 119
Registered: ‎03-10-2010
My Device: Bold 9000
My Carrier: Rogers

Re: accessing objects in other classes

Hi,

 

If I've got it right, you are trying to make your class behave like a static class, or in other words, a class that has only one single instance during your application life-cycle.

 

Although ActionScript doesn't have really a static class, you can get the same behavior using static methods and proprieties in a normal class. Let's say you have:

 

 

package
{
	public class MyStaticClass
	{
		public static const MYVALUE:int = 100;
		
		public static function MyStaticFunction(): void
		{
			trace("you called that");
		}
	}
}

 

 

Then you could call its methods and proprieties from any other class, like this:

 

 

MyStaticClass.MyStaticFunction();
trace (MyStaticClass.MYVALUE);

 

I hope it helps.

 



(*) Kudo-me if you really think I helped you!
My plug: I'm doing a Mobile Experiment, reported on my Blog: Check it out!.
---------------------------
Paulo Garcia
http://www.mobilecreators.com - Twitter: http://twitter.com/mobilecreators
Highlighted
Developer
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Re: accessing objects in other classes

If this is what your are trying to do, you can have a static instance of the class (also known as a singleton class).  This avoid static functions that can make some coding a little more difficult.  Like everything, what ever gets the job done and is easy to maintain.

 

If you just need access of one class from another, you can just encapsulate the class in the other class.  In some instances, you can pass a class as a reference to another class.  If you need to pass events from one class (ui element) to another, you can have a centralized singleton class as an event dispatcher that allows event passing and global state information that your application may need.  It allows for a loosely coupled application and avoids the need to pass class definitions around which can become a mess.

 

 

Developer
Posts: 158
Registered: ‎01-11-2010
My Device: 9700 | 9900 | PlayBook | Dev Alpha
My Carrier: Many :)

Re: accessing objects in other classes

Sorry for the delay.  I've been VERY busy.

 

making my class static did work.  thanks!

 

 

I just want to point out that the class i needed to make static live inside of a different package, so i cant just do

 

 

 

myStaticClass.Function(variable)

 

 

 

i have to do

 

 

myCustomPackage.mySpecificStaticClass.manipulate(this, variale)

 

 

 

thanks all!

 

 

 

- - - - - - - - - -
Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: accessing objects in other classes

hey dtater,

 

if you import the class into your .as file, you should be able to do it the short hand way. do the following at the top of your .as file:

 

 

import myCustomPackage.mySpecificStaticClass;

 

 

from then on you should be able to do stuff like this:

 

 

mySpecificStaticClass.manipulate(this, variale)

 

hope that helps out. good luck!

 

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Developer
Posts: 158
Registered: ‎01-11-2010
My Device: 9700 | 9900 | PlayBook | Dev Alpha
My Carrier: Many :)

Re: accessing objects in other classes

Doh.

 

 

The More you know!

- - - - - - - - - -
Developer
Posts: 345
Registered: ‎03-13-2011
My Device: Playbook
My Carrier: n/a

Re: accessing objects in other classes

If you have a lot of these it might be a good idea to create a simple utility class with nothing more than helper functions.
Developer
Posts: 158
Registered: ‎01-11-2010
My Device: 9700 | 9900 | PlayBook | Dev Alpha
My Carrier: Many :)

Re: accessing objects in other classes

Thats exactly what this class is Smiley Happy.

 

i am taking the AS3 JSON library, moddifying it to better suit my needs, and making a whole series of functions designed to request and manipulate the json data.  this way i can query a particular JSON api and process the results in a a few function calls.

 

See my signature for the details of the application... (as soon as it's done!)

- - - - - - - - - -