Solved

Scope question

Posted on 2003-11-26
3
136 Views
Last Modified: 2010-04-03
Im relatively new to flash an was wondering how this works exactly.

suppose I have a class.....

class Product
{
//variable declaration
var id:Number
var productName:String
var price:Number
//constructor
function Product (id:Number, prodName:Name, price:Number)
 {
 }

function getProduct{
       this.id  = id;
       this.prodName = prodName;
       this.price = price;
}
}


Suppose I have another class where I need to reference this.price?

how do I go about this?  Can I just go ahead and make this.price global so I can use it in any of my classes?  Or do I have to go like this???

class Store{
         var myNewVar:Number;

          var cliplessPedal:Product=new Product(1, "Clipless Pedal", 11);
          cliplessPedal.getProduct
          myNewVar = cliplessPedal.price
}


thanks,

knoxxx

0
Comment
Question by:JohnnyKnoxville
  • 2
3 Comments
 
LVL 27

Expert Comment

by:negatyve
ID: 9827244
The first class should be something like that:

class Product
{
      //variable declaration
      var id:Number
      var productName:String
      var price:Number
      //constructor
      function Product (pId:Number, pProductName:String, pPrice:Number)
      {
            // save parameters
            id = pId;
            productName = pProductName;
            price = pPrice;
      }

      function getProduct():Object
      {
            // returns an object with the instance's properties
            return {id:id, productName:productName, price:price};
      }
}



the second one should be:

class Store
{
      var myNewVar:Number;
      // suppose you have created an instance of Product Class,
      // and to pass it's reference to the instance of Store
      function Store(Product_instance_reference:Object)
      {
            // save in myNewVar the property "price" that you get
            // invoking the getProduct method from the Product Class
            // instance whose reference is in the Product_instance_reference
            // parameter
            myNewVar = Product_instance_reference.getProduct().price
      }
}
0
 

Author Comment

by:JohnnyKnoxville
ID: 9828605
Cool..thanks.    Im planning on creating a bunch of classes.  This will be my first run at it.  Once I create all of my classes....where do I instantiate them?  After reading some tutorials on the web....it looks like I am supposed to create an 'Actions' layer and put the code in there.  Is this correct?  Where do all of the events get triggered?  Do I put code in movies and on components also?  Seems like I'd be putting code all over the place.


thanks,,

knoxxx
0
 
LVL 27

Accepted Solution

by:
negatyve earned 50 total points
ID: 9828637
>Seems like I'd be putting code all over the place.

you are going to write inside the external as file only the classes definitions.
inside your movie you just instantiate your object and apply them their methods.
for example, in a frame of your movie:

var cliplessPedal:Product = new Product(1, "Clipless Pedal", 11);
var testInstance:Store = new Store(cliplessPedal);
trace(testInstance.myNewVar); // should trace 11
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question