Autor Beitrag
sunnyandy
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 47



BeitragVerfasst: Mo 10.09.07 08:59 
Hallo!

habe folgende Funktion in meiner Klasse Kunde:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
function TKunde.GetMobilPrivat: string;
begin
  result := FMobilPrivat;
end;


Dem Feld wird vorher ein Wert zugewiesen, bevor ich ihn auslese.
Es ist folgendermaßen definiert:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
  private
    FKundenNummer: string;
    FTelefonFirma: string;
    ...
    FMobilPrivat: string;
    ...
    public
    property MobilPrivat: string read GetMobilPrivat write SetMobilPrivat;


Beim Aufruf bekomme ich folgende Exception (direkt nach dem Abarbeiten der Zeile "result := ..."): "EAccessViolation" with message 'Access violation at address... in module ... . Read of address ...'
Habe mehrere solcher Felder, und die anderen gehen alle, aber bei dem gibt es diesen Fehler. Schreiben kann ich in dieses Feld übrigens auch nicht, da kommt die gleiche Meldung.

Kann mir jemand vielleicht sagen, woran es liegen könnte?

Dankeschön! Andy
noidic
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 851

Win 2000 Win XP Vista
D7 Ent, SharpDevelop 2.2
BeitragVerfasst: Mo 10.09.07 09:49 
An dem Code kann ich nix falsches erkennen.. kannst du mal den Code posten, wo du auf die eigenschaft zugreifst?

_________________
Bravery calls my name in the sound of the wind in the night...
sunnyandy Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 47



BeitragVerfasst: Mo 10.09.07 10:02 
hallo!

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
class procedure TKundeDB.Insert(BusinessObject: ISaveable);
var
  Kunde: TKunde;
  Query: string;
  connection: TDatabaseConnection;
begin
  // TypeCast durchführen
  Kunde := TKunde(BusinessObject);

  connection := (TDatabaseConnection.NewInstance as TDatabaseConnection);
  Query := connection.Datenbankbefehle.InsertKunde;
  With connection.ZugriffsKomponente do
  begin
    Kunde.MobilPrivat := '33'; // Hier kommt der Fehler!
...


Habe ich vielleicht den Kunden falsch übergeben? Muss ich bei eigens erstellen Objekten die Parameter anders übergeben?
Wenn ich anstatt das übergebene Objekt zu benutzen, einen neuen Kunden erstelle, kann ich dort auf die Eigenschaft zugreifen.

Hier rufe ich die obige Methode auf (AktuellerKunde enthält wirklich eine MobilPrivat):
ausblenden Delphi-Quelltext
1:
TKundeDB.Insert(AktuellerKunde);					
noidic
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 851

Win 2000 Win XP Vista
D7 Ent, SharpDevelop 2.2
BeitragVerfasst: Mo 10.09.07 10:13 
Prüf doch vor dem TypeCast mal, ob BusinessObject tatsächlich ein TKunde ist. Vielleicht geht da ja was schief.

_________________
Bravery calls my name in the sound of the wind in the night...
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Mo 10.09.07 10:28 
Lass doch mal die Zeile mit "With" weg. Ansonsten kann ich im Moment keinen Fehler sehen.
sunnyandy Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 47



BeitragVerfasst: Mo 10.09.07 19:31 
Hallo, erstmal danke für die Antworten!

Der Kunde implementiert ja auch das Interface ISaveable. Wenn ich nun vor dem TypeCast prüfen will, ob das übergebene Objekt vom Typ Kunde ist, wie mache ich das?

Habe das mal ausprobiert:

ausblenden Delphi-Quelltext
1:
2:
if (BusinessObject is TKunde) then
    Kunde := TKunde(BusinessObject);


Aber da sagt der Compiler, dass der Operator da nicht anwendbar ist. Wie muss man denn da beim Interface vorgehen?

Grüße, Andy!
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Di 11.09.07 14:06 
Im Allgemeinen gilt: Wenn man TKunde haben will, dann verlangt man danach (und nicht einem Teil, der von TKunde implementiert wird).

Dass Du ein Interface nicht auf eine Klasse casen kannst, ist rein logisch, wenn man sich mal anschaut, worum es sich bei beidem handelt. Klassen bestehen aus einer Methodentabelle UND einem Datenbereich; Interfaces besitzen NUR die Methodentabelle. Zum Typekcasten fehlt also was ...

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
HelgeLange
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 735
Erhaltene Danke: 6

Windows 7
Delphi7 - Delphi XE
BeitragVerfasst: Di 11.09.07 14:25 
Der Fehlker sieht für mich auch einfach nach einem Nicht-Initialisierten objekt aus. Wie BenBE schon sagte, ein Objekt besteht aus Daten- und Code-Bereich. Den Code-Bereich kann man immer nutzen, nur wenn man auf Daten innerhalb des Objekts zugreifen will, MUSS man es initialisiert (erzeugt) haben. Sowas wie :

ausblenden Delphi-Quelltext
1:
Kunde := TKunde.Create(aOwner);					

_________________
"Ich bin bekannt für meine Ironie. Aber auf den Gedanken, im Hafen von New York eine Freiheitsstatue zu errichten, wäre selbst ich nicht gekommen." - George Bernhard Shaw