-3

How can I find variables by name at runtime and set and get their values?

I have one edit where the user enters the name of a variable. (Assume the user enters the name of a variable that exists.) I need that the program finds the variable by name and returns the value. I would also like to change the value of the variable.

Here's an example with hypothetical function names:

// Display value of variable whose name is given by Edit1.Text
ShowMessage(GetValueOfVariable(Edit1.Text));

SetValueToVariable(Edit1.Text, 'NewValueToVariable'); //Set new value to variable.
Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • Where do the variables live? And which version of Delphi. – David Heffernan Feb 14 '13 at 11:45
  • What are you trying to achieve? – gronostaj Feb 14 '13 at 11:51
  • 1
    IIRC, this question was asked yesterday. It was closed and deleted. It was a duplicate. – Andreas Rejbrand Feb 14 '13 at 12:31
  • @AndreasRejbrand the OP is a member for two days. He's unexperienced with this site, IMHO. – Bogdan Doicin Feb 14 '13 at 12:38
  • You can see the answers to EE questions so long as you navigate to the question from the results of a Google search! Doesn't make EE any more palatable mind you! – David Heffernan Feb 14 '13 at 13:04
  • Just wondering, how the accepted post answers *"How to find and set a property by name ?"*. Or was that meant to be *"How to find and set a certain property of a component found by name ?"*. – TLama Feb 14 '13 at 13:14
  • @TLama correct. It's meant to be *How to find and set a certain property of a component found by name* – Bogdan Doicin Feb 14 '13 at 13:16
  • 1
    @Bogdan, if the question is really about how to find a component by name, then your answer is peculiar since it starts out saying it's not possible, and then goes on with nonsense about storing variables in edit fields. Could you please edit your answer so that it addresses just the question you think you're answering, without misleading by saying that unrelated things aren't possible? – Rob Kennedy Feb 14 '13 at 14:36
  • @RobKennedy Okay. I will edit it. Tell me if it's better now. – Bogdan Doicin Feb 14 '13 at 14:54
  • If you want an answer to the question, you need to improve it. So, I ask again, where do the variables live? What type of variables are they? – David Heffernan Feb 14 '13 at 18:00
  • Please edit the Q to say that. It's important. – David Heffernan Feb 14 '13 at 18:39

2 Answers2

3

If by variable you mean the variables you declared by coding then what you wish for is impossible.

If by variable you mean values stored in components' fields (e.g. a text stored into an edit box), you can try this approach. All the components in the main form have as parent... well, the main form. Also, each component, including "storing components", such ar edit boxes, also have their name. You can wisely give names to your "storing components" and cycle through the list of the components that have the main form as parent. Something like this:

for i := 0 to MainForm.ComponentCount do
begin
  if MainForm.Components[i] is TEdit then
    if MainForm.Components[i].Name = '{the name you seek for}' then
      ShowMessage(MainForm.Components[i].Text)
end;

In English, you cycle through every component in the form. If a component is an edit field and it has the name you wish for, then you show its value.

Bogdan Doicin
  • 2,342
  • 5
  • 25
  • 34
  • Thank you for supporting. I have managed to do this with edit fields, like to do with variables. I saw a similar question on another site, but to see the answer has to pay. http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_26973738.html – Christian Marchiori Feb 14 '13 at 12:24
  • 1
    Your code talks about parentage, but the `Components` array deals with ownership. And if you're looking for an owned component with a particular name, then get rid of the loop and just call `FindComponent`. – Rob Kennedy Feb 14 '13 at 14:35
  • @RobKennedy I think you misunderstood me, I know how to find components, I want find variables (var String, Integer, Double ...). – Christian Marchiori Feb 14 '13 at 15:22
  • 3
    Then why did you accept this answer, Christian? – Rob Kennedy Feb 14 '13 at 15:55
  • @RobKennedy It was an accident, sorry. – Christian Marchiori Feb 14 '13 at 16:16
  • Why did you "disaccept" my answer? I answered your question. – Bogdan Doicin Feb 14 '13 at 16:26
  • Sorry, I think you misunderstood me, I know how to find components, I want find variables (var String, Integer, Double ...). – Christian Marchiori Feb 14 '13 at 16:51
  • I understood that :) But I also gave you the answer you were seeking for: **If by variable you mean the variables you declared by coding then what you wish for is impossible.** – Bogdan Doicin Feb 14 '13 at 17:13
  • @Bogdan But I can do this in clipper. in delphi is really impossible? – Christian Marchiori Feb 14 '13 at 17:24
  • In Delphi is impossible, yes. You may ask for another opinion, if it helps you. – Bogdan Doicin Feb 14 '13 at 17:48
  • @Christian, just for being absolutely clear. Do you mean how to access variables, whose are declared as for instance `var UserName: string;` or you're asking how to access properties of a certain component, like for instance `Edit1.Text` as you've shown in your question. The latter is possible, the first is not. – TLama Feb 14 '13 at 17:53
  • I thought it was possible to do this in delphi like the clipper. – Christian Marchiori Feb 14 '13 at 18:01
  • What do I do now? Should I mark the answer as accepted or delete the question? – Christian Marchiori Feb 14 '13 at 18:02
  • @TLama, the question is not asking how to access properties of a component. The code accesses a property of a component because that component's property is where the *name* of the desired variable is stored: The user has typed text into the edit box, and that text represents the name of some variable in the program. The program's job is to fetch the variable with that name and show the variable's value. Component properties have nothing to do with it. – Rob Kennedy Feb 14 '13 at 18:31
  • @RobKennedy You understand exactly what I wanna do. – Christian Marchiori Feb 14 '13 at 18:35
  • @Rob, ah, now I see. Well, talking about that, OP might also encapsulate all the variables he needs to handle that way into a class and use e.g. old style RTTI. Here's a [simple project](http://code.google.com/p/projects-stackoverflow-tlama/downloads/detail?name=14874094.ZIP) of such *"property by name setter"*. – TLama Feb 14 '13 at 18:35
  • @TLama If the variables are in a class I can do? – Christian Marchiori Feb 14 '13 at 18:45
  • See the project I've made a few minutes ago... (link is in my previous comment). – TLama Feb 14 '13 at 18:46
0

You clarify in a comment that you wish to refer to local variables by name. This is impossible in all versions of Delphi.

If you want to be able to refer to values by names then you will have to stop using local variables to store your values. Instead use a dictionary. In Delphi that is:

TDictionary<string, YourValueDataType>

which is a generic class defined in the Generics.Collections unit.

So if your values are integers, for example, you would use

TDictionary<string, Integer>
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Of course, presumably you want your values to be persistent, so a local variable is not appropriate. So, whilst this answers the question that you asked, you probably asked the wrong question. I suggest you tell us what your problem is, rather than telling us your solution. Let us work out what the right solution is. – David Heffernan Feb 14 '13 at 18:52
  • Thanks for helping me, I think the answer of TLama solve my problem. – Christian Marchiori Feb 14 '13 at 19:34
  • I don't think it does. It's almost certainly not the best solution to your problem. If you would tell us what your problem is then we could help you better. But I'm not convinced you actually want us to help you. – David Heffernan Feb 14 '13 at 19:37
  • I did not know tdictionary, I will learn how to use it, if it is the best solution for what I want, I check your answer as accept. – Christian Marchiori Feb 15 '13 at 10:08
  • I did not talk about my problem why is a lot to explain, and as my english sucks gets tricky. – Christian Marchiori Feb 15 '13 at 10:11