Xojo Examples

Posted : admin On 1/13/2022

From Xojo Documentation

Used to specify the text encoding of a String.

Properties
Methods
ChrIsValidData
EqualsOperator_Compare

Notes

When a computer stores text, it encodes each character as a numeric value and stores the byte (or bytes) associated with that number. When it needs to display or print that character, it consults the encoding scheme to determine which character the number represents.

To create a class in Xojo use the Insert menu or toolbar and choose Class. These instances are what you interact with when writing code on the window and is what the user interacts with when they use your app. For example, when you drag a TextArea from the Library to a Window, you create a usable instance of the TextArea on the Window. We provide many resources to help you get started learning to make native, cross-platform apps with the Xojo programming language and IDE. From videos to tutorials, documentation and example projects, you'll have your first app ready very quickly. Download Xojo 2021r1.1 Released: April 22nd, 2021. Develop native apps for macOS, Windows, Linux, Web, iOS and Raspberry Pi. View release notes or EULA.

The first computers used the encoding scheme called 'ASCII', which stands for American Standard Code for Information Interchange. It specified 128 values and includes codes for upper and lower case letters, numbers, the common symbols on a keyboard, and some 'invisible' control codes that were heavily used in early computers.

Xojo Draw: A sample iOS application made with the Xojo programming language. This repository contains the full Xojo source code for Xojo Draw so.

As computers became more sophisticated and were introduced in non-English speaking countries, the limitations of the ASCII encoding scheme became apparent. It didn’t include codes for accented characters and had no chance of handling idiographic languages, such as Japanese or Chinese, which require thousands of characters.

As a result, extensions to the ASCII encoding scheme were developed. Outside the range of 0-127, the schemes, in general, do not agree. For example, in the US macOS and Windows computers use different encodings for codes 128-255. Many other encoding schemes for handling languages that use non-ASCII characters have been developed.

The most general solution to the problem is an encoding called Unicode. It is designed to handle every character in every language. It also enables you to represent a mixture of languages within one text stream. However, not all strings that you may encounter use Unicode.

When you encounter a string, you need to know its encoding in order to interpret the sequence of bytes (or double-bytes) that make up the string's content. By default, every string contains both the bytes (content) and the encoding (if it is known; it is Nil if not known). Two different formats of Unicode are supported: UTF-8 and UTF-16. All strings in your project are compiled as UTF-8. This is a Unicode encoding that uses one byte for ASCII characters and up to four bytes for non-ASCII characters.

If you work only with strings that are created and managed within your own application, you probably don't need to deal with encodings directly, as the issues are taken care of by everything using UTF-8. However, if you receive strings from an outside source such as via the internet, an external database (that is, not SQLite), or a text file, you should let specify what encoding is used. If the string is a Memoryblock, the encoding will be Nil.

You can assign an encoding to a string in several ways. For example, if you are reading the string using the TextInputStream class, you use the Encoding property. The Encodings module gives you access to all known encodings. Here is an example that reads a text file that uses the UTF8 encoding:

Var f As FolderItem
Var t As TextInputStream
f = FolderItem.ShowOpenFileDialog('text') // file type defined as as File Type
If f <> NilThen
t = TextInputStream.Open(f)
t.Encoding = Encodings.UTF8 // specify encoding of input stream
TextArea1.Value = t.ReadAll
t.Close
End If

Also, the Read, ReadLine, and ReadAll methods take an optional parameter that lets you specify the encoding.

If you need to output a string in a specific encoding, you can use the ConvertEncoding function to do so. For example, this code converts the text in a TextField to the WindowsANSI encoding:

Var s AsString
s = TextField1.Value.ConvertEncoding(Encodings.WindowsANSI)

You will find text encoding helpful if you develop:

  • Internet applications, such as web browsers or e-mail applications
  • Applications that transfer text across different platforms
  • Applications based in Unicode

The Encoding function makes it easy to obtain the TextEncoding of any string. Use the Encodings module to obtain a specified text encoding. Some of the most useful are UTF8, UTF16, UTF32, ASCII, MacRoman, MacJapanese, and WindowsLatin1. Use the Autocomplete feature of the Code Editor to view the complete list.

ASCII Codes

The following table presents the ASCII character codes. It presents the Decimal, Hex, and Octal values for ASCII codes (0 to 127).

DecimalHexOctalResultDecimalHexOctalResult
000NUL322040SP
111SOH332141!
222STX342242'
333ETX352343#
444EOT362444$
555ENQ372545%
666ACK382646&
777BEL392747'
8810BS402850(
9911HT412951)
10A12LF422A52*
11B13VT432B53+
12C14FF442C54,
13D15CR452D55-
14E16SO462E56.
15F17SI472F57/
161020DLE4830600
171121DC14931611
181222DC25032622
191323DC35133633
201424DC45234644
211525NAK5335655
221626SYN5436666
231727ETB5537677
241830CAN5638708
251931EM5739719
261A32SUB583A72:
271B33ESC593B73;
281C34FS603C74<
291D35GS613D75=
301E36RS623E76>
311F37US633F77?
6440100@9660140'
6541101A9761141a
6642102B9862142b
6743103C9963143c
6844104D10064144d
6945105E10165145e
7046106F10266146f
7147107G10367147g
7248110H10468150h
7349111I10569151i
744A112J1066A152j
754B113K1076B153k
764C114L1086C154l
774D115M1096D155m
784E116N1106E156n
794F117O1116F157o
8050120P11270160p
8151121Q11371161q
8252122R11472162r
8353123S11573163s
8454124T11674164t
8555125U11775165u
8656126V11876166v
8757127W11977167w
8858130X12078170x
8959131Y12179171y
905A132Z1227A172z
915B133[1237B173{
925C1341247C174
935D135]1257D175}
945E136^1267E176~
955F137_1277F177DEL

Examples

The following example obtains the TextEncoding of the string passed to the Encoding function.

Var t As TextEncoding
t = Encoding(TextArea1.Value)
If t <> Nilthen
Label1.Value = 'Base=' + t.Base.ToString
Label2.Value = 'Format=' + t.Format.ToString
Label3.Value = 'Variant=' + t.Variant.ToString
End If

The following statement uses the Encodings module to obtain the UTF8 text encoding for text in a TextField.

TextField2.Value = DefineEncoding(TextField1.Value, Encodings.UTF8)

The following example uses the Chr method to obtain the character corresponding to the code point of 165 for the MacRoman encoding, the bullet character (•):

See Also

Chr, ConvertEncoding, DefineEncoding, Encoding, GetInternetTextEncoding, GetTextConverter, GetTextEncoding functions; Encodings module.

Retrieved from 'http://docs.xojo.com/index.php?title=TextEncoding&oldid=78407'

From Xojo Documentation

  • 1Understanding Classes, Instances and References
  • 3Adding Properties to Classes
  • 4Adding Methods to Classes

In its simplest form, a class is a container of code and data much like a module. But unlike a module, a class provides better code reuse. Classes are the fundamental building blocks of object-oriented programming.

Classes offer many benefits, including:

  • Reusable Code
    When you directly add code to a PushButton on a Window to customize its behavior, you can only use that code with that one PushButton. If you want to use the same code with another PushButton, you need to copy the code and then make changes to the code in case it refers to the original PushButton (since the new PushButton will have a different name than the original).
    Classes store the code once and refer to the object (like the PushButton) generically so that the same code can be reused any number of times without modification. If you create a class based on the PushButton control and then add your code to that class, any usage (instances) of that custom class will have that code.
  • Smaller Projects and Apps
    Because classes allow you to store code once and use it over and over in a project, your project and the resulting app is smaller in size and may require less memory.
  • Easier Code Maintenance
    Less code means less maintenance. If you have basically the same code copied in several places of your projects, you have to keep that in mind when you make changes or fix bugs. By storing one copy of the code, when you need to make changes, you'll spend less time tracking down all those places in your project where you are using the same code. Changes to the code in a class are automatically used anywhere where the class is used.
  • Easier Debugging
    The less code you have, the less code there is to debug.
  • More Control
    Classes give you more control than you can get by simply adding code to the event handlers of a control in a window. You can use classes to create custom controls. And with classes, you have the option to create versions that don’t allow access to the source code of the class, allowing you to create classes you can share or sell to others.

Understanding Classes, Instances and References

Before you can use a class in your project, it is important to understand the distinction between these three concepts: the class itself, the instance of the class and the reference to the class.

The Class: Think of the class as a template for a container of information (code and data), much like a module. And like a module, each class exists in your project only once. But unlike a module, a class can have multiple instances that each contain different data.

The Instance: Classes provide better code reuse because of a concept called instances. Unlike a Module, which exists only once in your running app, a class can have multiple instances. Each instance (also called an object) is a separate and independent copy of the class and all its methods and properties.

The Class

There are many built-in classes for user interface controls. Some commonly used ones are PushButton, WebButton, MobileButton, Label, WebLabel, MobileLabel, TextField, WebTextField, MobileTextField, ListBox, WebListBox and MobileTable.

By themselves, control classes are not all that useful; they are just abstract templates. But each time you add a control class to a layout (a Window, WebPage or MobileScreen) you get an instance of the class. Because each button is a separate instance, this is what allows you to have multiple buttons on a Window, with each button being completely independent of each other with its own settings and property values.

This is an example of a Vehicle class that will be used in later examples:

Class Vehicle
Property Brand AsString
Property Model AsString
EndClass
Xojo examples

Note that this above code is for reference. You do not actually write code like this to create a class. To create a class in Xojo use the Insert menu or toolbar and choose Class.

The Instance

These instances are what you interact with when writing code on the window and is what the user interacts with when they use your app.

For example, when you drag a TextArea from the Library to a Window, you create a usable instance of the TextArea on the Window. The new instance has all the properties and methods that were built into the TextArea class. You get all that for free — styled text, multiple lines, scroll bars, and all the rest of it. You customize the particular instance of the TextArea by modifying the values of the instance’s properties.

When you add a control to a window (or web page), the Layout Editor creates the reference for you automatically (it is the name of the control).However, when writing code you create instances of classes using the New keyword. For example, this create a new instance of a Vehicle class that is in your project:

The Reference

A reference is a variable or property that refers to an instance of a class. In the above code example, the car variable is a reference to an instance of the Vehicle class.

You interact with properties and methods of the class using dot notation like this:

Var car AsNew Vehicle
car.Brand = 'Ford'
car.Model = 'Focus'

Here the Brand and Model were defined as properties of the Vehicle class and they are given values for the specific instance. Having an instance is important! If you try to access a property or method of a class without first having an instance, you will get a NilObjectException likely causing your app to quit. This is is one of the most common programming errors that people make and is typically caused by forgetting the New keyword. For example, this code might look OK at first glance:

Var car As Vehicle
car.Brand = 'Ford'
car.Model = 'Focus'

But the 2nd line will result in a NilObjectException when you run it because car is not actually an instance. It is just a variable that is declared to eventually contain a Vehicle instance. But since an instance was not created, it gets the default value of Nil, which means it is empty or undefined.

If you are not sure if a variable contains an instance, you can check it before you use it by using the Nil keyword like this:

If car <> NilThen
car.Brand = 'Ford'
car.Model = 'Focus'
End If

When the variable is not Nil, then it has a valid reference. And since it is a reference, it has important considerations when assigning the variable to another variable: When you do an assignment from one reference variable to another, the second variable points to the same reference as the first variable.

This means the second variable refers to the same instance of the class as the first variable and is not a copy of it. So if you change a property of the class with either variable, then the property is changed for both. An example might help demonstrate this:

Var car AsNew Vehicle
car.Brand = 'Ford'
car.Model = 'Focus'
Var truck As Vehicle
truck = car
// truck.Model is now 'Focus'
car.Model = 'Mustang'
// truck.Model is now also 'Mustang'
truck.Model = 'F-150'
// car.Model is now also 'F-150'

In this diagram you can see that the variables for both car and truck point to the same instance of Vehicle. So changing either one effectively changes both.

Two variables pointing at the same object

If you want to create a copy of a class, you need to instead create a new instance (using the New keyword) and then copy over its individual properties as shown below:

Var car AsNew Vehicle
car.Brand = 'Ford'
car.Model = 'Focus'
Var truck AsNew Vehicle
truck.Brand = car.Brand
trunk.Model = car.Model
// truck.Model is now also 'Focus'
truck.Model = 'F-150'
// car.Model remains 'Focus'

When you do it this way, you get two separate instances. Changes to one do not affect the other as you can see in these diagrams:

Two variables each pointing at different objects, both of which are instances of the same class.

Adding Classes

To add a Class to your Xojo project, use the Insert menu or toolbar and choose Class. Your class will appear in the Navigator.

Note: You do not directly create classes in code. They are always added using Insert as described above.

Adding Properties to Classes

Properties are variables that belong to an entire class instance rather than just a single method. To add a property to a class, use the Add button on the Code Editor toolbar, Insert ↠ Property from the menu, the contextual menu or the keyboard shortcut (Option-Command-P on macOS or Ctrl+Shift+P on Windows and Linux). You can set the property name, type, default value and scope using the Inspector.

Xojo Examples For Children

To quickly create a property, you can enter both its name and type on one line in the Name field like this: PropertyName As DataType. When you leave the field, the type will be set in the Type field.

Properties added in this manner are sometimes called Instance Properties because they can only be used with an instance of the class. You can also add properties that can be accessed through the class itself without using an instance. These are called Shared Properties.

Shared Properties

A shared property (sometimes called a Class Property) is like a “regular” property, except it belongs to the class, not an instance of the class. A shared property is global and can be accessed from anywhere its scope allows. In many ways, it works like a module property.

It is important to understand that if you change the value of a shared property, the change is available to every usage of the shared property.

Generally speaking, shared properties are an advanced feature that you only need in special cases. For example, if you are using an instance of a class to keep track of items (e.g., persons, merchandise, sales transactions, and so forth) you can use a shared property as a counter. Each time you create or destroy an instance of the class, you can increment the value of the shared property in its constructor and decrement it in its destructor. (For information about constructors and destructors, see the section Constructors and Destructors.) When you access it, it gives you the current number of instances of the class.

Adding Methods to Classes

To add a method to a class, use the Add button on the Code Editor toolbar, Insert ↠ Method from the menu, the contextualmenu or the keyboard shortcut (Option-Command-M on macOS or Ctrl+Shift+M on Windows and Linux). You can set the method name, parameters, return type and scope using the Inspector.

When typing the method name, the field will autocomplete with the names of any methods on its super classes.

Methods added in this manner are called Instance Methods because they can only be used with an instance of the class.

Xojo Download

You can also add methods that can be accessed through the class itself. These are called Shared Methods.

Shared Methods

A shared method (sometimes called a Class Method) is like a normal method, except it belongs to the class, not an instance of the class. A shared method is global and can be called from anywhere its scope allows. In many ways, it works like a module method.

Shared Methods do not know about an instance so its code can only access other shared methods or shared properties of the class.

Generally speaking, shared methods are an advanced feature that you only need in special cases. A common usage of shared methods is to create an instance (rather than relying on a Constructor).

See Also

Object-Oriented Programming topic

Xojo Programming Examples

Retrieved from 'http://docs.xojo.com/index.php?title=UserGuide:OOP_Classes&oldid=77807'