Autor Beitrag
JungerIslaender
ontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 427
Erhaltene Danke: 5

Win XP
Delphi 7; Delphi 2005
BeitragVerfasst: Di 11.12.07 14:54 
ausblenden Delphi-Quelltext
1:
 if spinedit1.Text : integer then...					


Hab es so versucht, funktioniert aber nicht.
alzaimar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2889
Erhaltene Danke: 13

W2000, XP
D6E, BDS2006A, DevExpress
BeitragVerfasst: Di 11.12.07 15:04 
ausblenden Delphi-Quelltext
1:
If TryStrToInt (MyString, MyInteger) Then					

_________________
Na denn, dann. Bis dann, denn.
JungerIslaender Threadstarter
ontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 427
Erhaltene Danke: 5

Win XP
Delphi 7; Delphi 2005
BeitragVerfasst: Di 11.12.07 15:07 
Das versteh ich nicht. Könntest du das vielleicht erklären?
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: Di 11.12.07 15:11 
Markieren + F1. In der DOH findest du eine ausführliche Dokumentation zu dieser Funtkion.
Alternativ bietet sich auch
int_variable := StrToIntDef(s,0); an.

Edit: Verglichen wird immer mit den logischen Operatoren "=","<",">". Den Doppelpunkt brauchst du nur beim Deklarieren von Variablen, Instanzen etc.

cu
Marc
elundril
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3747
Erhaltene Danke: 123

Windows Vista, Ubuntu
Delphi 7 PE "Codename: Aurora", Eclipse Ganymede
BeitragVerfasst: Di 11.12.07 15:20 
ja aber dann is es null wenns kein integer ist und er is so klug wie zuvor. ich würd sagen ein normales StrToInt und dann die exeption abfangen.

lg elundril

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: Di 11.12.07 15:25 
Kommt immer ganz darauf, was er vorhat.
Angenommen er will die Eingabe des Alters oder irgendeines anderen Wertes, bei dem nur Integer erlaubt sind, überprüfen.
Dafür benötigt man nicht gleich eine Exception.
ausblenden Delphi-Quelltext
1:
2:
i := StrToIntDef(s,-1);
if (i = -1) then showmessage('Bitte nur ganze Zahlen eingeben!');


Grüße
Marc
JungerIslaender Threadstarter
ontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 427
Erhaltene Danke: 5

Win XP
Delphi 7; Delphi 2005
BeitragVerfasst: Di 11.12.07 15:26 
Ich habe es jetzt so versucht:
ausblenden Delphi-Quelltext
1:
2:
 If TryStrToInt(Spinedit1.text, Code) =true  Then code:= strtoint(spinedit1.Text)
                else code:=0;


wenn ich mein programm starte und statt einem integerwert ein string einsetze (in spinedit1) Kommt trotzdem die fehlermeldung ... ist kein gültiger integerwert.

Ich möchte einfach nnur, dass wenn man etwas anderes als eine zahl eingibt, nicht das programm abstürzt.


Zuletzt bearbeitet von JungerIslaender am Di 11.12.07 15:28, insgesamt 1-mal bearbeitet
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Di 11.12.07 15:27 
Moin!

user profile iconelundril hat folgendes geschrieben:
ein normales StrToInt und dann die exeption abfangen.
Exceptions sollten immer nur der Notnagel sein. :| Wenn man den (Fehler-)Fall auch ohne fangen kann, dann sollte man das auch tun! :)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
elundril
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3747
Erhaltene Danke: 123

Windows Vista, Ubuntu
Delphi 7 PE "Codename: Aurora", Eclipse Ganymede
BeitragVerfasst: Di 11.12.07 15:29 
ok wusst ich nicht. sry!!

ich nehm an das die exception vom Spinedit ausgelöst wird. Überprüf lieber im KeyDown des SpinEdits ob es eine Zahl ist die eingegeben wurde oder nicht.

lg elundril

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
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 11.12.07 15:41 
user profile iconJungerIslaender hat folgendes geschrieben:
ausblenden Delphi-Quelltext
1:
2:
 If TryStrToInt(Spinedit1.text, Code) =true  Then code:= strtoint(spinedit1.Text)
                else code:=0;

Nimm an der Stelle StrToIntDef. Dabei wird im Fehlerfall der Zweite Parameter zurückgegeben.

_________________
I thought what I'd do was, I'd pretend I was one of those deaf-mutes.
zongo-joe
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 134

win xp prof
D3, D4, D7
BeitragVerfasst: Di 11.12.07 16:40 
wenn Du nur Zahlen als Eingabe zulassen willst, bei einem einfachen TEdit gehts das so:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not(key in [#8, '0'..'9']) then 
  begin 
    MessageBox(0,'Bitte nur Zahlen Eingeben','Fehler',0); 
    Key:= #0;
    Form1.Edit1.Text:=''; 
  end;
end;

(is hier irgendwo aus Forum).
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Di 11.12.07 17:22 
Hab keine vcl-Sources zur Hand aber iirc verwendet strtointdef selbst einen Exceptionhandler. Wie du's implementierst ist imho zweitrangig aber kapsle es auf jeden Fall in eine Funktion.
Die Implementationsdetails sind meistens uninterresant (höchstens beim Debugging und der Performance relevant, hier wie gesagt eher zweitrangig). Exceptionfrei geht's auf jeden Fall mit val.
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 11.12.07 17:40 
user profile icondelfiphan hat folgendes geschrieben:
Hab keine vcl-Sources zur Hand aber iirc verwendet strtointdef selbst einen Exceptionhandler.

Nö, intern wird direkt val verwendet und dann je nach Errorcode der default-Wert genommen.

_________________
I thought what I'd do was, I'd pretend I was one of those deaf-mutes.
DrRzf
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 260

Win XP Prof
D7 Enterprise
BeitragVerfasst: Di 11.12.07 19:05 
Sind auch negative eingaben erlaubt ?
dann hat

ausblenden Delphi-Quelltext
1:
StrToIntDef(Edit1.Text,-1);					


den nachteil dass bei einer eingabe von -1 (oder auch jeder andere gewählte wert) unklar ist ob die Zahl eingegben wurde oder
der rückgabewert der defaultwert von StrToIntDef ist.

abhilfe könnte man durch eine doppelabfrage mit StrToIntDef machen.
Ist zwar nicht elegant, aber es funktioniert, und das ganze ohne Fehlermeldung.

ausblenden Delphi-Quelltext
1:
2:
3:
if StrToIntDef(Edit1.Text,-1) = -1
  then if StrToIntDef(Edit1.Text,-2) = -2
    then MessageBox(0,'Bitte nur Zahlen Eingeben','Fehler',0);


Edit: Irgendwie den 2ten Post überlesen ^^
Es geht natürlich auch mit TryStrToInt wenn man den boolschen rückgabewert auswertet.

_________________
rein statistisch gesehen darf man keiner statistik trauen die man nicht selbst gefälscht hat.


Zuletzt bearbeitet von DrRzf am Di 11.12.07 19:12, insgesamt 1-mal bearbeitet
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 11.12.07 19:11 
So, wie er den Code oben eingebaut hat (bei ungültigen Zahlen 0), ist StrToIntDef am besten. Falls bei Fehleingaben eine spezielle Behandlung erfolgen soll, ist TryStrToInt am besten. Denn hier wird zurückgegeben, obs gültig war, und der Ausgabeparameter Value wird auf den konvertieren Wert gesetzt.

Puristen können natürlich auch val direkt benutzen. :wink:

_________________
I thought what I'd do was, I'd pretend I was one of those deaf-mutes.


Zuletzt bearbeitet von jakobwenzel am Di 11.12.07 21:42, insgesamt 1-mal bearbeitet
alzaimar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2889
Erhaltene Danke: 13

W2000, XP
D6E, BDS2006A, DevExpress
BeitragVerfasst: Di 11.12.07 20:16 
Hi Jakob,

endlich mal ein Stimme der Vernunft.

Hier nochmal alle Kandidaten:

ausblenden Delphi-Quelltext
1:
If Not TryStrToInt (MyString, Code) Then Showmessage(Format ('%s ist keine Zahl', [MyString]));					

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
Const
  iImpossibleInteger : -12345678;

Code := StrToIntDef (MyString, iImpossibleInteger);
If Code = iImpossibleInteger Then Showmessage(Format ('%s ist keine Zahl', [MyString]));

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
Try
  Code := StrToInt (MyString);
Except
  Showmessage(Format ('%s ist keine Zahl', [MyString]));
End;



user profile iconNarses hat folgendes geschrieben:
Exceptions sollten immer nur der Notnagel sein. :|

Nee, Exceptions sind Ausnahmen, keine Pickel. Exceptions dienen zur strukturierten Behandlung der ... Ausnahmen vom normalen Programmkontrollfluss. Es ist kein Notnagel, sondern -richtig angewendet- elegant.

_________________
Na denn, dann. Bis dann, denn.
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Mi 12.12.07 00:07 
Nur so am Rande ;). SpinEdits können bei mir ohnehin nur Zahlen beinhalten oder aber einen leeren String
(Edit: Per Copy/Paste kann man beliebige Werte reinpasten. Imho schon fast ein Bug/Inkonsistenz).

Zur Diskussion Exceptions oder nicht: Wenn man mehrere Strings zu Zahlen konvertieren muss, ist es viel eleganter, Exceptions zu verwenden statt an jeder Stelle ein If plus eine Messagebox und ein exit. Genau um solchen Code (und anderen) zu verhindern wurden Exceptions erfunden... Statt dem allgemeinen Exception-Handler sollte man jedoch einen spezifischen Handler für EConvError haben, um falsche Fehlermeldungen zu verhindern.
JungerIslaender Threadstarter
ontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 427
Erhaltene Danke: 5

Win XP
Delphi 7; Delphi 2005
BeitragVerfasst: Mi 12.12.07 13:21 
Vielen Dank, Problem gelöst.
malibu85
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 51



BeitragVerfasst: Di 18.12.07 12:43 
ich habe dasProblem so glöst
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:
var
  Form1: TForm1;
  Eingabebetrag :String;
  a:integer;

implementation

{$R *.nfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
Eingabebetrag:=Edit1.Text;


    if TryStrToInt(Eingabebetrag,a)=true then
       begin
         edit2.Text:='Integer';
       end
     else
       begin
        edit2.Text:='keine Integer';
       end;






end;
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 18.12.07 14:25 

_________________
I thought what I'd do was, I'd pretend I was one of those deaf-mutes.