Autor Beitrag
Einsamer Schütze
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 37

XP Prof
Delphi 2007 professional
BeitragVerfasst: Di 14.11.06 21:21 
Hallo Entwickler,
momentan schlage ich mich mit implementation und interface herum :les::
Ich hätte ganz gern ein modulglobales Objekt, auf das ich über verschiedene Knöpfe zugreifen kann, um Methoden aufzurufen.

Wenn ich nun meine die Klasse im interface Teil deklariere und die Methodendefinition im implemtation Teil lasse, bekomme ich dies:

[Fehler] UKlasse2.pas(21): Deklaration erwartet, aber 'USES' gefunden

Wenn ich die Methodendefinitionen gleich hinten dranhänge, redefiniere ich die Dinge.

[Fehler] UKlasse2.pas(20): Bezeichner redefiniert: 'TAuto.SetFarbe'

So wie ich die Sache sehe, kann ich aber im globalen VAR Teil kein Objekt instanzieren, wenn ich die Klasse nicht vorher erklärt habe, oder?

Wie macht man das denn?

Und eine noch: die [strg + shift + c] zum Erhalten eines Methodenrumpfes funktioniert bei mir nicht - Tipps?

Danke, ES

_________________
'Gott ist auf der Seite der Vernunft.' H. Mann
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: Di 14.11.06 21:25 
Was meinst du mit modulglobal?

_________________
Markus Kinzler.
jakobwenzel
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1889
Erhaltene Danke: 1

XP home, ubuntu
BDS 2006 Prof
BeitragVerfasst: Di 14.11.06 21:25 
Zeig mal deinen Code.

Das mit STRG+Shift+C geht deshalb nicht, weil du ne Personal hast.

_________________
I thought what I'd do was, I'd pretend I was one of those deaf-mutes.
Einsamer Schütze Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 37

XP Prof
Delphi 2007 professional
BeitragVerfasst: Do 16.11.06 20:07 
Hallo Entwickler,
'modulglobal' heißt bei mir, dass ich auf ein erzeugtes Objekt aus der ganzen Unit heraus zugreifen kann.
Esgeht darum, wo man was deklarieren und definieren muss (in C++ war alles so frei...)
So versuche ich (erfolglos)ein globales Objekt für meine Unit zu zu erstellen:

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:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    procedure Button1Click(Sender: TObject);

  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  var meinAuto : TAuto;
implementation
//------------------------------------------------------
type
  TAuto = class
  private
    FFarbe: string;
    FBaujahr: integer;

  public
    procedure SetFarbe(Farbe: string);
    function GetFarbe(): string;

   // property Farbe: string read GetFarbe  write SetFarbe;

end;
//-------------------------------------------------------
procedure TAuto.SetFarbe(Farbe: string);
begin

  FFarbe := Farbe ;
end;
//-------------------------------------------------------
function TAuto.GetFarbe():string;
begin
  Result := FFarbe + ' Extra';
end;

//-----------------------------------------------------------------

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);


begin
meinAuto := TAuto.create;
meinAuto.setFarbe(edit1.Text);
edit2.text:= meinAuto.GetFarbe;

edit3.Text := meinAuto.GetFarbe + ' Metallic';
end;




Hier der Code mit der 'Redefinieren' Fehlermeldung, betrifft wo alles deklariert und definiert ist:
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:
unit UKlasse2;

unit UKlasse2;

interface
type
  TAuto = class
  private
    FFarbe: string;
    FBaujahr: integer;

  public
    procedure SetFarbe(Farbe: string);
    function GetFarbe(): string;

   // property Farbe: string read GetFarbe  write SetFarbe;

end;
//----------------------------------------------------------------
procedure TAuto.SetFarbe(Farbe: string);
begin

  FFarbe := Farbe ;
end;
//-----------------------------------------------------------------
function TAuto.GetFarbe():string;
begin
  Result := FFarbe + ' Extra';
end;



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


Moderiert von user profile iconChristian S.: Delphi-Tags hinzugefügt

_________________
'Gott ist auf der Seite der Vernunft.' H. Mann
Einsamer Schütze Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 37

XP Prof
Delphi 2007 professional
BeitragVerfasst: Mo 27.11.06 12:04 
Titel: Code schräg?
Hallo, ist mein Code so schräg dass darauf keiner antworten möchte, oder war der Verweis auf C++ im Delphi-Forum unzulässig?
Ich bräuchte nur ein wenig Hilfestellung, wie das Prinzip gedacht ist.
Vielleicht hilft mir ja doch noch jemand auf die Sprünge,

Danke, ES
freak4fun
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 604
Erhaltene Danke: 4

Win 7 Pro
VS 2013 Express, Delphi, C#, PHP, Java
BeitragVerfasst: Mo 27.11.06 12:15 
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 UKlasse2;

interface

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

type
  TAuto = class
  private
    FFarbe: string;
    FBaujahr: integer;
  public
    procedure SetFarbe(Farbe: string);
    function GetFarbe(): string;
    // property Farbe: string read GetFarbe  write SetFarbe;
  end;

implementation

{$R *.dfm}

//----------------------------------------------------------------
procedure TAuto.SetFarbe(Farbe: string);
begin
  FFarbe := Farbe ;
end;

//-----------------------------------------------------------------
function TAuto.GetFarbe():string;
begin
  Result := FFarbe + ' Extra';
end;

end.


Grundlagen: dsdt.info

MfG
freak ;)

_________________
"Ich werde auf GAR KEINEN Fall…!" - "Keks?" - "Okay, ich tu's."
i++; // zaehler i um 1 erhoehen
Stefan.Buchholtz
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 612

WIN 2000, WIN XP, Mac OS X
D7 Enterprise, XCode, Eclipse, Ruby On Rails
BeitragVerfasst: Mo 27.11.06 12:26 
Deine erste Fehlermeldung: "UKlasse2.pas(21): Deklaration erwartet, aber 'USES' gefunden" liegt am uses - das muss immer die erste Direktive nach interface bzw. implementation sein. Zuerst Typen deklarieren, dann andere Units einbinden geht nicht.

Die Implementation einer Klasse oder Prozedur darf nur im implementation Teil einer Unit stehen. Die Typdeklaration darf entweder im interface- oder im implementation-Teil stehen - steht sie im implementation-Teil, ist die Klasse nur in der Unit gültig und kann von anderen Units nicht benutzt werden. Ein Typ muss auf jeden Fall deklariert werden, bevor er benutzt werden kann.
Normalerweise würde ich für jede Klasse eine eigene Unit anlegen, das ist aber eine Stilfrage.
Für deine Auto-Klasse würde das so aussehen:

ausblenden 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:
unit UAuto;

interface

type
  TAuto = class
  private
    FFarbe: string;  
    FBaujahr: integer;  
    procedure SetFarbe(Farbe: string);  
    function GetFarbe(): string;  
  public  
   property Farbe: string read GetFarbe  write SetFarbe;  
  end;  

implementation

//----------------------------------------------------------------  
procedure TAuto.SetFarbe(Farbe: string);  
begin  
  FFarbe := Farbe ;  
end;  

//-----------------------------------------------------------------  
function TAuto.GetFarbe():string;  
begin  
  Result := FFarbe + ' Extra';  
end;  

end.


Stefan

_________________
Ein Computer ohne Windows ist wie eine Schokoladentorte ohne Senf.
VaNaTiC
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 30



BeitragVerfasst: Mo 27.11.06 15:51 
Singleton-Pattern
Abgewandelt dazu: Modul-Global
D.h. Deklarationen raus aus der Interface-Sektion und rein an oberste Stelle in Implementation-Sektion
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:
unit ...;
uses ...;
interface

type
  TModulGlobal = class(...)
    public
      EineMethode...
  end;

  function ModulGlobal: TModulGlobal;

implementation

var
  g_ModulGlobal: TModulGlobal;

function
begin // wenn notwendig CriticialSection für threadsafe
  if not Assigned(g_ModulGlobal) then
    g_ModulGlobal := TMopdulGlobal.Create....
  Result := g_ModulGlobal;
end;

initialiazation
  // g_ModulGlobal eventuell auch hier schon erzeugen?!
finalization
  if Assingned(g_ModulGlobal) then
    try
      g_ModulGlobal.Free;
    finally
      g_ModulGlobal := nil;
    end;
end.
Einsamer Schütze Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 37

XP Prof
Delphi 2007 professional
BeitragVerfasst: So 04.03.07 23:17 
Ein verspätetes Danke noch, das habt Ihr wirklich gut erklärt. Natürlich war das Beispiel aus dem DSDT, deshalb war ich ja auch so verwundert, dass ich damit so Probleme bekam.
Seither ist aber einiges passiert.
Ahoi und weiter so,
ES :idea:

_________________
'Gott ist auf der Seite der Vernunft.' H. Mann