PDA

View Full Version : class with values that is used in a different class which computes outputs based on t


Jerry62712
12-23-2008, 05:43 PM
What I want is a class with values that is used in a different class which computes outputs based on those values and values from a form.

I see it as form calls script FSCalc to take the input form values and populate the output state fields. FSCalc will call FSParms which will then call FSDatabaseRead. That will actually have hard coded values for now and later point to a database to get the default parameters.

Specifically, I'll have FSParms with function:
private function stdDeduction(int hhSize):int {
/* Standard Deduction based on house hold size */
/* dollars per month - default is $0 */
var returnHHSizeDeduction:int = 0;

if (!validateHHSize) {
return hhSizeDeduction;
}

var hhSizeDeductionArray:Array = standardDeductions();


"standardDeductions" would be in class FSDatabaseRead:
private function standardDeductions():Array {
var sdArray:Array;
sdArray.push(144); /* less than 4 household members */
sdArray.push(147); /* 4 household members */
sdArray.push(172); /* 5 household members */
sdArray.push(197); /* more than 5 household members */
return sdArray;
}


So this returns an array of values to FSParm which will then return the specific value depending on the argument passed to it.

My questions include:
1) how do I "import" the .AS files so the classes are known to each other?
2) how do I address the function in FSParms to FSDatabaseRead?
3) am I missing something obvious?

TIA,
Jerry

drkstr
12-23-2008, 07:38 PM
package pkg.path {

public class SomeClass {

public static function someStaticFunction(): String {
return 'foo';
}

public function someFunction(): String {
return 'bar';
}

}
}

package pkg.some.other.path {

import pkg.path.SomeClass;

public class SomeOtherClass {

public function doStuff(): void {
//call a static function (I perfer this for utility classes such as a "calculator"
trace(SomeClass.someStaticFunction());

//the other way is to instantiate the class and call one of it's method
var classVar:SomeClass = new SomeClass();
trace(classVar.someFunction());
}

}
}

Make sense?


Best Regards,
~Aaron

Jerry62712
12-23-2008, 08:30 PM
In file FSParms.as in directory src under the project FSCalculator:
package src
{
import src.FSDatabaseParms;

public class FSParms {

public function FSParms() {

}

private function stdDeduction(int hhSize):int {
/* Standard Deduction based on house hold size */
/* dollars per month - default is $0 */
var returnHHSizeDeduction:int = 0;

if (!validateHHSize()) {
return 0;
}

var sd:FSDatabaseParms = new FSDatabaseParms();
var hhSizeDeductionArray:Array = sd.standardDeductions();
return hhSizeDeductionArray[hhSize];

} /* stdDeduction */

private function validateHHSize(int hhSize):Boolean {
if (hhSize < 1 || hhSize > 99) {
/* set some error code here */
return false;
}
else {
return true;
}
}
} /* class FSParms */
} /* package */

In file FSDatabaseParms.as in directory src under the project FSCalculator:
package src
{
/* the goal is to replace this with a call to the databases */

public class FSDatabaseParms
{
public function FSDatabaseParms()
{
}

private function standardDeductions():Array {
var sdArray:Array;
sdArray.push(144); /* less than 4 household members */
sdArray.push(147); /* 4 household members */
sdArray.push(172); /* 5 household members */
sdArray.push(197); /* more than 5 household members */

return sdArray;
}
} /* FSDatabaseParms class */
} /* package */

Is this what you said? FSParms imports FSDatabaseParms then uses a method in it (should it be public or is private OK in the same package?) that returns an array. That array, in FSParms, is indexed by the value passed to FSParms function stdDeductions. The resulting value is returned to the invoking class (FSCalc.as - not yet written).

The alternative (?) is to use:
var hhSizeDeductionArray:Array = FSDatabaseParm.standardDeductions();

BTW, thanks for the response. Does this board have a "thank you" counter?

drkstr
12-23-2008, 08:59 PM
The package should point to the directory the file is in below the top level directory (usually 'src').

in my example, you would have the following files:

src/pkg/path/SomeClass.as
src/pkg/some/other/path/SomeOtherClass.as

Generally packages are used to keep your classes in some kind of logical group. How you organize it is up to you.

If all your classes are in the top level directory, you do not need to specify any package. In other words:

package src {

shgould just be:

package {


should it be public or is private OK in the same package?Only public properties and methods can be accessed from outside the class, so you will need to make them public.


Best Regards,
~Aaron

Jerry62712
12-23-2008, 09:55 PM
Again, thanks for the help.

My FSCalc.as file was in the scr directory, but I moved it to the new scripts directory with the other .as files. In the function that sets the state to results, I want to change the value of all the data fields to the results of the calculations. Two problems have occurred.

1) now that it is moved, the "package scripts {" is getting an error. (Multiple markers at this line: -1037: Packages cannot be nested. -316 changed lines)

2) when I try to change a text field, I don't see any "help" so when I type "resultResultsData." I don't get the property "text" for that field. It exists in the result state and is set:
public function swapToResultsState():void {
currentState = "Result";
/* set the values */

resultResultsData.text = "xx";
}


Any ideas?

Plus, I can't seem to change the title of this thread from the unbearable one there now to what I THOUGHT I had set it to.

drkstr
12-24-2008, 02:32 AM
I mean this with all the best intentions, but it may be quicker for you to go to a local library or book store and pop open a book on AS3. Read a few chapters on classes, packages, and OOP. Otherwise you will eat up a lot of time asking individual questions on a very broad topic.

After you read up a bit, post back if you still have questions and I would be happy to help.


Best Regards,
~Aaron