Entwickler-Ecke

C# - Die Sprache - Einfaches Formatproblem


Stratego - Do 18.08.11 06:26
Titel: Einfaches Formatproblem
Hallo, ich habe ein sehr einfaches Problem das Programm lässt sich zwar ausführen aber gibt beim berechnen(Taschenrechner) eine Ausnahme weil der Formattyp falsch ist.

Wo ist das Problem?

Vielen Dank schonmal.


C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
 private void button2_Click(object sender, EventArgs e)
        {

            int zahl1 = Int32.Parse(button1.Text);
            int zahl2 = Int32.Parse(button2.Text);

            int ergebnis = zahl1 - zahl2;

            label4.Text = Convert.ToString(ergebnis);

        }


Christoph1972 - Do 18.08.11 07:08

Guten Morgen,

wie ist den Text des button1 & button2, da muss dann schon eine Ganzzahl stehen.


bakachan - Do 18.08.11 08:09

Wie mein Vorredner schon sagte wird button1.Text und/oder button2.Text keine gültige Ganzzahl sein.
Verwende am besten int.TryParse statt int.Parse (Programm stürzt nicht ab, der Rückgabewert zeigt dir an ob die Umwandlung erfolgreich war).