A Flash Developer Resource Site

Results 1 to 16 of 16

Thread: [RESOLVED] Array to textField Madness

  1. #1
    Instructional Designer
    Join Date
    Feb 2005
    Location
    Whidbey Island: north of Seattle, east of Victoria, south of Vancouver
    Posts
    299

    resolved [RESOLVED] Array to textField Madness

    This is a bit difficult to explain, but I'm trying to populate a text field from a multi-dimensional array. This is a reach for me, so it isn't surprising that I have an error:
    ReferenceError: Error #1069: Property 0 not found on String and there is no default value.
    at Less1Ex05Main/checkTarget()
    I can't see anything wrong. What is "Property 0? on a string,"? (something like soap on a rope?).

    I'll try to explain the code here. Any suggestions would be appreciated.

    I've complicated things a bit by having the textFiled hold a number as a string (it's a string because it'll have both a number and a comment).
    So I'm capturing the initial string in a variable and converting it to a number so I can run calculations on it:
    Code:
    m_premHolder = Number(m_premText);
    I've put the data into an array (each row is for one customer):
    Code:
    m_premData = new Array(
    	((.39, 'This option lowers the HO-4 premium by 39%.'), (0, 'We do not cover repair businesses.'), (0, 'Customer declines this coverage.'), (17, 'This increases policy limits for theft or loss.'), (0, 'We cannot write a policy in CA with dangerous pets.'), (0, 'Dangerous pets makes this moot.'), (0, 'Dangerous pets makes this moot.'), (0, 'Dangerous pets makes this moot.')), 
    	((.33, 'This option lowers the HO-4 premium by 33%.'), (0, 'We do not cover home repair business'), (0, 'Customer declines this coverage.'), (17, 'HO-210 has a 2k limit, cover this with HO-61.'), (0, 'Customer has no dangerous pets (we can proceed)!'), (0, 'Customer declines this coverage.'), (0, 'Customer declines this coverage.'), (52.11, 'This rate increase includes the jewelry.')), 
    	 ((0, '$100 is the default deductible for H0-3.'), (12, 'Rate varies with amount of liability.'), (0, 'Customer declines this coverage.'), (17, 'This increases policy limits for theft or loss.'), (0, 'Customer has no dangerous pets (we can proceed)!'), (0, 'This endorsement is not appropriate.'), (0, 'Customer declines this coverage.'), (87.60, 'This rate increase includes the jewelry.')), 
    	 ((0, '$100 is the default deductible for H0-3.'), (0, 'Disqualified for excess business traffic.'), (0, 'Customer declines this coverage.'), (17, 'This increases policy limits for theft/loss.'), (0, 'Customer has no dangerous pets (we can proceed)!'), (87, 'Coverage for boats depends on many factors.'), (0, 'Customer declines this coverage.'), (42.30, 'Do not bind, requires appraisal, form B and underwriter review.')), 
    	((.33, 'A 1.5k HO-3 deductible lowers the premium by 33%.'), (16, 'Rate reflects higher liability.'), (0, 'Customer declines this coverage.'), (17, 'This increases policy limits for theft/loss.'), (0, 'Customer has no dangerous pets (we can proceed)!'), (0, 'Customer declines this coverage.'), (0, 'Customer declines this coverage.'), (67, 'Do not bind, requires appraisal, form B and underwriter review.')), 
    	((.39, 'This option lowers the HO-4 premium by 39%.'), (0, 'Declines coverage.'), (0, 'Customer declines this coverage.'), (17, 'This increases policy limits for jewelry, watches, and furs themt/loss.'), (0, 'Customer has no dangerous pets (we can proceed)!'), (0, 'Customer declines this coverage.'), (95, 'Rate varies with liability and value.'), (167.5, 'This rate increase includes the jewelry.')) );
    And here is one case in a switch statement that parses array content to the textField:
    Code:
    switch(event.currentTarget){
    			case down100:
    			       down250.disable();
    				up500.disable();
    				up1k.disable();
    				up15k.disable();
    				up2k.disable();
    				m_premIncrease = m_premData[m_selectedIndex][0][0] * m_premHolder;
    				m_premHolder = m_premIncrease;
    				m_premText.text = String(m_premIncrease) + m_premData[m_selectedIndex][0][1];
    				break;
    Hope this makes sense.
    Last edited by Thomas D.Garrod; 08-05-2009 at 03:38 PM. Reason: elucidation
    Tomas

  2. #2
    Instructional Designer
    Join Date
    Feb 2005
    Location
    Whidbey Island: north of Seattle, east of Victoria, south of Vancouver
    Posts
    299
    I'm wondering if the issue might be string vs number. Could the array content be typed wrong? If so, any suggestions for resolution?
    Tomas

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Your array initialization is really weird. I think you wanted to do a literal declaration, but instead you're using the Array constructor with a bunch of superfluous parentheses.

    Instead of "new Array( crap crap crap )", try using the literal array declarations which are [element, element, element].

    Using nested parentheses like that results in something you really didn't expect. Each set of parentheses is evaluated in turn, but only returns the last element. So you've really only got a single dimensional array there, with 6 elements:

    Dangerous pets makes this moot.,This rate increase includes the jewelry.,This rate increase includes the jewelry.,Do not bind, requires appraisal, form B and underwriter review.,Do not bind, requires appraisal, form B and underwriter review.,This rate increase includes the jewelry.
    I would seriously suggest moving away from the heterogenous multi-dimensional array approach. It looks like you have a list of lists of pairs, where each pair is a number and a string. At the very least, use an Object for the pairs instead of another array. But really, use a Class. And perhaps use a class for the list of pairs as well, if there's a consistent logical definition of them. But here's the quick answer you're looking for:

    Code:
    m_premData = [[[.39, 'This option lowers the HO-4 premium by 39%.'], [0, 'We do not cover repair businesses.'], [0, 'Customer declines this coverage.'], [17, 'This increases policy limits for theft or loss.'], [0, 'We cannot write a policy in CA with dangerous pets.'], [0, 'Dangerous pets makes this moot.'], [0, 'Dangerous pets makes this moot.'], [0, 'Dangerous pets makes this moot.']], 
    	[[.33, 'This option lowers the HO-4 premium by 33%.'], [0, 'We do not cover home repair business'], [0, 'Customer declines this coverage.'], [17, 'HO-210 has a 2k limit, cover this with HO-61.'], [0, 'Customer has no dangerous pets [we can proceed]!'], [0, 'Customer declines this coverage.'], [0, 'Customer declines this coverage.'], [52.11, 'This rate increase includes the jewelry.']], 
    	 [[0, '$100 is the default deductible for H0-3.'], [12, 'Rate varies with amount of liability.'], [0, 'Customer declines this coverage.'], [17, 'This increases policy limits for theft or loss.'], [0, 'Customer has no dangerous pets [we can proceed]!'], [0, 'This endorsement is not appropriate.'], [0, 'Customer declines this coverage.'], [87.60, 'This rate increase includes the jewelry.']], 
    	 [[0, '$100 is the default deductible for H0-3.'], [0, 'Disqualified for excess business traffic.'], [0, 'Customer declines this coverage.'], [17, 'This increases policy limits for theft/loss.'], [0, 'Customer has no dangerous pets [we can proceed]!'], [87, 'Coverage for boats depends on many factors.'], [0, 'Customer declines this coverage.'], [42.30, 'Do not bind, requires appraisal, form B and underwriter review.']], 
    	[[.33, 'A 1.5k HO-3 deductible lowers the premium by 33%.'], [16, 'Rate reflects higher liability.'], [0, 'Customer declines this coverage.'], [17, 'This increases policy limits for theft/loss.'], [0, 'Customer has no dangerous pets [we can proceed]!'], [0, 'Customer declines this coverage.'], [0, 'Customer declines this coverage.'], [67, 'Do not bind, requires appraisal, form B and underwriter review.']], 
    	[[.39, 'This option lowers the HO-4 premium by 39%.'], [0, 'Declines coverage.'], [0, 'Customer declines this coverage.'], [17, 'This increases policy limits for jewelry, watches, and furs themt/loss.'], [0, 'Customer has no dangerous pets [we can proceed]!'], [0, 'Customer declines this coverage.'], [95, 'Rate varies with liability and value.'], [167.5, 'This rate increase includes the jewelry.']] ];

  4. #4
    Instructional Designer
    Join Date
    Feb 2005
    Location
    Whidbey Island: north of Seattle, east of Victoria, south of Vancouver
    Posts
    299

    Thanks

    I really do have trouble understanding arrays. Moock did a really poor job on that. He doesn't show anything about how to instantiate an array, and from his description of the literal versus the other type just increases confusion. If you know of a better explanation, I'd like to see it.

    As to the class, I felt the same way. I not sure how to do that though. I'm scrambling on this project. I promised it and then found out how very ignorant I am. I've spent over a week puzzling different aspects of this and I can't really explain the loss of time. So I've just got to make it work right away. Later, I can make it better.

    I appreciate the guidance. As you can see, it is sorely needed.

    Tomas
    Tomas

  5. #5
    Instructional Designer
    Join Date
    Feb 2005
    Location
    Whidbey Island: north of Seattle, east of Victoria, south of Vancouver
    Posts
    299
    I made the suggested change and got an instantiation on a non-constructor error.
    Tomas

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You probably left the 'new' in your code. Don't do that for the literal declaration. If that's not it, post the exact code causing that error.


    --------------------------------------
    You're probably making it harder on yourself by pressing on before getting some of these very basics down. You'll end up working around your own workarounds and wasting a lot of time. I haven't read Moock's book, but here's the basics on Arrays.
    Arrays are a one dimensional ordered collection. You can instantiate them both with the "new Array" constructor method and the literal array declaration. Using the constructor, refer to the livedocs here:
    http://help.adobe.com/en_US/AS3LCR/F...0.0/Array.html

    There are two forms of the constructor. One which takes a single number as the argument, and one which takes any number of arguments. The first creates an array with that many elements. The second creates an array with the proper number of elements and initializes those elements to the values passed in.
    The literal declaration syntax is a left bracket '[' followed by any number of elements separated by commas, followed by a right bracket ']'.

    So right there, we have 3 different ways to write out creating a new empty array:
    Code:
    var arr:Array = []; //literal, empty
    var arr2:Array = new Array(); //constructor, no argument means use the default value of 0 for the numElements argument.
    var arr3:Array = new Array(0); //constructor, explicitly tell it there are 0 elements
    And two ways of constructing an array with stuff in it:
    Code:
    var arr4:Array = [ 'one', 'two', 'three'];
    var arr5: Array = new Array('one', 'two', 'three');
    Once you have your array, the most important thing to grasp is that you access an individual element with the bracket syntax again, and that array indices begin at 0.
    Code:
    trace(arr4[1]); //traces 'two'
    Read the livedocs and understand the most important methods of arrays: push, pop, shift, unshift, and indexOf. There are more methods, but they are less often used.

  7. #7
    Instructional Designer
    Join Date
    Feb 2005
    Location
    Whidbey Island: north of Seattle, east of Victoria, south of Vancouver
    Posts
    299
    Check that (I forgot to remove the "new"). But now I'm getting a NoN at the front of my string (e.g., "NaN We do not cover repair business.").

    I don't understand that. It should be a number in the form of a string. Did I get something backwards?
    Tomas

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Probably. trace each thing you pull out to see if it's what you expect. You're probably pulling out a String and trying to turn it into a Number resulting in NaN.

  9. #9
    Instructional Designer
    Join Date
    Feb 2005
    Location
    Whidbey Island: north of Seattle, east of Victoria, south of Vancouver
    Posts
    299

    Right

    Right. Here is the trail of data:

    Code:
    //in the constructor
    m_premIncrease = 0;
    trace("m_premIncrease = " + m_premIncrease// Yields: m_premIncrease = 0 
    trace("m_premText = " + m_premText); // Yields: m_premText = [object TextField]
    
    m_premHolder = Number(m_premText);// m_premText is [object TextField]
    trace("m_premHolder = " + m_premHolder);
    
    //in the switch statement
    m_premIncrease = m_premData[m_selectedIndex][0][0] * m_premHolder;
    						m_premHolder = m_premIncrease;
    						m_premText.text = String(m_premIncrease) + "  " m_premData[m_selectedIndex][0][1];
    So the problem is in my conversion statement. But it isn't obvious to me.
    Tomas

  10. #10
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Try
    Code:
    m_premHolder = Number(m_premText.text)
    The Number top-level function (which looks just like a cast ) takes a String, not a TextField. Any data that it doesn't understand, will return NaN instead of throwing an error.

  11. #11
    Instructional Designer
    Join Date
    Feb 2005
    Location
    Whidbey Island: north of Seattle, east of Victoria, south of Vancouver
    Posts
    299
    Must need more Flax.
    I tried m_premHolder = Number(m_premText.text);//same result
    I tried m_premHolder = number(this['customerPremiumText']);//still = object TextField

    :>(
    Tomas

  12. #12
    Instructional Designer
    Join Date
    Feb 2005
    Location
    Whidbey Island: north of Seattle, east of Victoria, south of Vancouver
    Posts
    299
    I even tried m_premHolder = Number(this['customerPremiumText'].text);

    Can't fool Flash, it's still a "object TextField."
    Tomas

  13. #13
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Your reported traces don't make sense. What sort of thing is m_premHolder? If it's a textfield, then instead of trying to convert the content of m_premText to a number just to set it back into a textfield, just copy if over.
    Code:
    m_premHolder.text = m_premText.text;
    If m_premHolder is actually a Number, then I have no idea what your traces actually are. What does this trace?
    Code:
    trace("m_premHolder = " + m_premHolder);

  14. #14
    Instructional Designer
    Join Date
    Feb 2005
    Location
    Whidbey Island: north of Seattle, east of Victoria, south of Vancouver
    Posts
    299

    Good Questions

    Here are the declarations:
    private var m_premText:TextField;
    private var m_premHolder:Number;
    private var m_premIncrease:Number;

    Initially, m_premText holds a number in the form of a string.

    I want to perform a calculation on that number, so I try to convert it to a number.
    I instantiate m_premHolder with:
    m_premHolder = 0;
    trace("m_premHolder = "+m_premHolder);//yeilds 0

    Then I try the number to string conversion and the trace yields "object TextField"

    So are you saying I can perform a calculation on the value for m_premText without converting it to a number?
    Tomas

  15. #15
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    m_premText is a TextField, and it's always going to be a text field. You can't convert it to a number. You can perhaps convert its text property to a number though.

    You need to trace m_premText.text before you attempt to convert the text to a number and store it in m_premHolder, to be sure that you are actually working with the data you think you are.
    Code:
    //in the constructor
    m_premIncrease = 0;
    trace("m_premIncrease = " + m_premIncrease// Yields: m_premIncrease = 0 
    trace("m_premText.text = " + m_premText.text); //Yields:???
    
    m_premHolder = Number(m_premText.text);
    trace("m_premHolder = " + m_premHolder);

  16. #16
    Instructional Designer
    Join Date
    Feb 2005
    Location
    Whidbey Island: north of Seattle, east of Victoria, south of Vancouver
    Posts
    299

    Thanks Mr Loads O Flax

    I was just having logic problems. I followed your advice and traced the thing to death. Part of my problem is that I'm trying to do too much with my logic there are just too many variables. I've simplified things somewhat. I found that converting the number to string conversion before I assigned value to the textFiled was a key.

    Here is a successful case for those interested:

    Code:
    case up500:
    
    down100.disable(); down250.disable(); up1k.disable(); up15k.disable(); up2k.disable(); m_premHolder = 100; m_premCalc = m_premData[m_selectedIndex][0][7] * 100 + m_preLoad; m_premCalc.toFixed(2); m_premHolder = m_premCalc; m_premResult = String(m_premHolder); m_premText.text = m_premResult + " " + m_premData[m_selectedIndex][0][8]; break;
    Tomas

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center