Part 11 - Structs


Added by Cameron Kenneth Knight, last edited by Bot Buider on Sep 11, 2005  (view change)

Labels

 
(None)

Part 11 - Structs

Definition: Struct

Short for structure, a term meaning a data group made of related variables.

The main difference between structs are different than classes is that it is a value type instead of a reference type. This means that whenever you return this value, or set one equal to another, it is actually copying the data not a reference to the data. This is handy, becaues if it is declared without a value, it will default to something besides null. It also cannot be compared to null. This eliminates alot of error checking associated with reference types.

Structs also cannot inherit from classes, nor can classes inherit from structs. Structs can however, inherit from interfaces.

Unlike some other languages, structs can have methods.

Declaring a Struct

Declaring a struct is very similar to declaring a {{class}, except that the name is changed.

declaring a struct
struct Coordinate:
    def constructor(x as int, y as int):
        _x = x
        _y = y
    
    _x as int
    _y as int

c as Coordinate
print c.x, c.y
c = Coordinate(3, 5)
print c,x, c.y
Output
0 0
3 5

Here you can see that the struct was instanced without being called, showing the how a struct is a value.

Exercises

  1. Figure out a good exercise for this section.

Go on to Part 12 - Namespaces

  1. Nov 07, 2005

    Michel Casabianca says:

    Sample code should be: 8< struct Coordinate: def constructor(x as int, y as in...

    Sample code should be:

    -8<-----------------------------------------------------------
    struct Coordinate:
    def constructor(x as int, y as int):
    _x = x
    _y = y

    _x as int
    _y as int

    c as Coordinate
    print c._x, c._y
    c = Coordinate(3, 5)
    print c._x, c._y
    -8<-----------------------------------------------------------