Autor Beitrag
Defe
Hält's aus hier
Beiträge: 1



BeitragVerfasst: So 12.10.14 16:28 
Hallo liebe Community,

Ich knobel jetzt seit 1 Stunde.
Warum zur Hölle überspringt mein Compiler unten stehende Case-Anweisung, bzw. findet keinen richtigen Eingabewert?
Hier mal 2 von mir ausprobierte Varianten;

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
function TForm1.checkdat(s:string):boolean;
var hilf:string; a,int,startpos,charcount,i:integer;
begin
 case length(s) of
  0..6,8,9,11..350:result:=false;
  7: charcount:=1;
  10:charcount:=2;
 end;
...


ODER:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
function TForm1.checkdat(s:string):boolean; 
var hilf:string; a,int,startpos,charcount,i:integer;
begin
 case length(s) of
  7: charcount:=1;
  10:charcount:=2
  else result:=false;
 end;
...


verschiedene Stringlängen habe ich übrigens durch eine "Showmessage" vor der Case-Anweisung ausprobiert, beispielsweise 4,9,11.
Er will aber einfach nicht auf den ELSE-Bereich hüpfen....


Moderiert von user profile iconNarses: Topic aus Sonstiges (Delphi) verschoben am So 12.10.2014 um 22:16
WasWeißDennIch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 653
Erhaltene Danke: 160



BeitragVerfasst: So 12.10.14 17:16 
Und wenn Du das mal so umformulierst, dass die Optimierung nicht ggf. greift?
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
function TForm1.checkdat(s:string):boolean;
var
  hilf: string
  a,int,startpos,charcount,i: integer;
begin  
  case length(s) of
    7
      charcount := 1;
    10:
      charcount := 2;
    else
      charcount := 0;
  end;
  Result := charcount > 0;
  //...
end;
Horst_H
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1652
Erhaltene Danke: 243

WIN10,PuppyLinux
FreePascal,Lazarus
BeitragVerfasst: So 12.10.14 17:23 
Hallo,

dieser Schnipsel funktioniert doch:
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:
{$IFDEF FPC}
   {$MODE DELPHi}
{$ELSE}
   {$APPTYPE CONSOLE}
{$ENDIF}

function checkdat1(s:string):boolean;
var hilf:string; a,int,startpos,charcount,i:integer;
begin
  charcount:=0;
  result  := true;
  case length(s) of
     7: charcount:=1;
    10: charcount:=2;
    else
      result  := false;
  end;
end;

function checkdat2(s:string):boolean;
var hilf:string; a,int,startpos,charcount,i:integer;
begin
  charcount:=0;
  result:=false;
  case length(s) of
    7begin
         charcount :=1;
         result := true;
       end;
   10begin
         charcount :=2;
         result := true;
       end;
  end;
end;

Var
  i: integer;
  s:string;

BEGIN
  s := '';
  For i :=  0 to 10 do
  begin
    s := s+chr(i+Ord('A'));
    writeln(s,'  ',checkdat1(s));
    writeln(s,'  ',checkdat2(s));
  end;
END.

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
A  FALSE
A  FALSE
AB  FALSE
AB  FALSE
ABC  FALSE
ABC  FALSE
ABCD  FALSE
ABCD  FALSE
ABCDE  FALSE
ABCDE  FALSE
ABCDEF  FALSE
ABCDEF  FALSE
ABCDEFG  TRUE
ABCDEFG  TRUE
ABCDEFGH  FALSE
ABCDEFGH  FALSE
ABCDEFGHI  FALSE
ABCDEFGHI  FALSE
ABCDEFGHIJ  TRUE
ABCDEFGHIJ  TRUE
ABCDEFGHIJK  FALSE
ABCDEFGHIJK  FALSE


Gruß Horst
mandras
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 429
Erhaltene Danke: 107

Win 10
Delphi 6 Prof, Delphi 10.4 Prof
BeitragVerfasst: So 12.10.14 21:45 
user profile iconDefe hat folgendes geschrieben Zum zitierten Posting springen:
Hallo liebe Community,
Er will aber einfach nicht auf den ELSE-Bereich hüpfen....


Lokale Variablen in einer Func/Proc. sind vordefiniert - booleans als false, ints als 0, strings als leer etc.

result ist eine lokale Variable - somit ab Func-Beginn false.

Wenn Optimierung eingeschaltet ist (standard) wird der else-Zweig nicht codiert, da der Compiler weiß, daß er am Wert der Result-Variable nichts ändert.
WasWeißDennIch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 653
Erhaltene Danke: 160



BeitragVerfasst: So 12.10.14 22:16 
Lokale Variablen sind eben nicht initialisiert, deshalb sollte man sie ggf. vorbelegen.
OlafSt
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 486
Erhaltene Danke: 99

Win7, Win81, Win10
Tokyo, VS2017
BeitragVerfasst: So 12.10.14 22:22 
Das würde aber auch nichts ändern, außer man initialisiert Result mit true. Beläßt man es bei false, wird nach wie vor der else-Zweig vom Optimizer komplett entfernt, ergo scheint man im Debugger niemals im else-Zweig zu landen (wie auch, er existiert ja nicht mehr). So oder so: Anschließend gibts ne Warnung, das die auf Result und charcount zugewiesenen Werte nie benutzt werden ;)

_________________
Lies, was da steht. Denk dann drüber nach. Dann erst fragen.
Blup
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 173
Erhaltene Danke: 43



BeitragVerfasst: Mo 13.10.14 10:09 
user profile iconOlafSt hat folgendes geschrieben Zum zitierten Posting springen:
Das würde aber auch nichts ändern, außer man initialisiert Result mit true.

Das ist nicht richtig. Der Rückgabewert wird beim Eintritt in die Funktion nicht automatisch initialisiert.
Tatsächlich wird die Funktion wie eine Prozedur mit einem var-Parameter "Result" übersetzt.
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:
function Test1(AValue: Boolean): string;
begin
  if AValue then
    Result := 'Test1(True)';
end;

{erzeugt den selben Code}
procedure Test2(var Result: string; AValue: Boolean);
begin
  if AValue then
    Result := 'Test2(True)';
end;

var
  s: string;
begin
  s := 'Test1(False)';
  s := Test1(False);
  Application.MessageBox('Testergebnis', s);
  s := Test1(True);
  Application.MessageBox('Testergebnis', s);

  s := 'Test2(False)';
  Test2(s, False);
  Application.MessageBox('Testergebnis', s);
  Test2(s, True);
  Application.MessageBox('Testergebnis', s);
end;


Zuletzt bearbeitet von Blup am Mo 13.10.14 14:04, insgesamt 1-mal bearbeitet
mandras
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 429
Erhaltene Danke: 107

Win 10
Delphi 6 Prof, Delphi 10.4 Prof
BeitragVerfasst: Mo 13.10.14 12:09 
Ich nehme alles zurück..
Lokale Variablen werden nicht initialisiert.
Blup
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 173
Erhaltene Danke: 43



BeitragVerfasst: Mo 13.10.14 14:20 
Im Gegensatz zu Parametern werden einige lokale Variablen-Typen tatsächlich mit nil initialisiert.

String-Variable (nil = Leerstring)
Array-Variable (nil = Array mit 0 Elementen)
Interface-Variable (nil = nicht zugewiesen)

Alle anderen lokalen Variablen z.B. auch Object-Variablen sind nicht initialisiert.