質問

I am a beginner to Delphi and am currently practising it in RAD XE5 student version.

I have made 2 units, in the first I specify a variable in the INTERFACE section

like so:

interface
var
Globalstring : integer;

Then I assign a value to this variable via

TextInput.Text = GlobalString

Whenever I try to recall globalstring in my second unit, I get a blank.. :(

08/02/2014

Okay guys, I am sorry for the lack of information, the frustration had already gotten to me. It was indeed not the real code and the integer was indeed a string all along, so here follows the real code to clear all confusion

unit Unit1;

interface
uses FMX.Effects, FMX.StdCtrls, FMX.Controls,
     System.Classes, System.SysUtils, System.Types, System.UITypes,  System.Variants,
     FMX.Types, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Edit, FMX.Filter.Effects, FMX.Objects, Unit2;

type
 TForm1 = class(TForm)
 EditUser: TEdit;
 EditPass: TEdit;
 btnLogin: TButton;
 Label1: TLabel;
 chkGuest: TCheckBox;
 Label2: TLabel;
 SlideTransitionEffect1: TSlideTransitionEffect;
 Image1: TImage;
 procedure btnLoginClick(Sender: TObject);
 procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public

end;

var
 Form1: TForm1;
 UserLogin : string;


implementation

{$R *.fmx}

uses Unit4,Unit3,Unit5;

procedure TForm1.btnLoginClick(Sender: TObject);

begin
  Unit2.UsernameJef:= 'Jef'; Unit2.UsernameDennis :='Dennis';
  Unit2.PasswordJef:= '123'; Unit2.PassWordDennis :='456';

if ((EditUser.text=Unit2.UsernameJef) and (EditPass.text=Unit2.PasswordJef))
  or ((EditUser.text=Unit2.UsernameDennis) and (EditPass.text=Unit2.PassWordDennis))
    then form4.Show
      else Label1.Visible := true;



if (EditUser.Text= 'Guest')
  or (chkGuest.IsChecked = true)
   then Form5.Show;

UserLogin := EditUser.Text;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin

end;

end.

Then here follows the unit 4 in wich I actually use this "global variable".

I want to show a welcoming message in my application after my user logs on, in this case "Welcome Jef" or "Welcome Dennis"

procedure TForm4.FormCreate(Sender: TObject);
begin
lblWelkom.Text := Unit1.UserLogin

end ;

end.
役に立ちましたか?

解決

I have no idea what you've actually done wrong because you have not posted the necessary code. However, I'll explain some things you seem to be misunderstanding about programming, and perhaps that will help. Otherwise, please update your question with relevant information from both units (we're not all psychic).

Programs execute code as a sequence of instructions. Some instructions call other instructions. And no matter what code may sit inside your first unit that sets a global variable, that variable will not change until the instruction to actually change it has executed.

So, try the following:

  • Put a break-point on your line of code in unit 1 that sets the global.
  • And another on the line of code that reads the global in unit 2.
  • Run your program.
  • You will probably find that the program stops at the unit 2 breakpoint before the unit 1 breakpoint.
  • This means the instruction to change the value has not executed yet.
  • (And will not execute until another line calls the appropriate instruction that will cause it to be executed.)

There are of course other possibilities (but as mentioned before, we need the code in order to debug it):

  • There might be another instruction somewhere else that that clears the value you previously set.
  • Although you made you variable in unit 1 global, you might find that unit 2 is looking at an entirely different value - that happens to have the same name.
  • Globals are relatively simple, but if you were using objects you would also have to consider whether both references are using the same instance of your class.
  • EDIT: PS: Of course, if you used = expecting that to set the value, you'll find it doesn't. = is the comparison operator. := is the assignment operator; and when using it the result of the expression on the right will be assigned to the variable (global,local,field,argument) on the LEFT.

EDIT:

So, if you follow my earlier advice: put a breakpoint on lblWelkom.Text := Unit1.UserLogin and another on UserLogin := EditUser.Text;. You'll observe that you're reading the value of UserLogin before you've assigned it. LU RD explains why it's not working, and how to fix it.

他のヒント

One of the fundamental basics of Delphi is that you must explicitly assign a compatible type, especially between an Integer and a String. Your global variable is named Globalstring but yet it's declared as an Integer. Therefore, when you try to assign this integer to the edit control's text property, it won't compile because the types don't match.

A simple call to IntToStr would do the trick. There are many similar functions to convert various types to other types.

TextInput.Text:= IntToStr(GlobalString);

Mind the messages. You need to understand the errors, warnings, and hints that the compiler gives you. It's standard practice to make sure there are none. Of course the errors obviously need to be taken care of before you can even get a compiled application in the first place.

You do however have a major flaw in your design. You shouldn't be declaring an Integer with a name that refers to a String. This will only lead to future issues with maintenance. You should always declare variables with descriptive names so that you (and others) can understand what's what.

Your form in unit4 is created when your application starts, hence lblWelkom.Text will be assigned a blank value since Unit1.UserLogin is not getting a value until the end of your TForm1.btnLoginClick.

Minimal change to make this work is to set lblWelkom.Text in the OnShow event instead. Also define the global UserLogin before calling Form4.Show.

Normally you should avoid using global values. Your Form4 could have a property UserName instead, which you set before calling Show.

TForm4 =
...
private
  FUserName: String;
public
  property UserName : String read FUserName write FUserName;
...
end;

And in your OnShow event:

procedure TForm4.FormShow(Sender: TObject);
begin
  lblWelkom.Text := UserName;
end;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top