<html><head><title>Structures</title></head><body bgcolor="#FFFFFF" link="#207BC3" vlink="#003B83" alink="#003B83"><font face="Arial" size="2"><p align="center"><b><font size="5">Structures</font></b></p><br><b>Syntax</b><blockquote><pre><font face="Courier New, Courier, mono"size="2"><b><font color="#207BC3">Structure</font></b> <name> [<b><font color="#207BC3">Extends</font></b> <name>]...<b><font color="#207BC3">EndStructure</font></b></font></pre></blockquote><b>Description</b><br><blockquote><b><font color="#207BC3">Structure</font></b> is useful to define user type, and access some OS memory areas. Structures can be usedto enable faster and easier handling of data files. It is very useful as you can group into the sameobject the information which are common. Structures fields are accessed with the <b><font color="#207BC3">\</font></b> option. Structurescan be nested. Statics arrays are supported inside structures. <br><br>Dynamic objects like arrays, lists and maps are supported inside structure and are automatically initializedwhen the object using the structure is created. To declare such field, use the following keywords:<b><font color="#207BC3">Array</font></b>, <b><font color="#207BC3">List</font></b> and <b><font color="#207BC3">Map</font></b>. <br><br>The optional <b><font color="#207BC3">Extends</font></b> parameter allows to extends another structure with new fields. All fieldsfound in the extended structure will be available in the new structure and will be placed beforethe new fields. This is useful to do basic inheritance of structures. <br><br><a href="compilerfunctions.html">SizeOf</a> can be used with structures to get the size of the structure and <a href="compilerfunctions.html">OffsetOf</a> can be used to retrievethe index of the specified field. <br><br>Please note, that in structures a static array[] doesn't behave like the normal BASIC array (defined using <a href="dim.html">Dim</a>)to be conform to the C/C++/JavaScript structure format (to allow direct API structure porting). This means that a[2] willallocate an array from 0 to 1 where Dim a(2) will allocate an array from 0 to 2.<br><br>When using <a href="memory.html">pointers</a> in structures, the '*' has to be omitted when using the field, once more to ease API code porting. It can beseen as an oddity (and to be honest, it is) but it's like that since the very start of SpiderBasic and many, many sources relyon that so it won't be changed.<br><br>When using a lot of structure fields you can use the <a href="with_endwith.html">With</a> : <a href="with_endwith.html">EndWith</a>keywords to reduce the amount of code to type and ease its readability.<br><br>It's possible to perform a full structure copy by using the equal affectation between two structure element of the same type.<br><br><a href="compilerfunctions.html">ClearStructure</a> can be used to clear a structured memory area. It's for advanced use only, when<a href="memory.html">pointers</a> are involved. <br></blockquote><p><b>Example</b></p><blockquote><pre><font face="Courier New, Courier, mono"size="2"> <b><font color="#207BC3">Structure</font></b> PersonName.sForName.sAge.w<b><font color="#207BC3">EndStructure</font></b><b><font color="#207BC3">Dim</font></b> <font color="#207BC3">MyFriends</font>.Person(100)<font color="#207BC3">; Here the position '0' of the array MyFriend()</font><font color="#207BC3">; will contain one person and it's own information</font><font color="#207BC3"> MyFriends</font>(0)\Name = "Andersson"<font color="#207BC3"> MyFriends</font>(0)\Forname = "Richard"<font color="#207BC3"> MyFriends</font>(0)\Age = 32</font></pre></blockquote><p><b>Example:</b> A more complex structure (Nested and static array)</p><blockquote><pre><font face="Courier New, Courier, mono"size="2"> <b><font color="#207BC3">Structure</font></b> Window*NextWindow.Window <font color="#207BC3">; Points to another window object</font>x.wy.wName.s[10] <font color="#207BC3">; 10 Names available (from 0 to 9)</font><b><font color="#207BC3">EndStructure</font></b></font></pre></blockquote><p><b>Example:</b> Extended structure</p><blockquote><pre><font face="Courier New, Courier, mono"size="2"> <b><font color="#207BC3">Structure</font></b> MyPointx.ly.l<b><font color="#207BC3">EndStructure</font></b><b><font color="#207BC3">Structure</font></b> MyColoredPoint <b><font color="#207BC3">Extends</font></b> MyPointcolor.l<b><font color="#207BC3">EndStructure</font></b>ColoredPoint.MyColoredPoint\x = 10ColoredPoint.MyColoredPoint\y = 20ColoredPoint.MyColoredPoint\color =<font color="#207BC3"> RGB</font>(255, 0, 0)</font></pre></blockquote><p><b>Example:</b> Structure copy</p><blockquote><pre><font face="Courier New, Courier, mono"size="2"> <b><font color="#207BC3">Structure</font></b> MyPointx.ly.l<b><font color="#207BC3">EndStructure</font></b>LeftPoint.MyPoint\x = 10LeftPoint\y = 20RightPoint.MyPoint = LeftPoint<b><font color="#207BC3">Debug</font></b> RightPoint\x<b><font color="#207BC3">Debug</font></b> RightPoint\y</font></pre></blockquote><p><b>Example:</b> Dynamic object</p><blockquote><pre><font face="Courier New, Courier, mono"size="2"> <b><font color="#207BC3">Structure</font></b> PersonName$Age.l<b><font color="#207BC3">List</font></b> <font color="#207BC3">Friends$</font>()<b><font color="#207BC3">EndStructure</font></b>John.PersonJohn\Name$ = "John"John\Age = 23<font color="#207BC3">; Now, add some friends to John</font><font color="#207BC3">;</font><font color="#207BC3"> AddElement</font>(John\<font color="#207BC3">Friends$</font>())John\<font color="#207BC3">Friends$</font>() = "Jim"<font color="#207BC3"> AddElement</font>(John\<font color="#207BC3">Friends$</font>())John\<font color="#207BC3">Friends$</font>() = "Monica"<b><font color="#207BC3">ForEach</font></b> John\<font color="#207BC3">Friends$</font>()<b><font color="#207BC3">Debug</font></b> John\<font color="#207BC3">Friends$</font>()<b><font color="#207BC3">Next</font></b></font></pre></blockquote><p><b>Example:</b> Pointers</p><blockquote><pre><font face="Courier New, Courier, mono"size="2"> <b><font color="#207BC3">Structure</font></b> Person*Next.Person <font color="#207BC3">; Here the '*' is mandatory to declare a pointer</font>Name$Age.b<b><font color="#207BC3">EndStructure</font></b>Timo.Person\Name$ = "Timo"Timo\Age = 25Fred.Person\Name$ = "Fred"Fred\Age = 25Timo\Next = @Fred <font color="#207BC3">; When using the pointer, the '*' is omitted</font><b><font color="#207BC3">Debug</font></b> Timo\Next\Name$ <font color="#207BC3">; Will print 'Fred'</font></font></pre></body></html>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。