Recent

Author Topic: Basic questions  (Read 1447 times)

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • Knowledge is the key
Basic questions
« on: May 28, 2020, 08:15:45 am »
Hello!

That' not too important for me, I'm just curious about it.

What does the
Quote
.MethodName
do?

Thanks!

PascalDragon

  • Hero Member
  • *****
  • Posts: 5448
  • Compiler Developer
Re: Basic questions
« Reply #1 on: May 28, 2020, 09:32:41 am »
In what context? Calling a method? Declaring a method? A more complete example, please.

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • Knowledge is the key
Re: Basic questions
« Reply #2 on: May 28, 2020, 09:36:53 am »
I don' know that command..
So can you tell me the basic use instuctions for every situations?

PascalDragon

  • Hero Member
  • *****
  • Posts: 5448
  • Compiler Developer
Re: Basic questions
« Reply #3 on: May 28, 2020, 09:59:36 am »
I don't know what you're asking. Where have you seen ".MethodName"? Without context I can not help you.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Basic questions
« Reply #4 on: May 28, 2020, 10:04:18 am »
I think that refer to OOP. Functions and procedures of a class are called methods.

Examples:
AShape := TShape.Create(nil);
Button1.Click;
Form1.Hide;

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Basic questions
« Reply #5 on: May 28, 2020, 10:06:22 am »
Are you talking about this?
https://www.freepascal.org/docs-html/rtl/system/tobject.methodname.html

Quote
MethodName searches the VMT for a method with the specified address and returns the name of the method


PascalDragon

  • Hero Member
  • *****
  • Posts: 5448
  • Compiler Developer
Re: Basic questions
« Reply #6 on: May 28, 2020, 10:27:39 am »
And that's why I asked for context... given the question both rvk and Handoko's answers could be right. Or it could be something completely different...

Jake012345

  • Sr. Member
  • ****
  • Posts: 270
  • Knowledge is the key
Re: Basic questions
« Reply #7 on: May 28, 2020, 11:18:50 am »
Thanks!

But It's why good for us?

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Basic questions
« Reply #8 on: May 28, 2020, 11:39:50 am »
But It's why good for us?
You wouldn't use it for normal use.

It can be used to get a MethodName from a pointer. This is usually useful for debugging and for digging in code (RTTI).

Code: Pascal  [Select][+][-]
  1. var
  2.   Ptr: pointer;
  3. begin
  4.   Ptr := Self.MethodAddress('FormCreate');
  5.   Showmessage(Self.MethodName(Ptr));
or another example
Code: Pascal  [Select][+][-]
  1. uses typinfo;
  2. //...
  3. Showmessage( MethodName(GetMethodProp(Self, 'OnCreate').Code)); // gives FormCreate if assigned

Description from Delphi
Quote
Returns the name of a class method by address.

There are situations when it is useful to invoke an object method without hard coding the method name in advance. Call MethodAddress to dynamically retrieve the address of such a method by specifying the method name as a string.

MethodName is the opposite of this process--by supplying an Address method, the name of the method is returned as a string.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5448
  • Compiler Developer
Re: Basic questions
« Reply #9 on: May 28, 2020, 11:45:53 am »
I assume you mean rvk's answer (cause you didn't clarify that!). It's used for example for the event system inside Lazarus. The lfm file contains the name of the event handlers (e.g. OnCreate = FormCreate to assign FormCreate to the event OnCreate) and when reading the object stream MethodAddress is then used to retrieve the address of the event handler's method (in this example the address of FormCreate). MethodName on the other hand is used for the inverse, namely when you serialize a descendant of TComponent to an object stream. The stream writer gets the address stored in the event handler (e.g. OnCreate), passes that to MethodName and then writes the returned name to the object stream (in this example this will be FormCreate again).
MethodAddress and MethodName work for any method declared in a published section, but you can't get any method signature (so you need to know that at compile time).

 

TinyPortal © 2005-2018