Link to home
Start Free TrialLog in
Avatar of Alpha_AI
Alpha_AI

asked on

Function creation and function run during run-time. Is it possible?

Hi there,

I have a word called 'load' of type string and I want it to be a function
this word 'load' would change often of course.

ya know how ya would have

function TForm1.Load(file : string; fileavailable : boolean);
begin
    // begin code here..
end;

well I dont want that.... I want something like this

      Procedure TForm1.OnCreate(Sender : TObject);
      function TForm1.Load(file : string; fileavailable : boolean);
      function TForm1.Function(function : string; functionavailable : boolean);


Procedure TForm1.OnCreate(Sender : TObject);
begin
   Form1.Function(load,true);
end;

function TForm1.Function(function : string; functionavailable : boolean);
var
   functionstring : string;
begin
   functionstring := function;
   // now insert code here to initialise load function based on functionstring or even function.
   // im guessing it might be something like this

   TForm1.(string);  // which I know wouldn't work.
end;

function TForm1.Load(file : string; fileavailable : boolean);
begin
   file := 'loadme.txt'
end;

I just want the function to load based on what the string becomes.

Avatar of Alpha_AI
Alpha_AI

ASKER

if you have any questions, please ask me.

This doesn't make sense.  At first it sounds like you're trying to reinvent the DLL, but hten it sounds like you're trying to reinvent the compiler.  Please clarify.
hello Alpha Al. . . . I can not understand what you want to do?? But from your last Line
"I just want the function to load based on what the string becomes." ???

If you can limit the "Input" string to a Set of Strings, like

'Load'
'Reset'
'Cancel'
'Next'
'Unload'

then you just compare the input string and execute the function for that string

if Edit1.Text = 'Load' then
Form1.Load else
if Edit1.Text = 'Reset' then
Form1.Reset;
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi meikl, you've said that already. I must have refreshed my browser :-)
Ok, I think I have a pretty good idea on what you are trying to do.  You are attempting to pass a string to a function that will call some other function based up on the string name you choose.  Provoded below is one way of doing this, it is NON-Case sensitive.  This function also assumes that we are always dealing with procedures and does not account for parameter passing.  If you want parameter passing you have to use another method that is about 10 times more complicated to implement.  Please note that in each procedure that is to be called using your procedure calling function, you must have the “With Form1” Statement.  Hope this does what you want…. Enjoy.

//example procedure
procedure TForm1.Load;
begin
  with Form1 do //Very important, without this you could
                //get access violation
    begin
      //place your code here
      label1.caption := 'Booya';
    end;
end;


function RunProcedure(ProcedureName : string):boolean;
  var
    ProcedureExists : boolean;
    MyProcedure     : TProcedure;
    p               : Pointer;

begin
  //Initialize our variables
  ProcedureExists := false;
  p := nil;

  //Get the address of our procedure
  p := Form1.MethodAddress(ProcedureName);

  if p <> nil then
    begin
      //point to our located procedure
      MyProcedure := TProcedure(p);
      //call the procedure
      MyProcedure;
      //set our result to true
      ProcedureExists := true;
    end;

  result :=  ProcedureExists;
end;
you go a slight different way, geo
-> via TMethod, not bad
listening, and learning ;)
WhoPhlungPoo :

Form1 is a global ... what if you instantiate more than one TForm1?

You only risk getting a memory exception if "self" is unassigned or NIL - i.e. a rather serious bug.  References to methods and properties of the self are treated as though within a "with self do ..." block.

Don't break encapsulation without serious thought as to the consequences, and then it is generally better for performance and reliability to refactor to maintain encapsulation.
i agree with you, swift99,
but it may be for a scripting input
(the only thing, that makes sense for me)

meikl ;-)
A scripting interpreter ... yes ... but that would not entail what amounts to self modifying code.

Embodied in a Datamodule rather than a form, I can see that having some real uses.
Ok its looks like someone may have worked it out.
I am not near my computer at mums so i'll have to try the code out then when I get back to mums.

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial