Results 1 to 10 of 10

Thread: Method Pointers

  1. #1
    Join Date
    Feb 2009
    Location
    Nebraska
    Posts
    68
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Method Pointers

    I wrote a quick sample of using method pointers today for someone on IRC and since I only saw one other example in the forums, I figured I'd post it. It's not a tutorial, just an example.

    If there is interest I or someone else could write an actual tutorial about it.

    Maybe it'll come up in a search if someone needs it.

    SCAR Code:
    program MethodPointers;
    // SCAR V3.20rc
    type
      // Declare procedure types for the procedures that need to be called
      TSimpleMethod = procedure;
      // Note that the 'procedure' keyword and parameter list are here,
      // but no procedure name.
      TFancyMethod = procedure( aString: string );

    // Here's a couple of procedures with signatures
    // that matche the TSimpleMethod type.
    procedure MethodOne;
    begin
      writeln('method one');
    end;

    procedure MethodTwo;
    begin
      writeln('method two' );
    end;

    // Here's one with a signature that matches the
    // TFancyMethod type.
    procedure MethodFancy( aString: string );
    begin
      writeln( 'Fancy ' + aString );
    end;

    var
      // These variables can hold method pointers.
      aSimpleMethod : TSimpleMethod;
      aFancyMethod : TFancyMethod;

    begin
      // Set the value of a variable to one of
      // the matching procedures
      aSimpleMethod := @MethodOne;
      // Now call it. Note the () indicating that this
      // should call the method, not so something with
      // the value of the variable.
      aSimpleMethod();

      // Reassign the value and call it again.
      aSimpleMethod := @MethodTwo;
      // Notice that we're using the same name here, but
      // the output is different, since it's now actually
      // calling MethodTwo instead of MethodOne.
      aSimpleMethod();
     
      // This does the same thing, but now it takes a parameter.
      aFancyMethod := @MethodFancy;
      aFancyMethod( 'smancy' );
    end.
    Grippy has approximately 30,000 hours of Delphi coding experience. srsly.

  2. #2
    Join Date
    Dec 2007
    Location
    Williston, ND
    Posts
    3,106
    Mentioned
    0 Post(s)
    Quoted
    3 Post(s)

    Default

    more than 3 solid years of experience, wow

    Sorry, just had to say that. Looks helpful with them fancy comments in there
    Proud owner of "Efferator" my totally boted main account!
    "You see, sometimes, science is not a guess" -Xiaobing Zhou (my past physics professor, with heavy Chinese accent)

  3. #3
    Join Date
    Dec 2007
    Location
    Wizzup?'s boat
    Posts
    1,013
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Please explain it further.

    I don't get what it does, but it looks intriguing
    Project: Welcome To Rainbow

  4. #4
    Join Date
    Feb 2006
    Location
    Amsterdam
    Posts
    13,692
    Mentioned
    146 Post(s)
    Quoted
    130 Post(s)

    Default

    I use this a lot in my Multi Miner. You'll find that Pascal Script is very limited when it comes to pointers, though...
    Anyway, why is this in Scripting Help?



    The best way to contact me is by email, which you can find on my website: http://wizzup.org
    I also get email notifications of private messages, though.

    Simba (on Twitter | Group on Villavu | Website | Stable/Unstable releases
    Documentation | Source | Simba Bug Tracker on Github and Villavu )


    My (Blog | Website)

  5. #5
    Join Date
    Feb 2009
    Location
    Nebraska
    Posts
    68
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Wizzup? View Post
    I use this a lot in my Multi Miner. You'll find that Pascal Script is very limited when it comes to pointers, though...
    Yeah, compared to DWSII it's pretty rough, however IIRC it supports some routines to dynamically build calls to compiled Delphi methods that just isn't something you can do in DWS. Course, it's not like that is used as far as I can see.


    Anyway, why is this in Scripting Help?
    I tried to post it over in Tutorial Island, but I was getting an error. I'd prefer that it was over there though.

    Quote Originally Posted by benjaa
    Please explain it further.
    Ok, here is a short explanation.

    Any time you have a procedure in your script that procedure has an address. Normally you don't directly use the address yourself, you just use the name.

    You can think of it like a building. There is a building called "The White House" that sits at a particular place. It's address is "1600 Pennsylvania Ave", but you don't need to know that normally, because everybody knows what you're talking about if you just call it by it's name.

    If you want to add a level of indirection to your code in order to do something fancy you might need the address of your procedures. Indirection here just means that you aren't calling a procedure directly by it's name.

    Lets say you wanted to send the President a post card with your idea on how to save the economy. You could just write "The White House" on the front, and it would go there. But what if the President isn't there right then? Maybe he's over at Starbucks getting a cuppa. You'd need a way to write something other than "The White House".

    Obviously you could just write "Starbucks", if you knew that that is where he was going to be. But if you don't know where he is beforehand you'd want to set things up so that you could easily do either one.

    The address label on your postcard represents your variable, 'aSimpleMethod' in this example. The address that you put on the label is the address of the building, or procedure, that you intend. You can change it depending on what exactly it is you want to do at that moment.

    If Mr. President is out getting that Skinny Triple Venti White Mocha you stick "@Starbucks" on the address label, if he's hard at work in the Oval Office, "@The White House" goes on the label. Either way you use the same postcard, so you don't have to have a whole stack of postcards for places you think he might be, and you won't ever find yourself unable to send a postcard because you didn't expect to find him over at Harvard.

    The most common place you'll see this sort of thing is with events for visual components on forms. A button has a property (which acts very much like a variable) that you can put a procedure address into. Assigning something to the 'OnClick' property gives the button an address where it can send you a postcard when it needs to. You can change the address if you need to, but usually we don't, it just gets set once when the button is created.

    Quote Originally Posted by Brain
    more than 3 solid years of experience, wow
    Yep. I can only imagine how many times I've typed 'begin'<cr>. It's hard to type 'begin' in an email without hitting enter after it

    Not trying to brag about the experience, just don't want to have people spend their time unnecessarily explaining something basic when I post a question.

    Looks helpful with them fancy comments in there
    I tend to comment the holy bejesus out of my code. That way when I go back to fix something and I see something stupid and say "WTF was I thinking?" I can just look at the comments to see what the code was supposed to be doing.
    Grippy has approximately 30,000 hours of Delphi coding experience. srsly.

  6. #6
    Join Date
    Oct 2007
    Location
    #srl
    Posts
    6,102
    Mentioned
    39 Post(s)
    Quoted
    62 Post(s)

    Default

    Quote Originally Posted by Grippy View Post
    I tend to comment the holy bejesus out of my code. That way when I go back to fix something and I see something stupid and say "WTF was I thinking?" I can just look at the comments to see what the code was supposed to be doing.
    That is a great idea.



    You really should expand a bit more and just write a tutorial on this. You basically already did above, just elaborate a little more and it would be good to go.

  7. #7
    Join Date
    Feb 2009
    Location
    Nebraska
    Posts
    68
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by N C D S View Post
    You really should expand a bit more and just write a tutorial on this. You basically already did above, just elaborate a little more and it would be good to go.
    I can do that. I probably would have just gone ahead and done so, but that was about as much as I could write during a break at work. I guess I'll pretty it up, make the sample match the text, then think about maybe putting in a real-world example.

    I'm having trouble thinking of real-world example that isn't either too simple to just be gratuitous or to complex to be in a tutorial. Any suggestions?
    Grippy has approximately 30,000 hours of Delphi coding experience. srsly.

  8. #8
    Join Date
    Jul 2007
    Location
    Right now? Chair.
    Posts
    8,488
    Mentioned
    3 Post(s)
    Quoted
    12 Post(s)

    Default

    well, let us know if you want it moved to tutisland i would've done it, but you seem to want to change it around a bit first?

    The report post button is usually the fastest way to get it moved

    ~RM

    I & I know Zion. It is in the spirit, body and mind of every one of us
    RMouse(obj: TMSIObject): boolean;

  9. #9
    Join Date
    Feb 2009
    Location
    Nebraska
    Posts
    68
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Ok, I edited it so much that it isn't really an edit any more, so I just posted it as a tutorial.

    http://www.srl-forums.com/forum/showthread.php?t=46017

    You can treat this thread as you like, I suggest an early grave.
    Grippy has approximately 30,000 hours of Delphi coding experience. srsly.

  10. #10
    Join Date
    Jul 2007
    Location
    Right now? Chair.
    Posts
    8,488
    Mentioned
    3 Post(s)
    Quoted
    12 Post(s)

    Default

    i'll just lock
    ~RM

    I & I know Zion. It is in the spirit, body and mind of every one of us
    RMouse(obj: TMSIObject): boolean;

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. c++(pointers) help
    By hackncrack1 in forum C/C++ Help and Tutorials
    Replies: 10
    Last Post: 05-25-2009, 01:38 PM
  2. Introduction to Method Pointers
    By Grippy in forum OSR Advanced Scripting Tutorials
    Replies: 1
    Last Post: 03-11-2009, 10:10 PM
  3. Pointers?
    By mat_de_b in forum C/C++ Help and Tutorials
    Replies: 8
    Last Post: 11-05-2007, 09:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •