4 Replies Latest reply on Nov 4, 2012 3:49 PM by usoldier18

    How to resolve Comparison between a value with static type string and a possible unrelated type numb

    usoldier18

      I am new to this and any help would be greatly appreciated, Im trying to display custom numbers for values of a Hslider.

      Here is my code so far:

       

      <?xml version="1.0" encoding="utf-8"?>

      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

                                       xmlns:s="library://ns.adobe.com/flex/spark"

                                       xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

        <fx:Declarations>

        <!-- Place non-visual elements (e.g., services, value objects) here -->

        </fx:Declarations>

       

        <fx:Script>

                          <![CDATA[

                                    import mx.events.SliderEvent;

                                    protected function ValueSlider_changeHandler(event:SliderEvent):void

                          {

                                    ValueLabel.text = String(ValueSlider.value);

                                              if(ValueSlider.value == "0")

                                              ValueLabel.text = "150";

                                              if(ValueSlider.value == "1")

                                              ValueLabel.text = "333";

                                              if(ValueSlider.value == "2")

                                              ValueLabel.text = "543";

                                              if(ValueSlider.value == "3")

                                              ValueLabel.text = "9342";

                          }

                          ]]>

        </fx:Script>

                <s:Panel x="199" y="141" width="250" height="200">

                          <s:HSlider id="ValueSlider" x="74" y="68" maximum="5" minimum="0" stepSize="1"/>

                          <s:Label id="ValueLabel" x="109" y="38" text="Label"/>

        </s:Panel>

      </s:Application>

        • 1. Re: How to resolve Comparison between a value with static type string and a possible unrelated type numb
          pauland Level 4

          Why are you quoting the numbers?

           

          Shouldn't you have

           

          if(ValueSlider.value == 0)

          ValueLabel.text = "150";

           

          ?

           

          In general, it's bad practice not to use braces with conditionals and use indentation - it's easy to make a mistake without them:

           

          if(ValueSlider.value == 0){

               ValueLabel.text = "150";

          }

           

          You can optimise this better, like this:

           

          const CONVERSION:Array = ["150","333","543","9342"];

          ValueLabel.text = CONVERSION[ValueSlider.value];

           

          It's also bad practice to name instances using a capital as the first letter - Class names start with capitals, instances with lower case, all caps for constants.

           

          Hope that helps.

          1 person found this helpful
          • 2. Re: How to resolve Comparison between a value with static type string and a possible unrelated type numb
            usoldier18 Level 1

            Thank you for your help, I was using quotes around the number from a suggestion on another forum. Now how to I display the result on valueLabel? "valueLabel.text"? {valueLable.text}, "const"?,

             

            <?xml version="1.0" encoding="utf-8"?>

            <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

                                             xmlns:s="library://ns.adobe.com/flex/spark"

                                             xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

              <fx:Declarations>

              <!-- Place non-visual elements (e.g., services, value objects) here -->

              </fx:Declarations>

             

              <fx:Script>

                                <![CDATA[

                                          import mx.events.SliderEvent;

                                          protected function ValueSlider_changeHandler(event:SliderEvent):void

                                {

                                                    const CONVERSION:Array = ["150","333","543","9342"];

                                                    valueLabel.text = CONVERSION[valueSlider.value];

                                }

                                ]]>

              </fx:Script>

                      <s:Panel x="199" y="141" width="250" height="200">

                                <s:HSlider id="valueSlider" x="78" y="69" maximum="3" minimum="0" stepSize="1" value="1"/>

              <s:Label id="valueLabel" x="109" y="38" text= "(valueLabel.text)"/>  <-----what do I place here?

              </s:Panel>

            </s:Application>

            • 3. Re: How to resolve Comparison between a value with static type string and a possible unrelated type numb
              pauland Level 4

              Take a look at the project below. I have modified your example. You can either monitor the slider changes via binding or explicitly handle events:

               

              <?xml version="1.0" encoding="utf-8"?>

               

              <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

                            

                             xmlns:s="library://ns.adobe.com/flex/spark"

                            

                             xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

                 

                  <fx:Declarations>

                     

                      <!-- Place non-visual elements (e.g., services, value objects) here -->

                 

                     

                  </fx:Declarations>

                 

                 

                 

                  <fx:Script>

               

                      <![CDATA[

                          const CONVERSION:Array = ["150","333","543","9342"];

                          import mx.events.SliderEvent;

                         

                          protected function valueSlider2ChangeHandler(event:Event):void

                             

                          {

                             

                             

                             

                              valueLabel2.text = CONVERSION[event.currentTarget.value];

                             

                          }

                         

                      ]]>

                     

                  </fx:Script>

                 

                  <s:Panel x="199" y="141" width="250" height="200">

                     

                      <s:HSlider id="valueSlider" x="78" y="69" maximum="3" minimum="0" stepSize="1" value="1" />

                      <s:HSlider id="valueSlider2" x="378" y="69" maximum="3" minimum="0" stepSize="1" value="1" change="valueSlider2ChangeHandler(event)"/>

                     

                      <s:Label id="valueLabel" x="109" y="38" text= "{CONVERSION[valueSlider.value]}"/>

                      <s:Label id="valueLabel2" x="409" y="38" text="'XXX'"/>

                     

                  </s:Panel>

              </s:Application>

              • 4. Re: How to resolve Comparison between a value with static type string and a possible unrelated type numb
                usoldier18 Level 1

                Thank you very much, very helpfull and I will definilty come here for more help when I get stuck again.