Can you help me make more OOP abstraction with this code? - actionscript-3

This code is not java code, and I'm not getting any answer from ActionScript developers. So I tagged it with java, but Action Script is similar to java and this an OOP question.
I'm using Grid Data and I want to accomplish this following task:
Method 1: I want to multiply each row Row1num1 * Row1num2 and so on,
var Row1num1:String;
var Row2num2:String;
var Row2num1:String;
var Row2num2:String;
var Row3num1:String;
var Row3num2:String;
var event1:Object={num1:Row1num1,num2:Row1num2};
var event2:Object={num1:Row2num1,num2:Row2num2};
var event3:Object={num1:Row3num1,num2:Row3num2};
then add them to a dataGrid
dataGrid.columns =["num1","num2"];
dataGrid.addItem(event1);
dataGrid.addItem(event2);
dataGrid.addItem(event3);
but by using this method, if I have 20 rows, I will have a lot of variables, obviously it's bad.
method 2: In this method creating Grid Data rows at runtime and multiply them.
//button to add rowGrid
dd.addEventListener(MouseEvent.CLICK,ddd);
var numm:String="34";
function ddd(evt:MouseEvent):void
{
var event4:Object={num1:Rownum1,num2:Rownum2};
dataGrid.addItem(event4);
}
but when I use this method, I have a hard time accessing each row data and multiply them.
This example because I'm creating GPA calculator and I want to take each row credit Hours and multiply them with the scale value at the same row, first method is bad because there's not abstraction .
The second method what I'm hoping to work ,because I want user to add row depend on their number of courses.
I hope my English is not bad.
I hope my question don't get vote down, and by reading this question can you determine what I'm missing so I can learn it .
And is there any tutorial I can use to solve my problem?

I'm just addressing your first method for now, but it almost seems at though you want an array of some sort.
Here's a link on how to use Actionscript arrays.
If you need more dimensions, you can make an array of arrays. This will help you cut down on the number of variables.

I hope I correctly understood your question. I'll give it a go either way...
So, one of the best things about actionscript in comparison to most other strongly-typed Object-Oriented languages is how easy reflection is (probably thanks to its javascript origins).
That being said, what you can do is simply create an array using a "for" loop. What I am assuming is that the variables row1Num1 row2Num2 and so on already exist in your class. Otherwise, obviously it would be much more efficient to store them in an array and simply read from it into a new array. Anyhow, the code should look something like this:
method 1:
var eventsArr:Array = [];
for(var i:int = 1; this["row" + i + "Num1"] != undefined /*or i<=length*/; i++){
eventsArr.push({num1:this["row" + i + "Num1"], num2:this["row" + i + "Num2"]});
}
for(var j:int = 0; j < eventsArr.length; j++){
dataGrid.addItem(eventsArr[j]);
}
method 2:
dd.addEventListener(MouseEvent.CLICK,ddd);
var numm:String="34"; //I am assuming this refers to the row number you wanted to add.
function ddd(evt:MouseEvent):void
{
var event4:Object={num1:this["row" + numm + "Num1"],num2:this["row" + numm + "Num2"]};
eventsArr.push(event4);
dataGrid.addItem(event4);
}
Hope that helps.

Related

How to count the number of rows after filtering an advanceddatagrid?

I am trying to count the number of rows in an advanceddatagrid
I need a function which could count all item with or without filterFunction.
I tried some solution but none works. The best that I found, is to expand all items and use a cursor for looping.
But, when we have a lot of data, expanding all is not a good solution.
Do you have an idea on how to do that ?
Thank you
The only way I came up was investigating dataProvider
// current not expanded data row lenght
grid.dataProvider.lenght;
// expanded length
// I assume you use xml as your data provider
// then you can count it like this
xmlListTotalSize(new XMLList(grid.dataProvider.source.source));
// or with casts
xmlListTotalSize(new XMLList((IHierarchicalCollectionView(view.grid.dataProvider).source as HierarchicalData).source));
and xmllist traversal function might look something like this:
private static function xmlListTotalSize(x:XMLList):int
{
var i:int = x.length();
for each(var xChild:XML in x.children())
i += xmlListTotalSize(xChild.children());
return i;
}

AS3 - getting variable named in order within a function

I am sorry if this is a beginner's question.
I made some Arrays named like map01, map02 and so on... As you can see, I'm making a tile-based flash here. And I need to make a function that when you input a number like: createmap(1); it will get the variable map01 and use the information.
Can I do anything like: var temp:Array = Array(["map" + valueInput]);??
Please tell me if you need anything more.
First, instead of having variables with indices in their names, you should create an array of them. Here, an array of arrays.
So you just have to call var temp:Array = maps[valueInput] as Array;.
If you really don't want to do that and stick with your n variables, you can write
var index:String = valueInput.toString();
if (index.length == 1)
index = "0" + index; //have the index on two digits "01", "02"
var temp:Array = this["map" + index];
Note that it will only work for your 99 first variables (oh God...)

How to sort var length ids (composite string + numeric)?

I have a MySQL database whose keys are of this type:
A_10
A_10A
A_10B
A_101
QAb801
QAc5
QAc25
QAd2993
I would like them to sort first by the alpha portion, then by the numeric portion, just like above. I would like this to be the default sorting of this column.
1) how can I sort as specified above, i.e. write a MySQL function?
2) how can I set this column to use the sorting routine by default?
some constraints that might be helpful: the numeric portion of my ID's never exceeds 100,000. I use this fact in some javascript code to convert my ID's to strings concatenating the non-numeric portion with the (number + 1,000,000). (At the time I had not noticed the variations/subparts as above such as A_10A, A_10B, so I'll have to revamp that part of my code.)
The best way to achieve what you want is to store each part in its own column, and I would strongly recommend to change table structure. If it's impossible, you can try the following:
Create 3 UDFs which returns prefix, numeric part, and postfix of your string. For a better performance they should be native (Mysql, as any other RDMS, is not really good in complex string parsing). Then you can call these functions in ORDER BY clause or in trigger body which validates your column. In any case, it will work slower than if you create 3 columns.
No simple answer that I know of. I had something similar a while back but had to use jQuery to sort it. So what I did was first get the output into an javascript array. Then you may want to insert a zero padding to your numbers. Separate the Alpha from Nummerics using a regex, then reassemble the array:
var zarr = new Array();
for(var i=0; i<val.length; i++){
var chunk = val[i].match(/(\d+|[^\d]+)/g).join(',');
var chunks = chunk.split(",");
for(var s=0; s<chunks.length; s++){
if(isNaN(chunks[s]) == true)
zarr.push(chunks[s]);
else
zarr.push(zeroPad(chunks[s], 5));
}
}
function zeroPad(num,count){
var numZeropad = num + '';
while(numZeropad.length < count) {
numZeropad = "0" + numZeropad;
}
return numZeropad;
}
You'll end up with an array like this:
A_00100
QAb00801
QAc00005
QAc00025
QAd02993
Then you can do a natural sort. I know you may want to do it through straight MySQL but I am not to sure if it does natural sorting.
Good luck!

Re-stacking MovieClips in an Array

I was trying to make a similar thing with the game SameGame (ie. the block above the removed blocks fall downward). Before trying this with an Array that contains MovieClips, this code worked (tried it with int values). With MovieClips on the array, it seems not working the same way.
With int values, example:
popUp(0, 4): Before: 1,2,3,4,5,6,7,8,9,10; After: 1,2,3,4,6,7,8,9,10
But with MovieClips:
popUp(0, 4): Before: 1,2,3,4,5,6,7,8,9,10; After; 1,2,3,4
// Assume the numbers are movieclips XD
Basically, it strips everything else, rather than just the said block >_<
Here's the whole method. Basically, two extra arrays juggle the values above the soon-to-be removed value, remove the value, then re-stack it to the original array.
What could be wrong with this? And am I doing the right thing for what I really wanted to emulate?
function popUp(col:uint, row:uint)
{
var tempStack:Array = new Array();
var extraStack:Array = new Array();
tempStack = IndexArray[col];
removeChild(tempStack[0]);
for(var ctr:uint = tempStack.length-(row+1); ctr > 0; ctr--)
{
removeChild(tempStack[ctr]);
extraStack.push(tempStack.pop());
trace(extraStack);
}
tempStack.pop();
for(ctr = extraStack.length; ctr > 0; ctr--)
{
tempStack.push(extraStack.pop());
//addChild(tempStack[ctr]);
}
IndexArray[col] = tempStack;
}
PS: If it's not too much to ask, are there free step-by-step guides on making a SameGame in AS3 (I fear I might not be doing things right)? Thanks in advance =)
I think you just want to remove an element and have everything after that index shift down a place to fill what you removed. There's an inbuilt function for this called splice(start:uint, length:uint);
Parameters:
start - the index to start removing elements from
length - the amount of elements to remove
var ar:Array = ["hello","there","sir"];
ar.splice(1, 1);
ar is now -> ["hello", "sir"];
As per question:
Here's an example with different types of elements:
var ar:Array = [new MovieClip(), "some string", new Sprite(), 8];
ar.splice(2, 1);
trace(ar); // [object MovieClip], some string, 8
And further example to display the indexes being changed:
trace(ar[2]); // was [object Sprite], is now 8

Linq2Sql referring to entity column by variable creation

I have a linq2sql class with fields
WeekEnding1
WeekEnding2
WeekEnding3
WeekEnding4
I want to write some c# using the fields in a for loop.
Take this for example:
for(int i=1; i<=4; i++)
{
Msgbox(myClass.WeekEnding + i)
}
I realise that wont work but what will??
Malcolm
Unless you want to get into something with reflection, this will:
MsgBox(myClass.WeekEnding1);
MsgBox(myClass.WeekEnding2);
MsgBox(myClass.WeekEnding3);
MsgBox(myClass.WeekEnding4);
You can do what you're trying to do with reflection by putting this inside the loop:
PropertyInfo info myClass.GetType()
.GetProperty("WeekEnding" + i.ToString(),
BindingFlags.Public | BindingFlags.Instance);
MsgBox(info.GetValue(myClass, null));
But I'd recommend the first approach! The second approach will have to find the property in question on each pass through the loop, adding a considerable overhead.
In any event, your underlying data model sounds very much like it might need normalising - this is a common bad smell!