Autor Beitrag
schlumsch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 122

alles Win :)
Delphi 2005 Prof, Delphi 2007
BeitragVerfasst: Sa 21.11.09 12:01 
Hallo,

seit langem beshäftige ich mich mal wieder mit Delphi und habe
eine simple Frage zum Thema Variablen & scope.

Ich hab bislang in meinem Programm eine globale
Variable pictureFile:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
    ...
    procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);

  private
  procedure Systray(var sMsg: TMessage); message IC_CLICK;       // Tray
  public
    { Public-Deklarationen }
  end;
var
  Form1: TForm1;
  pictureFile: String;
  ...


Damit funktioniert mein Prog wunderbar. Nun möchte ich aus einer anderen Unit auf eben diese variable zugreifen und deklariere sie public, ändere alle zugriffe aus der aktuellen unit auf Form1.pictureFile := ....



ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
...
   procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);

  private

  procedure Systray(var sMsg: TMessage); message IC_CLICK;       // Tray
  public
    { Public-Deklarationen }
      pictureFile: String;
  end;




var
  Form1: TForm1;
...


Wieso funktioniert das nicht? Sprich die Variable ist mal einfach schlicht weg leer... wo ist da mein Fehler?

_________________
icq 102779206
"God is real, unless declared integer..."
Mike19
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 256

Win XP, Vista, Win 7
Delphi 2005, Turbo Delphi
BeitragVerfasst: Sa 21.11.09 12:10 
Hallo,

in Deinem ersten Quelltext war doch Deine Variable schon global, was willst Du da noch ändern. Wenn Du jetzt in einer anderen unit die erste unit in die uses-Klausel aufnimmst, steht Dir doch die Variable zur Verfügung.
schlumsch Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 122

alles Win :)
Delphi 2005 Prof, Delphi 2007
BeitragVerfasst: Sa 21.11.09 12:28 
nope tut sie nicht. Mal ganz davon abgesehn das sich mein Problem auch schon soeben geklärt hat (man sollte beim umbenennen von Variablen eben auch darauf achten das man das speichern und laden an den neuen Namen anpasst *g*) bekomme ich keinen Zugriff auf pictureFile wenn sie global ist. aus meiner 2ten unit heraus bringt mir ein Form1.
keinen Treffer, wenn ich die Variable public deklariere hingegen schon

_________________
icq 102779206
"God is real, unless declared integer..."
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Sa 21.11.09 12:50 
Nach einem Form1. werden dir nunmal die Klassenmember der TForm1-Klasse angeboten und nichts Anderes ;) . Genauso wie auf die globale Variable Form1 greifst du direkt auf pictureFile (oder explizit: Unti1.pictureFile) zu.
Jetzt fehlt nur noch der Standard-Disclaimer, dass robustes OOP-Design globale Variablen überflüssig macht ;) .

_________________
>λ=
Mike19
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 256

Win XP, Vista, Win 7
Delphi 2005, Turbo Delphi
BeitragVerfasst: Sa 21.11.09 12:58 
ich verstehe es nicht.

warum soll das nicht funktionieren

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  test: String;

implementation

uses Unit2;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
 machwas(Edit1.Text);
end;

end.


und:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
unit Unit2;

interface
uses unit1;
procedure machwas(test:String);
implementation
 procedure machwas(test: String);
 begin
   test:='irgendetwas';
   Form1.Label1.Caption:=Test;
 end;
end.
schlumsch Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 122

alles Win :)
Delphi 2005 Prof, Delphi 2007
BeitragVerfasst: Sa 21.11.09 14:56 
@Mike19: wie schon geschrieben lag das Problem an einer anderen Stelle in meinem Prog.
@KHA: Ja nee is klar... man sollte es mit "unit" probieren... Ich verfolge nur die Taktik mich Stück für Stück einer adequaten Implementierung zu nähern, sprich getter und setter für priv Vars sind eben erst an einem späteren Zeitpunkt geplant, zuvor hab ich noch genügend Ecken an denen ich baseln muss :)

danke jedenfalls

_________________
icq 102779206
"God is real, unless declared integer..."