Casting Vs The “as” operator

July 27, 2007

Just write a Flex app and try the following in a function

var btn:Button = new Button();

var lst:List = new List();

lst = List (btn);

In short, we are trying to cast a button into a list… What do you expect?? Yes… an RTE (Run Time Error)

It reads

TypeError: Error #1034: Type Coercion failed: cannot convert mx.controls::Button@16ab0479 to mx.controls.List.

There are times in your app development cycle, when you are not sure, if a particular casting is allowed or not. You definitely don’t want to throw an RTE (Run Time Error) to the user either. What you end up doing invariably is using a try…catch block to handle the RTE. A better and cleaner alternative to this is using the as operator

Using the as operator has the following advantages

  • It does the exact same thing as casting does, if you are casting between compatible types
  • It returns a null if the object cannot be casted to a particular type
  • No RTEs :)

Once you use the as operator, you can just do a null check to see if the casting is allowed… as below

var btn:Button = new Button;

var lst:List;

lst = btn as List;

if (! lst) {mx.controls.Alert.show(“Sorry you cant cast it this way”); }

But a word of caution. You cannot use the as operator to cast between types in the Top Level classes (to view them , go to the flex language reference). Which means… Say you want to cast a String to a Number, you cannot do,

num = str as Number;

This gives the following error

Implicit coercion of a value of type Number to an unrelated type String

You’ll have to do the age-old way of casting

num = Number(str);

Hope this is useful… at least to beginners :)


Skinning the Scrollbar in a ComboBox

July 25, 2007

Rajesh asked me this question today and I thought I’ll share with everyone…

I have done the following example using CSS that was generated from the Flex Style Explorer App

This is what this will achieve

This is how we can achieve it. We know that the dropDown on the ComboBox is actually a List. So if you can set the scrollbar onto the List and then set the List style onto the ComboBox, we are done.

  • The style on List verticalScrollBarStyleName can be used to skin the List’s scrollbar
  • Once this is done, use the dropdownStyleName style on ComboBox to style the dropDown List

As simple as that :)

Let’s look at the CSS now.

.myVScrollBar {
cornerRadius: 16;
highlightAlphas: 0, 0;
fillColors: #0099ff, #0033ff, #000000, #ffffff;
trackColors: #000000, #ffffff;
themeColor: #009dff;
}

.myList {
cornerRadius: 10;
verticalScrollBarStyleName: myVScrollBar
}

A CSS style declaration called myVScrollBar is used to skin the scrollbar (generated from the Style Explorer App). Another style declaration called myList is defined and here, as you see we nest the style declarations, by setting myVScrollBar to the verticalScrollBarStyleName style of the List.

Once this CSS file is saved (as say, test.css), just do the following to get this onto your App

< mx:Style source=”test.css”/>

<mx:ComboBox dataProvider=”{cards}” dropdownStyleName=”myList”/>

Enjoy :)


Getting on the Flex Highway – How to get up to speed with Flex (done)

June 20, 2007

Thank you all for attending the seminar…

Sorry about the glitches with the audio. I guess the bandwidth got chocked.

The seminar was in 2 parts.

  1. Introduction to Flex
  2. Get up to speed with Flex

To view the recording and download the presentation go to this link (and see the 3rd Seminar). It links to the flexapacseminars project on google code. The audio seems to have a bit of lag…


Getting on the Flex Highway – How to get up to speed with Flex

June 18, 2007

I would like to invite you all to a this week’s Flex APAC Tech Wednesday eSeminar. We have it scheduled on 20th June 2007. At 4pm IST.
You can check for the local time at your city by clicking this link: World Clock…

Topic : Getting on the Flex Highway – How to get up to speed with Flex

The session is a non-technical one… intended for people who are getting started with Flex and covers the set of resources that they can tap into for the same… It intends to collate the diverse set of resources available on the net for this purpose…

Duration : I hope to finish in 60 mins :)

Connect URL : http://my.adobe.acrobat.com/flexapacseminar/


Flex APAC eSeminar files on Google Code

June 7, 2007

During the last seminar, people complained of having lots of problems accessing the URLs of the eSeminars recordings & files. I thought we could collate it a single place. So I have gone ahead and created a Flex APAC Seminars Google Code Project which will host all the resources used in the eSeminars.

You can access the following information there…

  • Use the Wiki tab to find what each of the eSeminars contained. It has a wholesome info on what the seminar was on, when it was done, the host and the topic. It also provides links to the recordings and the zipped archives of the files & resources used in the seminar.
  • Use the Download tab to directly download the files (you can also go to the wiki and download from the link there)

For convenience, I have split the wiki into pages, each having information on 10 eSeminars. You can use this to access all the resources connected to each of the seminars. Do send me if you have any suggestions on this.


Flex APAC eSeminar series 2 – AS3 for OO Programmers

June 7, 2007

We had the second edition of the Flex APAC eSeminar Series yesterday. Harish from the Flex team here took the session on “AS3 for OO Programmers”. I was there with him to help with the logistics and to take the questions when Harish was doing the presentation.

It went off very well, much more successful than the initial trial run. We had a total attendance of 67 people, though mostly from India. Moving forward, we want the rest of APAC to actively participate too. We are working towards that.

Harish covered basics of AS3 and some nuances like converting an MXML code to AS and the component lifecycle workflow and things. I think it he did a great job. What’s more, there was a lot of interaction with and within the community which was very encouraging. Follow the following links for more details

  1. Link to the recording of the connect session
  2. Link to the resources/ppts/files, used in the presentation

Looking forward to meeting more of you in the seminars to come :)


JSON to ASON

June 2, 2007

I have blogged about JSON before but forgot to provide an example for getting JSON into AS. I saw a query on FlexIndia on JSON, so I thought I’ll post it now. The problem is to convert JSON to ActionScript Objects (or ASON). You can first try the demo application to see if this is what you are looking for…

In the app, you can either paste a URL (provided you have the security permissions) of a JSON to convert or paste your own JSON into the textarea and convert it.

This is what you need to do to get this working…

  1. Go to AS3CoreLib on Google Code and download the corelib-.90.zip file and extract it
  2. Get the corelib.swc from the /bin directory
  3. Download the app source or write your own code :)
  4. Do one of the following:
    If you are using FlexBuilder:

    1. Right click on your project go to Properties
    2. Navigate to Flex Build Path -> Libraries Tab -> Add SWC
    3. Now add the corelib.swc to your project
    4. Confirm that you have the line “import com.adobe.serialization.json.JSON;” in the code
    5. Run and Have Fun :)

    If you are using the command-line compiler:

    1. Place the swc in the directory of your mxml code
    2. Confirm that you have the line “import com.adobe.serialization.json.JSON;” in the code
    3. Type the following on the command line
    4. mxmlc -include-libraries=corelib.swc JSONTest.mxml
    5. Run and Have Fun :)

Follow

Get every new post delivered to your Inbox.