0

I want to add a method to an existing Delphi class. I think basic class framework OK but need to access some properties of the object that called my method in my method. I can't seem to get anything to work.

old class TStringGrid

new class OptStringGrid where myNewMethod is referenced

//example of new class method
procedure myNewMethod (const Name: string);
begin
  //Here is my question location.
  // I would like to access properties of the calling object in this case
  // testgrid. Like... i:= testgrid.rowcount;
end 

// Unit Calling statements
var
  testGrid : OptStringGrid;
  i: integer;
begin
  i := testgrid.myNewMethod(strName);
end;

New to Delphi, forgive my terminology if wrong please. I know example code not compilable. I'm looking for techniques to access the properties as described.

1 Answer 1

7

To access members of the object whose method is executing, you can use the Self variable. It's automatically declared and assigned inside any method body. In fact, its use is usually implicit — any members of the object are automatically in scope inside the method body. You generally only need to qualify member access with Self when there is already some other variable in the method that has the same name as the member you wish to use.

The key thing about implementing methods is that you need to make sure they're actually methods. The code shown in the question does not define myNewMethod as a method. Rather, it's a standalone subroutine. Only methods can be called on objects, and therefore only methods can have access to the objects they're called on.

A method declaration appears inside a class declaration. Yours might look something like this:

type
  TOptStringGrid = class(TStringGrid)
  public
    function myNewMethod(const Name: string): Integer;
  end;

A method definition appears in the implementation section of your unit, along with all your other subroutine bodies, just like all the event-handler implementations the IDE creates for you when you double-click events in the Object Inspector. Those are just ordinary methods.

What distinguishes a method implementation from an implementation of some other kind of subroutine is that the method name includes the name of the class it belongs to:

function TOptStringGrid.myNewMethod(const Name: string): Integer;
begin
  // ...
end;

Observe the TOptStringGrid. portion in the code above. That's how the compiler knows that the method body belongs to that class and not anything else named myNewMethod.

Within that method body, you can access all the published, public, and protected members of the ancestor class, TStringGrid, including the RowCount property.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.