I'm new to pascal so bear with me. I've been learning pascal for a little over a month and have had lots of growing pains as I've never been an OOP programmer. Which is where all my frustrations are coming from.
So I'm trying to make a stack class of my own for learning purposes and also because I need something that allows me to drop off stuff at the end of the stack. Its one of many brain teasers for my feeble mind.
When the code gets to line 46 it fails while trying to assign 0 to the first element in the array. If I'm not mistaken at this moment in time I haven't changed the size of the array so that's all that should exist. Of course it could be that the array is non existent at this point and the constructor is trying to write to something that's not there.
I can't get past setting the stackArr[0] to zero.
program StackQueue;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes
{ you can add units after this };
Type
{ TmyStack }
TmyStack = class(Tobject)
public
element: integer;
indexNum: integer;
stackArr: array of integer;
constructor Create;
procedure setSize(itsNewLength: integer);
procedure setIndex(IndexNumber: integer);
procedure push(IndexNumber: integer);
function pop(IndexNumber: integer): integer;
end;
constructor TmyStack.Create;
begin
inherited Create;
// Set the initial index number to 0
indexNum := 0; // Probably don't need to do this, but doing it anyways.
setIndex(indexNum);
end;
{ TmyStack }
procedure TmyStack.setSize(itsNewLength: integer);
begin
setLength(stackArr, itsNewLength);
end;
procedure TmyStack.setIndex(IndexNumber: integer);
begin
stackArr[0] := IndexNumber;
end;
procedure TmyStack.push(IndexNumber: integer);
begin
end;
function TmyStack.pop(IndexNumber: integer): integer;
begin
end;
var
myStack: TmyStack;
begin
myStack := TmyStack.Create;
try
// Set the lengh of the array
myStack.setSize(25);
finally
mystack.Free;
end;
writeln('Hit any key to end.');
readln;
end.
I'm having all kinds of brain farts as to how I can be accessing things within the class from other procedures and functions and all so I apologize if I've really messed it up.
But the question is why can't I assign to index 0 a value? What am I doing wrong?