Date and time related methods. DateTime is a predefined object. You should not declare it yourself. Date and time values are stored as ticks. Ticks are the number of milliseconds since January 1, 1970 00:00:00 UTC. This value is too large to be stored in an Int variable. It should only be stored in a Long variable. The methods DateTime.Date and DateTime.Time convert the ticks value to a string. You can get the current time with DateTime.Now. Example: DimnowAsLong now = DateTime.Now Msgbox("The date is: " & DateTime.Date(now) & CRLF & _
"The time is: " & DateTime.Time(now), "")
Returns a ticks value which is the result of adding the specified time spans to the given ticks value. Pass negative values if you want to subtract the values. Example: DimTomorrowAsLong Tomorrow = DateTime.Add(DateTime.Now, 0, 0, 1)
Log("Tomorrow date is: " & DateTime.Date(Tomorrow))
Date (TicksAsLong) AsString
Returns a string representation of the date (which is stored as ticks). The date format can be set with the DateFormat keyword. Example: Log("Today is: " & DateTime.Date(DateTime.Now))
Gets or sets the format used to parse date strings. See this page for the supported patterns: formats. The default pattern is MM/dd/yyyy (04/23/2002 for example).
DateParse (DateAsString) AsLong
Parses the given date string and returns its ticks representation. An exception will be thrown if parsing fails. Example: DimSomeTimeAsLong SomeTime = DateTime.DateParse("02/23/2007")
DateTimeParse (DateAsString, TimeAsString) AsLong
Parses the given date and time strings and returns the ticks representation.
DeviceDefaultDateFormatAsString [read only]
Returns the default date format based on the device selected language.
DeviceDefaultTimeFormatAsString [read only]
Returns the default time format based on the device selected language.
GetDayOfMonth (TicksAsLong) AsInt
Returns the day of month component from the ticks value. Values are between 1 to 31.
GetDayOfWeek (TicksAsLong) AsInt
Returns the day of week component from the ticks value. Values are between 1 to 7, where 1 means Sunday. You can use the AHLocale library if you need to change the first day.
GetDayOfYear (TicksAsLong) AsInt
Returns the day of year component from the ticks value. Values are between 1 to 366.
GetHour (TicksAsLong) AsInt
Returns the hour of day component from the ticks value. Values are between 0 to 23.
GetMinute (TicksAsLong) AsInt
Returns the minutes within a hour component from the ticks value. Values are between 0 to 59.
GetMonth (TicksAsLong) AsInt
Returns the month of year component from the ticks value. Values are between 1 to 12.
GetSecond (TicksAsLong) AsInt
Returns the seconds within a minute component from the ticks value. Values are between 0 to 59.
GetTimeZoneOffsetAt (DateAsLong) AsDouble
Returns the offset measured in hours from UTC at the specified date (offset can change due to daylight saving settings).
GetYear (TicksAsLong) AsInt
Returns the year component from the ticks value.
NowAsLong [read only]
Gets the current time as ticks (number of milliseconds since January 1, 1970).
Sets the application time zone. This setting affect the conversions of dates to ticks value and vice versa (device default settings are not changed).
Time (TicksAsLong) AsString
Returns a string representation of the time (which is stored as ticks). The time format can be set with the TimeFormat keyword. Example: Log("The time now is: " & DateTime.Time(DateTime.Now))
Gets or sets the format used to parse time strings. See this page for the supported patterns: formats. The default pattern is HH:mm:ss (23:45:12 for example).
TimeParse (TimeAsString) AsLong
Parses the given time string and returns its ticks representation. Note that the returned value date will be today.
TimeZoneOffsetAsDouble [read only]
Returns the current offset measured in hours from UTC.
Holds a thrown exception. You can access the last thrown exception by calling LastException. For example: Try DiminAsInputStream in = File.OpenInput(File.DirInternal, "SomeMissingFile.txt")
'... Catch Log(LastException.Message)
Creates a single dimension array of the specified type, or Object if the type is not specified. The syntax is: Array As type (list of values). Example: DimDays() AsString = ArrayAsString("Sunday", "Monday", ...)
Asc (CharAsChar) AsInt
Returns the unicode code point of the given character or first character in string.
Decodes the given bytes array as a string. Data - The bytes array. StartOffset - The first byte to read. Length - Number of bytes to read. CharSet - The name of the character set. Example: DimsAsString s = BytesToString(Buffer, 0, Buffer.Length, "UTF-8")
CallSub (ComponentAsObject, SubAsString) AsObject
Dynamically calls a sub based on the sub name. Note that unlike Basic4android CallSub is not required unless you need to call a sub based on the sub name.
Creates a new String by copying the characters from the array. Copying starts from StartOffset and the number of characters copied equals to Length.
Chr (UnicodeValueAsInt) AsChar
Returns the character that is represented by the given unicode value.
Stops executing the current iteration and continues with the next one.
Cos (RadiansAsDouble) AsDouble
Calculates the trigonometric cosine function. Angle measured in radians.
CosD (DegreesAsDouble) AsDouble
Calculates the trigonometric cosine function. Angle measured in degrees.
Creates a Map with the given key / value pairs. The syntax is: CreateMap (key1: value1, key2: value2, ...) Example: DimmAsMap = CreateMap("January": 1, "February": 2)
New line character. The value of Chr(10).
Returns the monitor scale, which is DPI / 96. (DPI stands for dots per inch).
Declares a variable. Syntax: Declare a single variable: Dim variable name [As type] [= expression] The default type is String.
Declare multiple variables. All variables will be of the specified type. Dim variable1 [= expression], variable2 [= expression], ..., [As type] Note that the shorthand syntax only applies to Dim keyword. Example:Dima = 1, b = 2, c = 3AsInt
Declare an array: Dim variable(Rank1, Rank2, ...) [As type] Example:DimDays(7) AsString The actual rank can be omitted for zero length arrays.
DipToCurrent (LengthAsInt) AsInt
Scales the value, which represents a specific length on a default density device (Density = 1.0), to the current device. For example, the following code will set the width value of this button to be the same physical size on all devices. Button1.Width = DipToCurrent(100)
Note that a shorthand syntax for this method is available. Any number followed by the string 'dip' will be converted in the same manner (no spaces are allowed between the number and 'dip'). So the previous code is equivalent to: Button1.Width = 100dip 'dip -> density independent pixel
Exits the most inner loop. Note that Exit inside a Select block will exit the Select block.
Immediately ends the application and stops the process.
Immediately ends the application and stops the process. The specified ExitCode will be returned as the process exit code (0 means no errors).
Files related methods.
Floor (NumberAsDouble) AsDouble
Returns the largest double that is smaller or equal to the specified number and is equal to an integer.
Syntax: For variable = value1 To value2 [Step interval] ... Next If the iterator variable was not declared before it will be of type Int.
Or: For Each variable As type In collection ... Next Examples: Fori = 1To10 Log(i) 'Will print 1 to 10 (inclusive). Next ForEachnAsIntInNumbers'an array Sum = Sum + n Next
Note that the loop limits will only be calculated once before the first iteration.
Gets the value of the system property mapped to the given key. Returns the DefaultValue parameter if there is no such property.
GetType (objectAsObject) AsString
Returns a string representing the object's java type.
Single line: If condition Then true-statement [Else false-statement] Multiline: If condition Then statement Else If condition Then statement ... Else statement End If
Tests whether the object is of the given type. Example: Fori = 0ToActivity.NumberOfViews - 1 IfActivity.GetView(i) IsButtonThen DimbAsButton b = Activity.GetView(i)
b.Color = Colors.Blue EndIf Next
IsDevTool (ToolNameAsString) AsBoolean
Returns true if ToolName equals B4J.
IsNumber (TextAsString) AsBoolean
Tests whether the specified string can be safely parsed as a number.
Returns the last exception that was caught (if such exists).
Logs a message to StdOut stream.
Logarithm (NumberAsDouble, BaseAsDouble) AsDouble
Logs a message. The message will only be logged in Debug mode.
Logs a message to StdErr stream.
Max (Number1AsDouble, Number2AsDouble) AsDouble
Returns the larger number between the two numbers.
For classes: returns a reference to the current instance. For activities and services: returns a reference to an object that can be used with CallSub, CallSubDelayed and SubExists keywords. Cannot be used in code modules.
Min (Number1AsDouble, Number2AsDouble) AsDouble
Returns the smaller number between the two numbers.
Converts the specified number to a string. The string will include at least Minimum Integers and at most Maximum Fractions digits. Example: Log(NumberFormat(12345.6789, 0, 2)) '"12,345.68" Log(NumberFormat(1, 3 ,0)) '"001"
Converts the specified number to a string. The string will include at least Minimum Integers, at most Maximum Fractions digits and at least Minimum Fractions digits. GroupingUsed - Determines whether to group every three integers. Example: Log(NumberFormat2(12345.67, 0, 3, 3, false)) '"12345.670"
Power (BaseAsDouble, ExponentAsDouble) AsDouble
Returns the Base value raised to the Exponent power.
Quote character. The value of Chr(34).
Regular expressions related methods.
Returns from the current sub and optionally returns the given value. Syntax: Return [value]
Rnd (MinAsInt, MaxAsInt) AsInt
Returns a random integer between Min (inclusive) and Max (exclusive).
Sets the random seed value. This method can be used for debugging as it allows you to get the same results each time.
Round (NumberAsDouble) AsLong
Returns the long number closest to the given number.
Calling this method causes the thread to start managing the message queue. This method should only be called in non-UI applications.
Calling this method will cause the thread to stop managing the message queue. This method should only be called in non-UI applications.
Declares a sub with the parameters and return type. Syntax: Sub name [(list of parameters)] [As return-type] Parameters include name and type. The lengths of arrays dimensions should not be included. Example: SubMySub (FirstNameAsString, LastNameAsString, AgeAsInt, OtherValues() AsDouble) AsBoolean ...
EndSub In this example OtherValues is a single dimension array. The return type declaration is different than other declarations as the array parenthesis follow the type and not the name (which does not exist in this case).
SubExists (ObjectAsObject, SubAsString) AsBoolean
Tests whether the object includes the specified method. Returns false if the object was not initialized or not an instance of a user class.
Tan (RadiansAsDouble) AsDouble
Calculates the trigonometric tangent function. Angle measured in radians.
TanD (DegreesAsDouble) AsDouble
Calculates the trigonometric tangent function. Angle measured in degrees.
Any exception thrown inside a try block will be caught in the catch block. Call LastException to get the caught exception. Syntax: Try ... Catch ... End Try
Declares a structure. Can only be used inside sub Globals or sub Process_Globals. Syntax: Type type-name (field1, field2, ...) Fields include name and type. Example: TypeMyType (NameAsString, Items(10) AsInt)
Dima, bAsMyType a.Initialize a.Items(2) = 123
Loops until the condition is true. Syntax: Do Until condition ... Loop
Loops while the condition is true. Syntax: Do While condition ... Loop
Strings are immutable in Basic4android, which means that you can change the value of a string variable but you cannot change the text stored in a string object. So methods like SubString, Trim and ToLowerCase return a new string, they do not change the value of the current string. Typical usage: DimsAsString s = "some text" s = s.Replace("a", "b") You can use StringBuilder if you need a mutable string. Note that string literals are also string objects: Log(" some text ".Trim)
Lexicographically compares the two strings. Returns a value less than 0 if the current string precedes Other. Returns 0 if both strings are equal. Returns a value larger than 0 if the current string comes after Other. Note that upper case characters precede lower case characters.
Returns a new string which is a substring of the original string. The new string will include the character at BeginIndex and will extend to the character at EndIndex, not including the last character.
Example: "012345".SubString2(2, 4) 'returns "23"
Returns a new string which is the result of lower casing this string.
Returns a new string which is the result of upper casing this string.
Returns a copy of the original string without any leading or trailing white spaces.
StringBuilder is a mutable string, unlike regular strings which are immutable. StringBuilder is especially useful when you need to concatenate many strings. The following code demonstrates the performance boosting of StringBuilder: DimstartAsLong start = DateTime.Now 'Regular string DimsAsString Fori = 1To5000 s = s & i Next Log(DateTime.Now - start)
'StringBuilder start = DateTime.Now DimsbAsStringBuilder sb.Initialize Fori = 1To5000 sb.Append(i)
Next Log(DateTime.Now - start) Tested on a real device, the first 'for loop' took about 20 seconds and the second took less then tenth of a second. The reason is that the code: s = s & i creates a new string each iteration (strings are immutable). The method StringBuilder.ToString converts the object to a string.
A Timer object generates ticks events at specified intervals. Using a timer is a good alternative to a long loop, as it allows the UI thread to handle other events and messages. Note that the timer events will not fire while the UI thread is busy running other code. The timer Enabled property is set to False by default. To make it start working you should change it to True.