Skip to main content
Known Participant
October 13, 2009
Question

Access of undefined method/property through reference with a static type Class

  • October 13, 2009
  • 2 replies
  • 1038 views

I get the following error: (it's not word for word but you get the idea)

Error: Access of undefined method getStatus through reference with a static type Class.

Here's what's happening in the code. I'm trying to create a User class that is instantiated at the start of my app. I want the User class to have properties like mainStatus, with helper methods like setStatus etc. Pretty simple.

so on my HardDisk I have my flash_working folder with all my flash projects. I created my class file/package under the directory com.mypackage

----------------------------------------------------------

package com.mypackage

{

    import flash.display.*;
    public class User extends Sprite
    {
        public var mainStatus:int;
        public function User()
        {
            trace("User Created!");
            mainStatus = 0;
        }

        public function setStatus(status:int):void
        {
         mainStatus = status;
        }

    public function getStatus():int
    {
        return mainStatus;
    }
   
    }
}

--------------------------------------------------------

Ok, so far so good.

now I created a new .fla file under the root of /flash_working/. The class file is in /com/mypackage/User.as

in my .fla file I have:

-------------------------------------

import com.mypackage.User;

var myUser:User = new User();

var i = User.getStatus();

trace(i);

-------------------------------------

That's all the code I have. Could someone please explain why it's giving me that error?

If I try to access the public var mainStatus through user.mainStatus that gives a similar error saying:

Error: Access of undefined property mainStatus through reference with a static type Class.

Thanks for any help!

jef3189

This topic has been closed for replies.

2 replies

October 13, 2009

You can't access the get 'getStatus' function through the class interface. That can only be done with a static class method, but that is not what you want anyway. You just need to access the method through an instance of the class. For example:

var i = myUser.getStatus();

You can also use getters and setters to make your life a little easier with these types of methods.

RossRitchey
Inspiring
October 13, 2009

the public getStatus() function that you created needs to be referred to through an instance of the class.

So:

import com.mypackage.User;

var myUser:User = new User();

var i = myUser.getStatus();

trace(i);

Also, an aside. You can create a getter/setter for status, to avoid having to do the function as such.

package com.mypackage

{

    import flash.display.*;
    public class User extends Sprite
    {
        public var mainStatus:int;
      

        public function get status():int

        {

            return mainStatus;

        }

        public function set status(value:int):void

        {

             mainStatus = value as int;

        }

          public function User()
        {
            trace("User Created!");
            mainStatus = 0;
        }

    }

}

And then, you can call it as:

import com.mypackage.User;

var myUser:User = new User();

trace(myUser.status);

EDIT

I just noticed that you made the variable public as well, which means you can access it without getter/setter or function.

import com.mypackage.User;

var myUser:User = new User();

trace(myUser.mainStatus);