I'm creating a button in ActionScript by extending flash.display.SimpleButton
The button doesn't behave as expected, however, when I declare certain variables in the constructor which also happen to exist as properties in the SimpleButton class. They appear to conflict..
Why is this? Shouldn't the locally declared variables be allowed to co-exist with similarly named class properites?
Snippet below might better illustrate the issue:
public class MyButton extends SimpleButton{
public function MyButton(/*..*/){
var upState:ButtonDisplayState = new ButtonDisplayState(/*..*/));
var downState:ButtonDisplayState = new ButtonDisplayState(/*..*/);
var overState:ButtonDisplayState = new ButtonDisplayState(/*..*/);
var hitTestState:ButtonDisplayState = new ButtonDisplayState(/*..*/);
super(upState, overState, downState, hitTestState);
}
}
The API docs are here (look for upState for example): http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/SimpleButton.html#upState
Thanks,
Aodh.
You can't redeclare already existing variables, local or not. The only place where you can do something like this is method parameters, where you can have same parameter names as local / class variables.
Why don't you just pass in those states to the constructor directly like:
super(new ButtonDisplayState(/*..*/)), new ButtonDisplayState(/*..*/)), new ButtonDisplayState(/*..*/)), new ButtonDisplayState(/*..*/)));
or alternatively just set them directly after calling super(); like this:
upState = new ButtonDisplayState(/*..*/));
downState = new ButtonDisplayState(/*..*/);
overState = new ButtonDisplayState(/*..*/);
hitTestState = new ButtonDisplayState(/*..*/);
Related
I am having trouble understanding the meaning of the following code:
public class CustomListViewAndroidExample extends Activity {
ListView list;
CustomAdapter adapter;
public CustomListViewAndroidExample CustomListView = null; // What does this line mean?
public ArrayList<ListModel> CustomListViewValuesArr = new ArrayList<ListModel>();
Its instance of current activity you can use in oncreate like
CustomListView.addContentView(view, params);
It's just like an "ordinary" variable except that it's explicitly set to null when the class is created. It's no different in principal than the following line:
CustomListViewValuesArr = new ArrayList<ListModel>();
This isn't really any different than setting the value in the constructor or OnCreate method.
Note that, as with your other variables, you'll need to initialize this to something other than null in order to be able to use it.
It is slightly odd that it's public, though. I'd also recommend including explicit access modifiers in front of your other fields - it's a bad practice to omit them and rely on the defaults.
I want to store a number of unique variables (objects) in a class like so:
//Notice that each object has unique attributes passed to it
public static Entity SomeEntity01, SomeEntity02, SomeEntity03;
SomeEntity01 = new Entity(some, values);
SomeEntity02 = new Entity(some, new, values);
SomeEntity03 = new Entity(some, other, values);
I wish to access these variables in the class like this:
MyClass.SomeEntity01
I can do this with instantiation, but it would obfuscate the code with useless instances. I can't do this ordinarily because classes don't allow object instantiation outside of methods (from what I can tell).
If possible, how can I store and access variables (specifically objects) in a class without using instantiation or static methods?
You can initialize static fields in a static constructor:
public static class MyClass
{
public static Entity SomeEntity01, SomeEntity02, SomeEntity03;
static MyClass() // static constructor
{
SomeEntity01 = new Entity(some, values);
SomeEntity02 = new Entity(some, new, values);
SomeEntity03 = new Entity(some, other, values);
}
}
I already found the issue and moved the class object inside one of the methods, so I guess I'm just kind of curious why an object such as this MyObject name = new MyObject(); is not recognized but this private static int intName works when initialized (declared) on top of the methods right after "class Program" or whatever your class is.
both should work. If you are accessing it from a static method you need to add static keyword to the object definition like below.
private static MyObject name = new MyObject();
if you are accessing the same from instance method
MyObject name = new MyObject();
is fine. I am assuming this was your problem
I want to send a function 2 parameters.
1. Type of class
2.Name of object.
In the new function I want to create the object by the type and the name that was given.
for example:
private void CreateColumns()
{
try
{
CreateColumn(typeof(DataGridViewTextBoxColumn),"customerFolderID");
CreateColumn(typeof(DataGridViewImageColumn),"customerName");
....
private void CreateColumn(Type ColumnType, string ColumnName)
{
try
{
...
I thought I can use reflection:
object Instance = Activator.CreateInstance(ColumnType);
but then how can I do something like this:
Instance ColumnName = new Instance();
The motivation is to replace those lines:
DataGridViewTextBoxColumn customerFolderID = new DataGridViewTextBoxColumn();
dgv_CustomersGrid.Columns.Add(customerFolderID);
DataGridViewImageColumn customerName = new DataGridViewImageColumn();
dgv_CustomersGrid.Columns.Add(customerName);
is there a way ?
is there any alternative?
maybe I should use dynamic somehow (I don't familiar with this yet) ?
I would like to dynamically invoke a Class's Property via a String. In the following code, I can dynamically invoke a Class's Function via a String.
var myClass:Class = getDefinitionByName("myPackage.MyClass") as Class;
myClass["myStaticMethod"]();
where MyClass is defined as:
package myPackage {
public class MyClass {
public function MyClass() {}
public function myMethod():void {};
public static function myStaticMethod():void {};
public static function get myProperty():Object { return null; }
}
}
However, a Property, such as MyClass.myProperty is not a Function. So,
var myClass:Class = getDefinitionByName("myPackage.MyClass") as Class;
myClass["myProperty"]();
throws an error: TypeError: Error #1006: value is not a function because myProperty is not a Function.
Is there any way to do this dynamically via Strings?
Thanks for the help.
To solve this issue, I simply needed to remove the () from the code. That is, the new code looks like:
var myClass:Class = getDefinitionByName("myPackage.MyClass") as Class;
myClass["myProperty"]; // This works.
The Answer of Alex will indeed works properly, but only if you have the String written properly. Else you get this error thrown at you: TypeError: Error #1006: value is not a function. To avoid this you could try test if the property or method is defined before using it. Like so:
if(myClass["myProperty"] != undefined)
{
...
}
Anyhow, in your specific example you are requesting a getter, and that's why you had to remove the () from your source. If you would be needing a method, I would also recommend you to save the method as a function:
var myFunction: Function = myClass["theFunction"];
And then to use either the call or the apply methods.
myFunction.call(null, myParam);
IF you are interested in studying all the methods that an Object has and comparing them to a String. Consider also:
var child:Sprite = new Sprite();
var description:XML = describeType(child);
var methodList: XMLList = description.descendants('method');
The attributes of a <method/> node are:
name: The name of the method.
declaredBy: The class that contains the method definition.
returnType: The data type of the method's return value.
I hope this helps out, let me know if you found it useful.