Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Android Application Development
This is the forum to discuss the Wrox book Beginning Android Application Development by Wei-Meng Lee; ISBN: 978-1-1180-1711-1
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Android Application Development section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 2nd, 2011, 07:09 PM
Registered User
Points: 9, Level: 1
Points: 9, Level: 1 Points: 9, Level: 1 Points: 9, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Smile Ch1 P.37 Code Mystery

protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
return new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("This is a dialog with some simple text...");


The above code fragment has a syntax I do not recognize.
First, I always thought that a return statement immediately returns from the method.

I get the impression that the new AlertDialog.Builder Constructor returns an object that is then referenced. However, I see the .setIcon method and the .setTitle methods placed in a manner similar to the visual basic with statement.


Can someone explain what's going on?
Reply With Quote
  #2 (permalink)  
Old August 28th, 2011, 02:49 PM
Registered User
Points: 26, Level: 1
Points: 26, Level: 1 Points: 26, Level: 1 Points: 26, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2011
Location: Los Angeles
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Default you are correct in your understanding

John,

You are correct, the return will return right away. What's going on here is that each

.setSOMETHING

statement is setting various properties of the object AlertDialog, as the book mentioned:
"You set the various properties, such as icon, title, and buttons, as well as checkboxes:"
Hope this makes sense.
Reply With Quote
  #3 (permalink)  
Old September 2nd, 2011, 11:26 AM
Registered User
Points: 9, Level: 1
Points: 9, Level: 1 Points: 9, Level: 1 Points: 9, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Smile Possible Explanation of How the P.37 Code Works!

I also found out after researching the AlertDialog.Builder class that the methods of the class return a reference to the same instance of the AlertDialog.Builder class. So in effect, you've got a chain of method calls, each using the returned reference.

I wrote the following test to demonstrate how this works:
Code:
public class C1 {
	public C1(){
		System.out.println("C1 Constructor Called");
	}
	
	public C1 M1(){
		System.out.println("M1 Method Called");
		return(this);
	}
	
	public C1 M2(){
		System.out.println("M2 Method Called");
		return(this);
	}
	
	public C1 M3(){
		System.out.println("M3 Method Called");
		return(this);
	}
	
	public C1 create(){
		return(this);
	}
}



public class test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		C1 cONE = doTest();
		System.out.println("Call method M3 via returned reference!");
		cONE.M3();
	}
	
	static public C1 doTest(){
		int choice = 0;
		
		switch(choice){
		case 0:
			return new C1() 
				.M1()
				.M2()
				.M3()
				.create();
		}
		return null;
	}
	
}
Each method of the C1 class returns this - which is the key to being able to chain the methods in the switch statement above. The conventional way to write the switch statement would have been as follows:
Code:
switch(choice){
   case 0:
	C1 cOne = new C1(); 
	cOne.M1();
	cOne.M2();
	cOne.M3();
	cOne.create();
	return cOne;
}
Whether the new way is more efficient than the conventional way is the question? Anyone have an opinion?
Reply With Quote
  #4 (permalink)  
Old September 2nd, 2011, 05:11 PM
Registered User
Points: 34, Level: 1
Points: 34, Level: 1 Points: 34, Level: 1 Points: 34, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2011
Posts: 8
Thanks: 0
Thanked 1 Time in 1 Post
Default

So the function of return is to a) return control to the caller b) start a new thread of execution in which the remaining code is executed?

Alternatively, return is setting the value to be returned when the time comes, which is when the code in the procedure is exhausted?

Just a new boy making wild guesses.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Single Typo in 'Final' VB Code - causes 37 errors irelandk BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 May 3rd, 2009 03:30 PM
Ch.5, Hashtable code mystery Vodnik BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 0 January 8th, 2008 06:11 PM
String Mystery binishkhan87 C# 3 October 6th, 2007 05:19 PM
error mystery htummala Oracle 2 February 28th, 2005 02:28 AM



All times are GMT -4. The time now is 10:46 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.