Results 1 to 11 of 11

Thread: Types, Arrays, and Classes

  1. #1
    Join Date
    Apr 2007
    Posts
    3,152
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default Types, Arrays, and Classes

    This tutorial has been converted to wiki format and is now hosted here.
    Types Arrays and Classes

    Types
    - Record
    - Type
    - Set
    Arrays
    - Static Array
    - Dynamic Array

    Records
    In Scar you name the record by declaring it much like a variable, except its a type, so you write "type" instead of "var". Then you write the name and equal it to a record. Kind of an iffy explanation, i know, so -
    SCAR Code:
    type {name} = record
    Now that you have declared the record, you should add an "end;" to the end so as not to forget it when you add your variables inside. So your code should look like this
    SCAR Code:
    type DanCardin = record

    end;
    Now you're ready to add the variables in to the record to actually make it useful.
    SCAR Code:
    type Dan = record
      fName, lName: string;
      height, age: byte;
      aliases: array of string;
    end;
    You just declare variables inside your record to give it properties that you can later use.

    Now you're done. Its time to actually use the record. Lets start by creating a Dan and giving it properties.
    SCAR Code:
    type Dan = record
      fName, lName: string;
      height, age: byte;
      aliases: TStringArray;
    end;

    var DanCardin: Dan;
        i: integer;
    begin
      danCardin.fname := 'Sean';
      danCardin.lname := 'McUin';
      danCardin.height := 6;
      danCardin.age := 15;
      danCardin.aliases := ['Dan Cardin', 'BigBlackBlue', 'DanCardin', 'Chuck Norris'];
      Writeln('Hey! im ' + dancardin.fname + ' ' + dancardin.lname + '. Im ' + inttostr(dancardin.height) + 'ft tall and am ' + inttostr(dancardin.age) + ' years old.');
      Writeln('My aliases are: ');
      for i := 0 to high(dancardin.aliases) do
        Writeln(dancardin.aliases[i]);
    end.
    To actually set your variables equal to something, you'll write the name your your Dan variable (DanCardin) and put a "." then the name of the variable you want to access. This is called dot syntax. If you dont want to have to write "dancardin.this" and "dancardin.that" everywhere then you can make it simpler by doing this
    SCAR Code:
    type Dan = record
      fName, lName: string;
      height, age: byte;
      aliases: TStringArray;
    end;

    var DanCardin: Dan;
        i: integer;
    begin
      with dancardin do
      begin
        fname := 'Sean';
        lname := 'McUin';
        height := 6;
        age := 15;
        aliases := ['Dan Cardin', 'BigBlackBlue', 'DanCardin', 'Chuck Norris'];
      Writeln('Hey! im ' + fname + ' ' + lname + '. Im ' + inttostr(height) + 'ft tall and am ' + inttostr(age) + ' years old.');
      Writeln('My aliases are: ');
      for i := 0 to high(aliases) do
        Writeln(aliases[i]);
      end;
    end.
    Now thats all nice and cool, but lets say you want to make yourself another Dan. You're not satisfied with just DanCardin, you want DanFlardin too! well since you've made yourself a nice little Dan variable, you can! all you do is add yourself another name in your variable declaration.
    SCAR Code:
    var DanCardin, DanFlardin: Dan;
    There is another interesting thing you can do, if DanCardin and DanFlardin just happen to be exactly the same(maybe they're twins!). Instead of redeclaring all of those variables you can simply do this
    SCAR Code:
    DanFlardin := DanCardin;

    another thing to notify you of - you can also make arrays of records.
    SCAR Code:
    type Dan = array of record
      fName, lName: string;
      height, age: byte;
      aliases: TStringArray;
    end;

    var DanCardin: Dan;

    {is the same thing as...}

    type Dan = record
      fName, lName: string;
      height, age: byte;
      aliases: TStringArray;
    end;

    var DanCardin: array of Dan;

    Thats just the basics of what you can do with records, but there is much much more





    Types
    A Set is a variable, but you are the one to decide what can be held in that variable. for example
    SCAR Code:
    type
      color = (red, orange, green, purple, blue);
    Theres not much to elaborate on since its a variable and...yeh. the best use i can think of for it is to limit the input able to be given in a procedure. like this
    SCAR Code:
    type
      food = (lobster, swordfish, tuna, wood, sandwhich);

    procedure eatFood(kind: food);
    begin
      {code for eating}
    end;




    Sets
    The Set keyword defines an set type for up to 255 discrete values. A set variable always holds all set values - some are set on, some are set off. That being said, the only use i know of for them is to extend the use of a type. With a set you can have multiple variables in your type, kind of making it like an array.
    you just add
    SCAR Code:
    {name} = set of {type's name};
    as a variable to declare it.
    SCAR Code:
    type
      food = (lobster, swordfish, tuna, wood, sandwhich);

    var foods: set of food;

    procedure eatALotOfFood;
    begin
      if lobster in foods then
        {eat lobster}
      else
      if swordfish in foods then
        {eat swordfish}
      else
      if tuna in foods then
        {eat tuna}
      else
      {etc}
    end;

    begin
      foods := [lobster, swordfish, tuna];
      eatALotOfFood;
    end.
    "in" is what you use to check if the specified value is in the set. example:
    SCAR Code:
    type
      TNumber = (Ace, One, Two, Three, Four, Five, Siz, Seven, Eight,
      Nine, Ten, Jack, Queen, King);

    var
      CourtCards: Set of TNumber;
      CardNumbers : array[1..4] of TNumber;
      i : Integer;

    begin
      CourtCards := [Ace, Jack, Queen, King];

      CardNumbers[1] := Ace;
      CardNumbers[2] := Four;
      CardNumbers[3] := Jack;
      CardNumbers[4] := Seven;

      for i := 1 to 4 do
        if (CardNumbers[i] in CourtCards) then
          Writeln('Card '+IntToStr(i)+' is a face card');
    end.




    Classes(in delphi)
    (cuz 99% of tuts here are scar so i am helping non scar people)

    Not that u can use them in scar but for other people its a tut.



    Classes are pretty much an extension of records ....ok now...

    open up a console app and make a type....*waiting*

    so here I have my type
    SCAR Code:
    type
      TSunday = record
        IceCream:string;
        Topping:string;
      end;
    to have a class you simply do this
    SCAR Code:
    type
      TSunday = class(TObject)
        IceCream:string;
        Topping:string;
      end;
    Now to use it for something you might add a function to the class like this
    SCAR Code:
    type
      TSunday = class(TObject)
        IceCream:string;
        Topping:string;
        Function MakeSunday:string;
      end;
    And now you would press ctrl+shift+c and delphi will automatically set it up for you by putting it in
    SCAR Code:
    function TSunday.MakeSunday: string;
    begin

    end;

    then you can do whatever you want, example-
    SCAR Code:
    type
      TSunday = record
        IceCream:string;
        Topping:string;
        Function MakeSunday:string;
      end;

    { TSunday }

    function TSunday.MakeSunday: string;
    begin
      result:= IceCream + ' icecream with ' + Topping + ' on top!';
    end;
    Now you would add a global variable of type <name of your type>(mine is tSunday)
    SCAR Code:
    var Sunday:TSunday;

    Now to use your class you need to create it because it is an object, because you cant use a class without creating it

    So to create your class you would put this
    SCAR Code:
    Sunday := TSunday.Create;
    in where you're using it. Then when you're done using the class you need to free it by doing
    SCAR Code:
    Sunday.free;
    and now a good habit to get into would be, putting the code you use in between the create and the free, into a try finally block. That will make sure that whatever happens in code in the try finally block, the finally will always happen. So you would need to do
    SCAR Code:
    begin
      Sunday := TSunday.Create;
      try

      finally
        Sunday.Free;
      end;
    end.
    the stuff in between the try and the finally is the code you want it to do and in between the finally and the end is where you put Free so that the memory is always freed.

    Now you can add code in between the try and finally to make the class do something
    SCAR Code:
    type
      TSunday = class(Tobject)
        IceCream:string;
        Topping:string;
        Function MakeSunday:string;
      end;

    { TSunday }

    function TSunday.MakeSunday: string;
    begin
      result:= IceCream + ' icecream with ' + Topping + ' on top!';
    end;

    var Sunday: TSunday;

    begin
      Sunday := TSunday.Create;
      try
        Sunday.IceCream := 'Vanilla';
        Sunday.Topping := 'Sprinkles';
        Writeln('You have ' + Sunday.MakeSunday);
      finally
        Sunday.Free;
      end;
      Readln; //so you can see it
    end.

    Now that is pretty much the basics of classes, but i have one more thing to show you(not really teach just to show and let u play around with it if u want to)

    you can have classes of other classes- example
    SCAR Code:
    type
      TSunday = class(Tobject)
        IceCream:string;
        Topping:string;
        Function MakeSunday:string;
      end;

      TUltraSunday = class(TSunday)
        SecondTopping:string;
        Marshmellows:Boolean;
      end;
    this means that and ultra sunday will be able to have a type of ice cream and topping just like in a regular sunday, but its also gonna have a second topping and a choice of marshmellows. It will Inherit that functionality =D(inheritance is a big OOP thing).

    to prove that its inherited if its too unbelievable ()








    Arrays!


    An array is pretty much a list of whatever type of data ur using(like a list of strings or a list of integers)

    There are 2 kinds of arrays

    • Static Arrays
    • Dynamic Arrays


    Static Arrays

    A static array is basically a list of {insert variable type here} that's index can be accesses by the numbers that you supply.

    Declaring an array is just like declaring a variable with a slight difference. first off, you add the word "array" in front of the variable type that you want to be an array. Then between the type and "array" you put
    SCAR Code:
    [{number}..{higher number}] of
    So all together an example could be
    SCAR Code:
    var
      CoolArray: Array[0..7] of integer;
    Now assigning a value to one of the indexes of the array is just like assigning a value to a variable, except you put two brackets [] with the index number you want. example -
    SCAR Code:
    myarray[3] := 'hello';
    Just to clarify, this would be wrong
    SCAR Code:
    var myarray: array [0..3] of string;
    begin
      myarray[5] := 'ddfasd';
    end;
    because 5 goes out of the range of 0 - 3, whereas this, would be correct
    SCAR Code:
    var myarray: array [0..3] of string;
    begin  myarray[2] := 'ddfasd';
    end;

    Then to use the array you could do something along the lines of this:
    SCAR Code:
    var coolarray : array [0..3] of integer;
    begin
      CoolArray[0] := 5;
      CoolArray[1] := 73;
      CoolArray[2] := 134;
      CoolArray[3] := 34;
    Now what i just showed u was a single dimension array. There is also a multidimensional array. to make an array multidimensional you just add "array of [] again for each dimension of the array you want(filling in the high and low of the indexes of course. example
    SCAR Code:
    myarray: array [4..33] of array [2..25] of integer;
    This picture should help with the understanding of the structure.
    code example:
    SCAR Code:
    var  
      MultiDimension: array[0..2] of array[0..2] of string;//see?

    begin
      MultiDimension[0][0]:= 43;
      MultiDimension[0][1]:= 3;
      MultiDimension[0][2]:= 1;
      MultiDimension[1][0]:= 2;
      MultiDimension[1][1]:= 3;
      MultiDimension[1][2]:= 4;
      MultiDimension[2][0]:= 5;
      MultiDimension[2][1]:= 6;
      MultiDimension[2][2]:= 7;
    end;

    And if you wanted to show your array easily you could use the 2 functions low and high in a for to do statement. which i can show you
    SCAR Code:
    var
      CoolArray: Array[0..7] of integer;
      i:integer;
    begin
      for i := 0 to high(myarray) do
        myarray[i] := i * 10;  //to save space
      for i:= low(MyArray) to High(MyArray) do
      begin
        Writeln(inttostr(MyArray[i]));
      end;
    end;

    Dynamic Arrays


    while static arrays are set in their size perminately, Dynamic Arrays can be changed at runtime

    to declare a dynamic array, you just leave out the length -
    SCAR Code:
    var
      Dynamic:array of integer;//so u just take out the length of the array :)
    but to be able to use your array later on, you have to set the length at runtime.

    SCAR Code:
    var
    Dynamic: array of string;

    begin  
      SetArrayLength(Dynamic{name of array}, 4{length});
      Dynamic[0]:= 'woah';
      Dynamic[1]:= 'cool';
      Dynamic[2]:= 'amazing';
      Dynamic[3]:= 'woot!';
    end;
    the only problem one may have with dynamic arrays is that you cannot choose what your starting number is. That is a small price to pay however to the usefullness of a dynamic array.
    SCAR Tutorials: The Form Tutorial | Types, Arrays, and Classes
    Programming Projects: NotePad | Tetris | Chess


  2. #2
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,553
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    nice work
    ~Hermen

  3. #3
    Join Date
    Jun 2006
    Posts
    3,861
    Mentioned
    3 Post(s)
    Quoted
    0 Post(s)

    Default

    What's up with your SCAR's text?

  4. #4
    Join Date
    Apr 2007
    Posts
    3,152
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    what the number of lines? i dunno and i dont know how to change it
    SCAR Tutorials: The Form Tutorial | Types, Arrays, and Classes
    Programming Projects: NotePad | Tetris | Chess


  5. #5
    Join Date
    Dec 2006
    Location
    utah
    Posts
    1,427
    Mentioned
    2 Post(s)
    Quoted
    7 Post(s)

    Default

    aww you stoled my tut , ill still make one i guess
    Co Founder of https://www.tagcandy.com

  6. #6
    Join Date
    May 2007
    Location
    NSW, Australia
    Posts
    2,823
    Mentioned
    3 Post(s)
    Quoted
    25 Post(s)

    Default

    Nice TUT

    Its good that alot of people are making TUT's now, gotta love em.

  7. #7
    Join Date
    Apr 2007
    Posts
    3,152
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    i updated with arrays

    edit: tuts finished with classes too
    SCAR Tutorials: The Form Tutorial | Types, Arrays, and Classes
    Programming Projects: NotePad | Tetris | Chess


  8. #8
    Join Date
    Apr 2007
    Location
    Perth, Australia
    Posts
    3,926
    Mentioned
    3 Post(s)
    Quoted
    2 Post(s)

    Default

    Nice. I now know about classes . BTW sundae is the correct spelling .

  9. #9
    Join Date
    Dec 2006
    Location
    Third rock from the sun.
    Posts
    2,510
    Mentioned
    4 Post(s)
    Quoted
    1 Post(s)

    Default

    Aww, I saw the name and for a split second I thought SCAR supported class types

    Maybe in a future version, eh?

    Nice tut, and you made me hungry for some ice cream

  10. #10
    Join Date
    Oct 2006
    Posts
    1,071
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    Yay I think I get types now. Thanks Learning stuff again is funnn.

  11. #11
    Join Date
    Apr 2007
    Posts
    3,152
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    /me updates tutorial, making it longer, with more topics and better quality.
    SCAR Tutorials: The Form Tutorial | Types, Arrays, and Classes
    Programming Projects: NotePad | Tetris | Chess


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Types and Arrays
    By nielsie95 in forum Outdated Tutorials
    Replies: 22
    Last Post: 03-07-2012, 09:49 AM
  2. Arrays, stuck on arrays
    By Camaro' in forum OSR Help
    Replies: 1
    Last Post: 03-08-2008, 02:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •