How to define Java's Object[] someObject=new Object[10] in Actionscript - arrays

I have a class named Component and it has a code like this
class Component {
var ID:String;
var typical:String;
var connection:String;
var temperature:String;
var voltage:String;
var visibility:Boolean = true;
public function Component(type:String, temperature:String, connection:String, voltage:String) {
this.typical = type;
this.temperature = temperature;
this.connection = connection;
this.voltage = voltage;
}
public function setVisibility(b:Boolean):Void {
visibility = b;
}
}
I would like to create an array instance of this class just like in java (like Component[] someComponent = new Component[10]) How can I define this is Actionscript?

In Flash Player 10, Adobe added the Vector class. It is the typed array you are looking for. You can make a vector like this:
private var vector:Vector.<SomeComponent> = new Vector.<SomeComponent>(10);
private var anotherOne:Vector.<Number> = new Vector.<Number>;

thanks for the answer. I found something that might be another example.
just define an array like
var myArray:Array=new Array();
after that define each element of the array as another object, like
myArray[0]=new Component(); //Here Component is the class which I defined and showed in my original question.

Related

Why does Flutter report error when accessing list element?

I'm receiving a syntax error from Android Studio when creating a list in Flutter (Dart).
Even in the simplest form copied from the Flutter documentation, I get the same error.
the code:
var simonSequence = new List<int>(3);
var c = simonSequence[0]; //error here
final anEmptyListOfDouble = <int>[];
anEmptyListOfDouble[0]=0; //also error here
give an error on the line that accesses the list element.
any suggestions are appreciated.
because you are writing the code inside the class scope, while you must be writing it in a function.
this is what you are doing
class _SimonState extends State<Simon>{
//other codes
var simonSequence = new List<int>(3);
var c = simonSequence[0]; //error
final anEmptyListOfDouble = <int>[];
anEmptyListOfDouble[0]=0; //error
}
this is what your code SHOULD be like
class _SimonState extends State<Simon>{
//other codes
//some function you want your code to be called from
void anyFunction(){
var simonSequence = new List<int>(3);
var c = simonSequence[0]; //error
final anEmptyListOfDouble = <int>[];
anEmptyListOfDouble[0]=0; //error
}
#override
Widget build(BuildContext context) {
//then you will call your function anywhere you need like here
//for example
return RaisedButton(
onPressed:(){
anyFunction();
}
);
}
}
It simply because you're trying to access the declared variable within the class scope. It's marked as error because it's not a declaration for a variable. See the following code and its comment for details:
class _SimonState extends State<Simon>{
// Here you can only declare your variables.
// Or declaring a method.
var simonSequence = new List<int>(3);
var c = simonSequence[0]; // Error! This is not a variable declaration.
final anEmptyListOfDouble = <int>[];
anEmptyListOfDouble[0]=0; // Error! This is not a variable declaration.
...
void anotherMethod() {
...
// Correct, your accessing the variable here.
var c = simonSequence[0];
}
...
}

Cast recieved data from service to the correct model

hi i have the following models:
export class Vehicle {
}
export class Trike extends Vehicle {
}
export class Car extends Vehicle {
}
These are retrieve from a Mock of an API
var vehiclesResource = vehicleService.getResource();
vehiclesResource.query((data) => {
this.areas = data;
var c = data[0] instanceof Car;
var t = data[0] instanceof Trike;
var cc = data[0].constructor === Car;
var tt = data[0].constructor === Trike;
});
A car is retrieved from the API, now when the data arrives i want to cast it to the correct object (Car in this case). but some how c = false t = false cc = false and tt = false.
setting the breakpoint in visualstudio shows me the data is of the type Object (Resource)
what am i doing wrong?
Most likely is that you are receiving data by means of deserializing it. In this case the type of the object will be lost even though its structure will be the same. For example if you do the following:
let a = new Car();
let data = JSON.stringify();
let aa = JSON.parse(data);
The type of the 'aa' object will be 'Object' and not 'Car'.
If it is acceptable you can convert you class definitions to interface definitions instead and use it like this:
let aa = <Car>JSON.parse(data);
Or you can implement your own deserialization method, more info here: How do I initialize a typescript object with a JSON object

ActionScript (AS3) object /JSON text input

I have a problem with ActionScript (AS3) / JSON. There is probably a really simple answer to this, so I apologize if this seems like a really noob question.
What I am trying to do is take multiple text strings, place them into an object and then have that object sent as a JSON string to a server.
Every thing else in my code works fine, accept whenever I input a piece of text into my text input box and send the object, the array produces null entries like so:
{"email":null,"last_Name":null,"first_Name":null,"date_of_birth":null,"telephone":null}
However if I replace customer.first_name = inPutfname with: customer.first_name = " John";
Then the array traces back as the following which is what I want:
{"email":null,"last_Name":null,"first_Name":John,"date_of_birth":null,"telephone":null}
So how do I take the multiple input text strings and place them into the object so that the array reads back as it does in the above example, or ideally like this example:
{"email":randomemail#email.com"last_Name":smith,"first_Name":john,"date_of_birth":27/07/1989,"telephone":012343456788}
Here is the code I have so far:
var inPutFirstname: String;
var inPutLastname: String;
var inPutEmail: String;
var inPutTelephone: String;
var inPutDob: String;
// changes customer data into an object
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
var customer:Object = new Object;
customer.first_Name =inPutFirstname;
customer.last_Name = inPutLastname; PROBLEM AREA?
customer.email = inPutEmail;
customer.date_of_birth = inPutDob;
customer.telephone = inPutTelephone;
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//changes customer object into json string
var myJson: String = JSON.stringify(customer);
var myVariableUrl: URLVariables = new URLVariables();
myVariableUrl.data = JSON.stringify(myVariableUrl);
var myRequestUrl: URLRequest = new URLRequest();
var authHeaderUrl: URLRequestHeader = new URLRequestHeader("xxxxxx", "xxxxxxx");
myRequestUrl.method = URLRequestMethod.POST;
myRequestUrl.data = myJson;
myRequestUrl.url = "website url"
var uload: URLLoader = new URLLoader();
uload.load(myRequestUrl);
uload.load(myRequestUrl);
mySendbutton.addEventListener(MouseEvent.MOUSE_DOWN, thefunction1);
// captures text from the input and places them into the customer object
function captureText1(): void {
inPutFirstname = myFirstName.text;
inPutLastname = myLastName.text;
inPutEmail = myEmail.text;
inPutDob = myDob1.text + myDob2.text + myDob3.text;
inPutTelephone = myTele.text;
}
function thefunction1(event: MouseEvent): void {
captureText1();
trace(inPutfname);
trace(myJson);
}
Any Help would be Amazing.
Thanks guys
Your problem is that the code for creating the JSON object doesn't seem to be contained in a function, which means it will be executed immediately (considering this is probably code attached to a frame).
What you need to do is to is to wrap the code relating to creating the JSON object into a function, something like this:
(Please note that there are a lot of ways this code can be structured better, however this should illustrate some core concepts.)
var inPutFirstname: String;
var inPutLastname: String;
var inPutEmail: String;
var inPutTelephone: String;
var inPutDob: String;
function generateJSON():String {
var customer:Object = new Object();
customer.first_Name = inPutFirstname;
customer.last_Name = inPutLastname;
customer.email = inPutEmail;
customer.date_of_birth = inPutDob;
customer.telephone = inPutTelephone;
return JSON.stringify(customer);
}
function sendJSON(data)
var myVariableUrl: URLVariables = new URLVariables();
myVariableUrl.data = JSON.stringify(myVariableUrl);
var myRequestUrl: URLRequest = new URLRequest();
var authHeaderUrl: URLRequestHeader = new URLRequestHeader("xxxxxx", "xxxxxxx");
myRequestUrl.method = URLRequestMethod.POST;
myRequestUrl.data = data;
myRequestUrl.url = "website url"
var uload: URLLoader = new URLLoader();
uload.load(myRequestUrl);
}
mySendbutton.addEventListener(MouseEvent.MOUSE_DOWN, thefunction1);
// captures text from the input and places them into the customer object
function captureText1(): void {
inPutFirstname = myFirstName.text;
inPutLastname = myLastName.text;
inPutEmail = myEmail.text;
inPutDob = myDob1.text + myDob2.text + myDob3.text;
inPutTelephone = myTele.text;
}
function thefunction1(event: MouseEvent): void {
// First populate the string variables with user input
captureText1();
// Then generate the JSON string
var jsonData:String = generateJSON();
// Lastly, send the JSON string
sendJSON(jsonData);
trace(inPutfname);
trace(jsonData);
}
1st of all, can you add in function captureText1() traces for:
trace(myFirstName.text);
trace(myLastName.text);
trace(myEmail.text);
etc...
just to be sure that you have proper value in there.
And if you are, instead of using extra variables for that you can do (right after traces):
var customer:Object = new Object;
customer.first_Name = myFirstName.text;
customer.last_Name = myLastName.text;
customer.email = myEmail.text;
customer.date_of_birth = "" + myDob1.text + myDob2.text + myDob3.text;
customer.telephone = myTele.text;
var myJson: String = JSON.stringify(customer);
trace(myJson);

How to declare array with custom indices, in Unity JavaScript?

I'm looking for a way to store variables of same type in an array, and access them by their names. Like in PHP you do:
$array = array();
$array['first member']=$var1;
$array['another member']=$var2;
// so on
I want to do a similar thing in Unity3D's JavaScript. I have AudioClip variables, currently used like this:
#pragma strict
var Audio_Goal:AudioClip;
var Audio_Miss:AudioClip;
var Audio_Saved:AudioClip;
function PlaySound_Goal()
{
audio.PlayOneShot(Audio_Goal);
}
function PlaySound_Miss()
{
audio.PlayOneShot(Audio_Miss);
}
of course this requires one function per audioclip, which is ugly.
I want to do it like this:
function PlayAudio(audioname:String)
{
audio.PlayOneShot(AudioArray[audioname]);
}
How can I do this?
You need to use something like a dictionary:
#pragma strict
import System.Collections.Generic;
var AudioArray = new Dictionary.<string,AudioClip >();
// add to dict
AudioArray["one"] = AudioClip.Create(/**/); //see AudioClip.Create(...) for arguments
// play from dict
audio.clip = AudioArray["one"];
audio.Play();
You can do something like:
var myAudioClips: AudioClip[];
function Start() {
// Your stuff...
}
function PlayAudio(audioname:String)
{
for (var myClip in myAudioClips) {
if (audioname.Equals(myClip.name)) {
audio.PlayOneShot(myClip);
}
}
}
And then add all of your clips in the array by dragging them using the editor.
You should be able to use Enumerable.Zip>, but because it's not available in UnityScript, I think this is the best you can do:
var names : List.<String>;
var clips : List.<AudioClip>;
var keyFunc = function(kvp : KeyValuePair.<String, AudioClip>) kvp.Key;
var valueFunc = function(kvp : KeyValuePair.<String, AudioClip>) kvp.Value;
var dictionary = names.Select(
function(name, index) new KeyValuePair.<String, AudioClip>(name, clips[index])
).ToDictionary(keyFunc, valueFunc);
I don't know why you need to store the Funcs. This code compiles, but won't run:
var dictionary = names.Select(
function(name, index) new KeyValuePair.<String, AudioClip>(name, clips[index])
).ToDictionary(function(kvp) kvp.Key, function(kvp) kvp.Value);
Report it as a bug if you want. I don't want to, because I don't think the devs need to waste any more time on this language.

Get Folder Address In DLL

According This Images:
I have a folder named Gallery in Main.data Project.
I added Main.Data.Dll as reference in main project.
I want to show this images in mainwindow form.I change images build action to resource.
I want to know how can i get this folder address in main project?
in mainwindow:
private void LoadImages()
{
foreach (var imgaddress in Directory.GetFiles((Here), "*.jpg", SearchOption.AllDirectories))
{
//Do Some...
}
}
What Do i must write(Here)?
There's probably a more elegant solution, but something like this would get you the resource strings or URIs for all the jpgs in Main.Data:
private void LoadImages ()
{
var asm = Assembly.LoadFrom("Main.Data.dll");
var rm = new System.Resources.ResourceManager(asm.GetName().Name + ".g", asm);
var resourceSet = rm.GetResourceSet(Thread.CurrentThread.CurrentCulture, true, true);
var resourceUris = new List<Uri>();
var resourceStrings = new List<String>();
foreach (var resource in
resourceSet.Cast<DictionaryEntry>().Where(resource => ((string) resource.Key).EndsWith("jpg")))
{
resourceStrings.Add((string)resource.Key);
resourceUris.Add(
new Uri(String.Format("pack://application:,,,/Main.Data;component/{0}",
((string) resource.Key))));
}
rm.ReleaseAllResources();
// Do something with resourceStrings or resourceUris...
}

Resources