user1554472
user1554472

Reputation: 57

how to access the constructor of one class from another class in actionscript?

I’ve two classes namely adminglobal.as and company.as.

I need to access the constructor present inside adminglobal.as from company.as. Also when accessing I need to use the variables present inside the constructor from company.as too.

Basically what I need is to call the constructor in adminglobal.as from company.as and use the variables present inside the constructor in company.as. How to do it?

Upvotes: 0

Views: 2769

Answers (3)

danii
danii

Reputation: 5693

Let's say you have a class AdminGlobal in a file named adminglobal.as and a class Company in a file company.as. To access the constructor of one class inside the other, you only have to make that class public and then import that class, if they are placed in different packages. If they are in the same package then you don't need to implicitly import the class.

In order to use the variables and methods from one class in another you only need to make sure that those variables and methods are marked as public as well. I suggest you take a look at this tutorial from Kirupa about Classes in ActionScript3.

For example, if you have adminglobal.as inside a folder named admins, the route would be: your_project_folder/src/admins/adminglobal.as

And example contents of class AdminGlobal:

package admins
{
  public class AdminGlobal
  {
    //accesible variable
    public var name:String;

    //constructor
    public function AdminGlobal()
    {
      //do init stuff here
      name= "anon";
    }

  }

}

And if you have your class Company in the file company.as in the src folder of your project (default package), the route would be: your_project_folder/src/company.as

And example contents of class Company, accessing class AdminGlobal:

package
{
  //import public class from another package
  import admins.AdminGlobal;

  public class Company
  {

    //constructor
    public function Company()
    {
      //call constructor of public class
      var ag:AdminGlobal = new AdminGlobal();

      //access public variables
      trace(ag.name);
    }

  }

}

Upvotes: 1

shaunhusain
shaunhusain

Reputation: 19748

I believe you're just thinking through this the wrong way, you can have arguments/parameters for your constructor, so AdminGlobal could be declared like

public AdminGlobal(companyId:Number,companyName:String);

then in Company you could call

new AdminGlobal(this.companyId,this.companyName);

and do whatever you need to do with those variables in the constructor of AdminGlobal

Or even

AdminGlobal(company:Company);

then in Company do

new AdminGlobal(this);

and you have a handle on any public members of Company from AdminGlobal;

Upvotes: 0

Ahmet Alsan
Ahmet Alsan

Reputation: 624

Use "public static var" to reach a variable from other classes.

For Example;

package com
{
    public class DataVO
    {
        public static var version:String = "1";
    }
}

Upvotes: 0

Related Questions