0

This is a simple question but I don't know how to construct a variable name by concatenating two strings. The code below is how not to do it...

var
  UserName1 : String;
  UserName2 : String;
  Password1 : String;
  Password2 : String;
  UserCount : Integer;

UserCount := 2;

for Wk1 := 1 to UserCount do
begin
  DoLogin(UserName+Wk1, Password+Wk1);
end;  

2 Answers 2

2

Basically, you can't do this. Variable names are fixed at compile time and translate to addresses which hold the value of the variables.

It looks like you want an array, or in this case, two: one array will hold the login names and one array the password. Of course, you could combine the two into a record and then have an array of records.

type
 LogType = record
            username, password: string[31]
           end;

var
 LogArray: array [1..10] of logtype;
 usercount, wk1: integer;

begin
 UserCount := 2;
 for Wk1 := 1 to UserCount do
  begin
   DoLogin(logarray[wk1].username, logarray[wk1].password);
   etc
  end;
end;
Sign up to request clarification or add additional context in comments.

3 Comments

Any particular reason you used ShortString for the string types? The OP didn't indicate any need for that, and it's not commonly used any longer in Delphi (certainly not by default in Dephi 5, which is in one of the tags).
@Ken: Force of habit. I started programming with Pascal in the early 1980s when memory was a constraint. I can't get used to profligate memory use with strings....
@No'amNewman: I think I can understand you, but, on the other hand, your habit may in fact lead you into profligating memory to a greater extent than it might be with the string type. Consider the sample code in your answer. How often can you come across a password that long (31 chars)?
2

Don't try and do that.

Reflection (or RTTI - Run Time Type Information) would be needed and it's not good to use in Delphi as it can change from release to release and you would need to include debug info when building I think.

So use an array or two.

var
UserNames[1..2] : String;
Passwords[1..2] : String;
UserCount : Integer;

UserCount := 2;

for Wk1 := 1 to UserCount do
begin
  DoLogin(UserNames[Wk1], Passwords[Wk1]);
end;

1 Comment

D2010 does support proper RTTI on non publised symbols. But it shooting a mosquito with a cannon. The mosquito is dead, but so is everybody else.

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.