Author Topic: Types, Arrays, Classes  (Read 2975 times)

Offline Dan Cardin

  • Magical Yams
  • Global Moderator
  • *****
  • Posts: 417
  • Rep: 0
  • I is am are you!
    • MSN Messenger - thelonelydingo33@hotmail.com
    • AOL Instant Messenger - YoYoPotatoTomato
    • View Profile
    • DanCardin
    • Email
Types, Arrays, Classes
« on: December 31, 2007, 03:35:14 pm »
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]type {name} = record[/scar]
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]type DanCardin = record

end;[/scar]
Now you're ready to add the variables in to the record to actually make it useful.
[scar]type Dan = record
  fName, lName: string;
  height, age: byte;
  aliases: array of string;
end;[/scar]
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]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);
end.[/scar]
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]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);
  end;
end.[/scar]
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]var DanCardin, DanFlardin: Dan;[/scar]
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]DanFlardin := DanCardin;[/scar]

another thing to notify you of - you can also make arrays of records.
[scar]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;[/scar]

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]type
  color = (red, orange, green, purple, blue);[/scar]
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]type
  food = (lobster, swordfish, tuna, wood, sandwhich);

procedure eatFood(kind: food);
begin
  {code for eating}
end;[/scar]




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]{name} = set of {type's name};[/scar] as a variable to declare it.
[scar]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.[/scar]
"in" is what you use to check if the specified value is in the set. example:
[scar]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 in CourtCards) then
      Writeln('Card '+IntToStr(i)+' is a face card');
end.[/scar]




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]type
  TSunday = record
    IceCream:string;
    Topping:string;
  end;[/scar]
to have a class you simply do this
[scar]type
  TSunday = class(TObject)
    IceCream:string;
    Topping:string;
  end; [/scar]
Now to use it for something you might add a function to the class like this
[scar]type
  TSunday = class(TObject)
    IceCream:string;
    Topping:string;
    Function MakeSunday:string;
  end;[/scar]And now you would press ctrl+shift+c and delphi will automatically set it up for you by putting it in
[scar]function TSunday.MakeSunday: string;
begin

end;[/scar]

then you can do whatever you want, example-[scar]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;[/scar] Now you would add a global variable of type <name of your type>(mine is tSunday)
[scar]var Sunday:TSunday;[/scar]

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]Sunday := TSunday.Create;[/scar] in where you're using it. Then when you're done using the class you need to free it by doing [scar]Sunday.free;[/scar]
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]begin
&nbsp; Sunday := TSunday.Create;
&nbsp; try

&nbsp; finally
&nbsp; &nbsp; Sunday.Free;
&nbsp; end;
end.[/scar]
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]type
&nbsp; TSunday = class(Tobject)
&nbsp; &nbsp; IceCream:string;
&nbsp; &nbsp; Topping:string;
&nbsp; &nbsp; Function MakeSunday:string;
&nbsp; end;

{ TSunday }

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

var Sunday: TSunday;

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

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]type
&nbsp; TSunday = class(Tobject)
&nbsp; &nbsp; IceCream:string;
&nbsp; &nbsp; Topping:string;
&nbsp; &nbsp; Function MakeSunday:string;
&nbsp; end;

&nbsp; TUltraSunday = class(TSunday)
&nbsp; &nbsp; SecondTopping:string;
&nbsp; &nbsp; Marshmellows:Boolean;
&nbsp; end;[/scar]
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 (:D)
 







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][{number}..{higher number}] of[/scar]So all together an example could be [scar] var
&nbsp; CoolArray: Array[0..7] of integer;[/scar]
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]myarray[3] := 'hello';[/scar]
 Just to clarify, this would be wrong[scar]var myarray: array [0..3] of string;
begin
&nbsp; myarray[5] := 'ddfasd';
end;[/scar] because 5 goes out of the range of 0 - 3, whereas this, would be correct[scar]var myarray: array [0..3] of string;
begin&nbsp; myarray[2] := 'ddfasd';
end;[/scar]

Then to use the array you could do something along the lines of this:[scar]
var coolarray : array [0..3] of integer;
begin
&nbsp; CoolArray[0] := 5;
&nbsp; CoolArray[1] := 73;
&nbsp; CoolArray[2] := 134;
&nbsp; CoolArray[3] := 34;[/scar]
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]myarray: array [4..33] of array [2..25] of integer;[/scar] This picture should help with the understanding of the structure.
code example:
[scar]var&nbsp;
&nbsp; MultiDimension: array[0..2] of array[0..2] of string;//see?

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

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]var
&nbsp; CoolArray: Array[0..7] of integer;
&nbsp; i:integer;
begin
&nbsp; for i := 0 to high(myarray) do
&nbsp; &nbsp; myarray := i * 10;&nbsp; //to save space
&nbsp; for i:= low(MyArray) to High(MyArray) do
&nbsp; begin
&nbsp; &nbsp; Writeln(inttostr(MyArray));
&nbsp; end;
end;[/scar]

Dynamic Arrays

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

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

[scar]var
Dynamic: array of string;

begin&nbsp;
&nbsp; SetArrayLength(Dynamic{name of array}, 4{length});
&nbsp; Dynamic[0]:= 'woah';
&nbsp; Dynamic[1]:= 'cool';
&nbsp; Dynamic[2]:= 'amazing';
&nbsp; Dynamic[3]:= 'woot!';
end;[/scar]
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.
« Last Edit: January 24, 2009, 07:49:01 am by Dan Cardin »

Freddy1990.com

Types, Arrays, Classes
« on: December 31, 2007, 03:35:14 pm »

Offline ti.teg.tnod.I

  • The Best
  • Administrator
  • *****
  • Posts: 3233
  • Rep: 10
  • ^-^
    • View Profile
    • Rainbows In A Fire
Re: Types, Arrays, Classes
« Reply #1 on: January 01, 2008, 04:00:25 pm »
This is a SCAR area so Classes seems a little useless. Other than that good job!

Freddy1990.com

Re: Types, Arrays, Classes
« Reply #1 on: January 01, 2008, 04:00:25 pm »

Offline Dan Cardin

  • Magical Yams
  • Global Moderator
  • *****
  • Posts: 417
  • Rep: 0
  • I is am are you!
    • MSN Messenger - thelonelydingo33@hotmail.com
    • AOL Instant Messenger - YoYoPotatoTomato
    • View Profile
    • DanCardin
    • Email
Re: Types, Arrays, Classes
« Reply #2 on: January 01, 2008, 05:31:47 pm »
i didnt see a delphi tut section, and to know classes you have to know types really so it kinda fits in this tut :) thanks for reading it

Offline ti.teg.tnod.I

  • The Best
  • Administrator
  • *****
  • Posts: 3233
  • Rep: 10
  • ^-^
    • View Profile
    • Rainbows In A Fire
Re: Types, Arrays, Classes
« Reply #3 on: January 02, 2008, 12:12:23 pm »
i didnt see a delphi tut section, and to know classes you have to know types really so it kinda fits in this tut :) thanks for reading it

Lol no problem you could post Delphi stuff (Like tuts) in the Delphi programming section of the forum.

DaBomber

  • Guest
Re: Types, Arrays, Classes
« Reply #4 on: January 02, 2008, 01:38:53 pm »
Isn't that the same one that's on the SRL-Forums?

Freddy1990.com

Re: Types, Arrays, Classes
« Reply #4 on: January 02, 2008, 01:38:53 pm »

Offline ti.teg.tnod.I

  • The Best
  • Administrator
  • *****
  • Posts: 3233
  • Rep: 10
  • ^-^
    • View Profile
    • Rainbows In A Fire
Re: Types, Arrays, Classes
« Reply #5 on: January 03, 2008, 06:29:29 am »
Isn't that the same one that's on the SRL-Forums?

Is it? Could you figure that out for me please? If so...Then this tutorial is not welcome.

DaBomber

  • Guest
Re: Types, Arrays, Classes
« Reply #6 on: January 03, 2008, 07:20:46 am »
can't, i'm still at school >.< maybe later

Freddy1990.com

Re: Types, Arrays, Classes
« Reply #6 on: January 03, 2008, 07:20:46 am »

Offline ti.teg.tnod.I

  • The Best
  • Administrator
  • *****
  • Posts: 3233
  • Rep: 10
  • ^-^
    • View Profile
    • Rainbows In A Fire
Re: Types, Arrays, Classes
« Reply #7 on: January 03, 2008, 07:39:57 am »
can't, i'm still at school >.< maybe later

Ok, I always get lost looking for something on the SRL Forums so I'll just wait.

DaBomber

  • Guest
Re: Types, Arrays, Classes
« Reply #8 on: January 03, 2008, 08:54:29 am »
lol, srl-forums.com --> forums --> tutorial island

Offline ti.teg.tnod.I

  • The Best
  • Administrator
  • *****
  • Posts: 3233
  • Rep: 10
  • ^-^
    • View Profile
    • Rainbows In A Fire
Re: Types, Arrays, Classes
« Reply #9 on: January 03, 2008, 09:30:24 am »
Lol!....But there are so many tuts :'(, I'll leave it to the professional ;).

DaBomber

  • Guest
Re: Types, Arrays, Classes
« Reply #10 on: January 03, 2008, 09:33:56 am »
not really, i'm still not even a junior member there >.<

Offline ti.teg.tnod.I

  • The Best
  • Administrator
  • *****
  • Posts: 3233
  • Rep: 10
  • ^-^
    • View Profile
    • Rainbows In A Fire
Re: Types, Arrays, Classes
« Reply #11 on: January 03, 2008, 09:35:36 am »
not really, i'm still not even a junior member there >.<

O.o, to become a JR. Member you just need 10 posts and 7 days of activity.

DaBomber

  • Guest
Re: Types, Arrays, Classes
« Reply #12 on: January 03, 2008, 09:37:46 am »
yeah i know that >.<

Offline Dan Cardin

  • Magical Yams
  • Global Moderator
  • *****
  • Posts: 417
  • Rep: 0
  • I is am are you!
    • MSN Messenger - thelonelydingo33@hotmail.com
    • AOL Instant Messenger - YoYoPotatoTomato
    • View Profile
    • DanCardin
    • Email
Re: Types, Arrays, Classes
« Reply #13 on: January 03, 2008, 11:08:35 am »
i cant post my own tutorial here and there? Some people like Dabomber apparently havent been at srl long or dont go there at all. So whats the problem with me posting here too?

http://www.srl-forums.com/forum/types-arrays-and-t20521.html btw

DaBomber

  • Guest
Re: Types, Arrays, Classes
« Reply #14 on: January 03, 2008, 12:27:32 pm »
I knew that it was posted there but i did not know who posted there which is why i made a comment about it. yes if you wrote it then you can post it.

Freddy1990.com

Re: Types, Arrays, Classes
« Reply #14 on: January 03, 2008, 12:27:32 pm »