Autor Beitrag
Mitmischer 1703
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 754
Erhaltene Danke: 19

Win 7, Debian
Delphi Prism, Delphi 7, RAD Studio 2009 Academic, C#, C++, Java, HTML, PHP
BeitragVerfasst: So 27.12.09 11:55 
Hallo C#-Forum :)

ausblenden Delphi-Prism-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
 try                    
  try
   var Data := new System.IO.StreamWriter (OutputFile);
   for i: Int32 := 0 to length(prims)-1 do begin
    if prims[i] then
    begin
     Console.WriteLine(i.ToString);
     Data.WriteLine(i.ToString);
    end;
   end;
  except
   on E : Exception do
    Console.WriteLine('Fehler beim Schreiben in Datei: ' + E.Message + E.InnerException);  
  end;
 finally 
  Data.Close;
 end;
 Console.ReadLine;


Ich stehe vor folgendem Problem:

Ich habe eine Variable namens Data - doch im finally-Block kann Prism sie plötzlich nicht mehr finden - Wieso nicht?

_________________
Die Lösung ist nicht siebzehn.
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: So 27.12.09 12:20 
Hallo,

ich dachte, Prism würde wie Delphi arbeiten und die Deklaration der Variablen am Anfang einer Methode verlangen? Das stimmt anscheinend nicht, sondern Prism arbeitet in dieser Hinsicht wie C#.

Das Problem liegt im Gültigkeitsbereich der Variablen. Die Variable Data ist deklariert innerhalb des inneren try-Blocks; sie ist also nur dort gültig. Du musst also die Deklaration in den äußeren try-Block (der zu finally gehört) verschieben. Die Initialisierung gehört vermutlich ebenfalls dorthin; andernfalls könnte im finally-Block nicht Close ausgeführt werden.

Da lob ich mir doch die zusammenhängende Konstruktion try-catch-finally unter C#; die doppelte try-Konstruktion empfand ich unter Delphi immer als Krücke.

Gruß Jürgen
Mitmischer 1703 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 754
Erhaltene Danke: 19

Win 7, Debian
Delphi Prism, Delphi 7, RAD Studio 2009 Academic, C#, C++, Java, HTML, PHP
BeitragVerfasst: So 27.12.09 12:40 
Prism erlaubt da auch mitten im Quelltext ;)

Meine Konstruktion sieht jetzt so aus:

ausblenden Delphi-Prism-Quelltext
1:
2:
3:
 try
   var Output := new System.IO.StreamWriter (OutputFile);                  
   try


aber das Problem besteht immernoch.

_________________
Die Lösung ist nicht siebzehn.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19326
Erhaltene Danke: 1749

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 27.12.09 13:04 
Also analog zu Delphi würde ich das so erwarten:
ausblenden Delphi-Prism-Quelltext
1:
2:
3:
4:
5:
6:
  var Data := new System.IO.StreamWriter(OutputFile);
  try
    ...
  finally 
    Data.Close;
  end;
Das wäre auch logisch, denn wenn die Variable innerhalb des try-Blocks selbst deklariert ist, dann muss diese ja bei der Exception noch gar nicht deklariert sein (z.B. Exception in Zeile 1, Deklaration in Zeile 2). Und dann wäre sie in finally noch nicht vorhanden.
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: So 27.12.09 14:25 
Danke, Sebastian! Das war auch mein erster Gedanke, aber durch den Wechsel zwischen Delphi und C# und ohne Prism-Kenntnisse war ich mir zu unsicher und hatte das verworfen und nicht als Lösung erwähnt. Jürgen
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: So 27.12.09 14:52 
Oder natürlich... ;)
ausblenden Delphi-Prism-Quelltext
1:
2:
using Data := new StreamWriter(OutputFile) do
...

_________________
>λ=
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19326
Erhaltene Danke: 1749

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 27.12.09 14:53 
Ah, so sieht das bei Prism aus, von C# kannte ich das ja. :D
Direkt mal in den grauen Zellen abgespeichert. :mrgreen: