LiveCode
  • Why LiveCode?
  • Customers
  • Resources
  • Education
  • Consulting
  • Pricing
  • Blog
  • Login
  • Try LiveCode
Back to
  • slides
  • tutorial
Resources

Using Lists in the Library

Topic Progress:
← Back to Lesson

Using lists in the library

It will often be useful to return more that a single piece of data from a library handler.

Next we will update the sayHello handler to take a string containing a comma separated list of names, and return a LiveCode Builder list with an element containing a Hello message for each name passed in.

Updating the return type

The first step is to update the sayHello handler definition.

We will be returning a List rather than a String so update the return type.

We also want to be able to pass more than one name in so update the parameter name to pNames.

public handler sayHello(in pNames as String) returns List
    … widget code
end handler

Defining variables

We will be using 3 variables in the handler

  • tNameList – a list variable created from the pNames parameter
  • tMessage – the list variable that will be returned, with an element holding a messgae for each name passed in in pNames
  • tElement – a string variable used to iterate over the elements of tNameList

Add variable definitions for these 3 variables.

public handler sayHello(in pNames as String) returns List
  variable tNameList as List
  variable tMessage as List
  variable tElement as String

end handler

Creating a List from a String

We want to convert the String parameter into a List. We can do this using the split statement, which splits the string into a list of strings, using the specified delimiter.

We know the pNames parameter is a command separated list of names so we split pNames by comma to create a List variable.

public handler sayHello(in pNames as String) returns List
  variable tNameList as List
  variable tMessage as List
  variable tElement as String
 
  split pNames by "," into tNameList
end handler

Building a List

Next we want to create a list of messages, with an element for each name.

  • Loop over each element in tNameList
  • Construct a “Hello” message using the current element
  • Append it to the end of the list using the push statement.
  • Return the list of messages
public handler sayHello(in pNames as String) returns List
  variable tNameList as List
  variable tMessage as List
  variable tElement as String

  split pNames by "," into tNameList

  repeat for each element tElement in tNameList  
     push ("Hello" && tElement & "!") onto tMessage  
  end repeat  
  return tMessage 
end handler

Testing the library

Compile the library using the Extension Builder.

  • Open the Extension Builder from the Tools menu.
  • Load the helloWorldLibary.lcb file
  • Click Test.
  • This will load the library and create a test stack.
  • Add a button to the test stack.
  • Set the code of the button to
  • on mouseUp
     put sayHello("Adam,Brenda,Craig") into tArray
    end mouseUp
  • Add a breakpoint to the end mouseUp line so you can view the array in the Variable Watcher.

array4

Note: Remember that LCB lists are converted to numerically keyed arrays in LiveCode Script.

← Previous Topic

← Previous Topic Next Topic →
  •  
  •  
  • (0)
  •  

Expand All | Collapse All
Lessons Status
1 - Hello World Library
  • Introduction
  • Installing a Text Editor
  • Creating an LCB library
  • LCB library definition
  • LCB handler definitions
  • Compiling and testing a library in LiveCode
  • LCB metadata
  • Installing an LCB Library
  • Packaging and sharing an LCB library
  • Including an LCB library in a standalone
  • Conclusion
2 - Extended Hello World Library
  • Introduction
  • Explicit Types
  • LCB Lists and Arrays
  • Type Conversion between LCB and LiveCode Script
  • Using Explicit Typing in the Library
  • Passing Parameters to the Library
  • Using Lists in the Library
  • Documenting the Library
  • Browsing the Documentation
  • Conclusion
3 - Rotated Text Widget
  • Introduction
  • Defining a Widget
  • LCB Module Level Variables
  • Widget Properties
  • Widget Handlers
  • LCB Canvas Operations
  • Compiling and Testing the Widget
  • Installing the Widget and adding it to a Stack
  • Including the Widget in a Standalone
  • Conclusion
4 - Extended Rotated Text Widget
  • Introduction
  • Integrating Properties
  • Accessing LiveCode Control Properties
  • Handling Mouse Events
  • Saving and Loading Widgets
  • Read Only Properties
  • Documenting the Widget
  • Conclusion
5 - Pie Chart Widget
  • Introduction
  • Planning the Widget
  • Widget Properties
  • The OnCreate Handler
  • The OnPaint Handler
  • Responding to Mouse Events
  • Saving and Loading the Widget
  • Installing and Including the Widget in a Standalone
  • Documenting the Widget
  • Conclusion
6 - Modifying the Line Graph Widget
  • Introduction
  • Duplicating the Line Graph Widget
  • Additional Color Properties
  • Filling the area below the line
  • Adding graph marker mouse events
  • Conclusion
7 - Additional Resources
  • Extending LiveCode Guide
  • LiveCode Builder Language Reference
  • LiveCode Builder Style Guide
  • LiveCode Documentation Format
 

LCB Source

LCB Source
 

Login

LiveCode

  • Why LiveCode?
  • Pricing
  • Customer Stories
  • Extensions
  • LiveCode in Education
  • LiveCode in Business

Looking for LiveCode FileMaker?

LiveCode for FM

Resources

  • Docs
  • API (Language Dictionary)
  • Lessons
  • Sample Stacks
  • Forums
  • Stackoverflow
  • User Groups
  • Support
  • Directory
  • LiveCode Services

About

  • About
  • Meet The Team
  • Careers at LiveCode
  • Contact us

Recent Posts

  • 2 Releases: Deployment Begins With DP-7
  • LiveCode Classic 10.0.1 Stable and 10.0.2-rc-1 Released
  • Create Progress – the Three D’s
  • LiveCode Create: Closing out the year with big steps forward
  • Create dp-5: Unified Engines and Shared Extensions
Sitemap
Terms Privacy Policy EULA
Brought to you by LiveCode Ltd, Registered in Scotland, No. SC200728
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok