Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Classes in Delphi 7

Status
Not open for further replies.

GiovanniCiurleoKing

Programmer
Apr 30, 2004
2
GB
Hello,

I am trying to write a class in Delphi 7. The class definition looks like this:


unit Unit6;

interface


type TModule = class
StudentName: string;
CourseName: string;
YearsInCourse: integer;
CurrentYear: integer;
CurrentModule: integer;
Code: array [1..12] of string;
Title: array [1..12] of string;
Leader: array [1..12] of string;
Credits: array [1..12] of integer;
CWWeight: array [1..12] of integer;
ExamWeight: array [1..12] of integer;
CWMark: array [1..12] of integer;
ExamMark: array [1..12] of integer;

public
// modifier methods
procedure SetStudentName(sName: string);

end;


implementation

procedure TModule.SetStudentName(sName: string);
begin
StudentName := sName;
end;

etc

The unit (Unit2) for the form that uses this class has Unit6 in the 'uses' section and declares a global instance of the TModule class called Modules.

In one of the form's event handlers, it tries to call Modules.SetStudentName(edit1.text) the program crashes saying there has been an access violation.

If I set a breakpoint in Unit2 where it calls Modules.SetStudentName and trace into it, the access violation is thrown within Unit6 - TModule.SetStudentName itself where it tries to set StudentName.

If I use the watch tool to try and view the class variable StudentName it says its an inaccesable value, which is of course the problem.

The confusing thing is, I have seen a program (which does something else) which looks identical to mine. This runs fine but mine does not.

I'm completely stumped as to the cause of the problem. I'm sure it's something really silly I've missed. If it is, please don't laugh I'm new to Delphi lol

Any advice would be appreciated.

Thanks

Giovanni Ciurleo
 
Forgive me if I'm going down the wrong track but it sounds like you haven't created your object.

I added the new unit into the form's uses list. I then added a unit variable just above the implementation part of the unit:
Code:
  var
    Modules: TModule;

I placed the following line in the Form's FormCreate event:
Code:
  Modules := TModule.Create;

I placed the following code in the Form's FormDestroy event:
Code:
  Modules.Free;

and finally tried to set the student name from a button event:
Code:
  Modules.SetStudentName('Clive');

I had no access violations and the SetStudentName method worked as expected.

Hope this helps!

Clive [infinity]
 
You also might want to put the keyword "private" at the top so it would look something like this:
Code:
type
   TModule = class(TObject)
   private
       StudentName: string;
   ....

The "TObject" thing is implicit, but I like to put it in to make it clear what I am descending from.

Another "Delphi thing" is to expose class members using "properties":

Code:
type
   TModule = class(TObject)
   private
       FStudentName: string;    // "F" is for "Field"
       procedure SetStudentName(Value : String);   // mutator
   public
       property StudentName : String read FStudentName write SetStudentName;
   end;

implementation
...
  ThisModule.StudentName := AName;


Cheers
 
I would also add a constructor to your class to ensure every member variable is initialised. You can have a constructor that initialises variables to default values, and/or a parametised constructor that you pass values to, to be assigned to the member variables. Below, I'll show you how to implement the parametised constructor (integrating Richard's code) e.g.
Code:
interface

  type
    TModule = class(TObject)
    private
      fStudentName: String;
      fCourseName: String;
      fCurrentYear: Integer;
      ...
    public
      constructor Create(AStudentName, ACourseName: String; ACurrentYear: Integer); // add extra params here
      ...
    end;

implementation

constructor TModule.Create(AStudentName, ACourseName: String; ACurrentYear: Integer);
begin
  fStudentName := AStudentName;
  fCourseName := ACourseName;
  fCurrentYear := ACurrentYear;
end;

...

Then when you first create the object you would do it like this:
Code:
  Modules := TModule.Create('Giovanni Ciurleo',
                            'Computer Science',
                            '2');

Let me know if you want more information about classes and constructors. Some good resource you might be interested in are outlined in the following FAQ I wrote: faq102-2794

Hope this helps!

Clive [infinity]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top