PDA

View Full Version : Types, Arrays, and Classes



Dan Cardin
10-08-2007, 07:10 PM
This tutorial has been converted to wiki format and is now hosted here.
Types Arrays and Classes (http://freddy1990.com/wiki/index.php?title=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 -
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
type DanCardin = record

end;
Now you're ready to add the variables in to the record to actually make it useful.
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.
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
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.
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
DanFlardin := DanCardin;

another thing to notify you of - you can also make arrays of records.
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
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
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 {name} = set of {type's name}; as a variable to declare it.
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:
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
type
TSunday = record
IceCream:string;
Topping:string;
end;
to have a class you simply do this
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
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
function TSunday.MakeSunday: string;
begin

end;

then you can do whatever you want, example-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)
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 thisSunday := TSunday.Create; in where you're using it. Then when you're done using the class you need to free it by doing 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 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 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- exampletype
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 (:D) http://img218.imageshack.us/img218/217/80978550kj2.png (http://imageshack.us)









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 [{number}..{higher number}] ofSo all together an example could be 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 - myarray[3] := 'hello';
Just to clarify, this would be wrongvar 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 correctvar 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:
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 myarray: array [4..33] of array [2..25] of integer; This picture should help with the understanding of the structure. http://i35.tinypic.com/2crlwgo.jpg
code example:
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
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 :eek:

to declare a dynamic array, you just leave out the length -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.

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.

ShowerThoughts
10-08-2007, 07:14 PM
nice work :)

bullzeye95
10-08-2007, 07:56 PM
What's up with your SCAR's text?

Dan Cardin
10-08-2007, 11:46 PM
what the number of lines? i dunno :( and i dont know how to change it

SKy Scripter
10-08-2007, 11:58 PM
aww you stoled my tut :(, ill still make one i guess :)

BobboHobbo
10-09-2007, 12:48 AM
Nice TUT :)

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

Dan Cardin
10-10-2007, 07:09 PM
i updated with arrays :)

edit: tuts finished with classes too

ZephyrsFury
10-11-2007, 01:18 AM
Nice. I now know about classes :D. BTW sundae is the correct spelling ;).

Jason2gs
10-11-2007, 02:32 AM
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 :p

Tim0suprem0
10-15-2007, 03:43 AM
Yay I think I get types now. Thanks :D Learning stuff again is funnn.

Dan Cardin
07-14-2008, 08:25 PM
/me updates tutorial, making it longer, with more topics and better quality.