Papa De Beau
Papa De Beau

Reputation: 3828

as3 assign another class to an existing class varriable?

I want to apply a new class to the same var name.

My code:

public var One:Word; 
//later called
One = new Word();

// later changed
One = new redWord(); 

How do I change the var "One" from the class name "Word" to the new class name "redWord"?

Upvotes: 0

Views: 45

Answers (2)

Eduardo
Eduardo

Reputation: 8392

By convention, variable names use lower case and class names use upper case. I would recommend changing One to one and redWord to RedWord.

To be able to assign to One instances of both Word and redWord, it must be declared of a type that is compatible with both. A possibility would be Object, but that may be too generic. Check if there is a common superclass to Word and redWord, and make One of that type.

The more specific your type is, the more errors you will discover at compile time, and the less nasty surprises you will be likely to find at runtime.

Upvotes: 2

Patrick Gunderson
Patrick Gunderson

Reputation: 3281

You can use class wildcards if you want to be able to assign variables of multiple types to a var:

public var One:*;

Alternatively if both of your classes extend the same class you can use that as the base class for your var:

public class Words{
    ...
}

public class Poem extends Words{
    ...
}

public class Prose extends Words{
    ...
}
public var Soliloquy:Words;

Upvotes: 3

Related Questions