Autor Beitrag
Ritzeratze
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 101



BeitragVerfasst: Mo 09.06.14 17:14 
Moin,
bin mit einer Hausaufgabe zur Ausnahmebehandlung eines Taschenrechners zu folgenden Ergebnis gelangt.

ausblenden volle Höhe C#-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:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Taschenrechner
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void buttonBeenden_Click(object sender, EventArgs e)
    {
      Close();
    }

    private void buttonBerechnen_Click(double zahl1, double zahl2)
    {
      Pruefen(TextBoxZahl1, TextBoxZahl2);
      double ergebnis = 0;

      //die Rechenoperation ermitteln und ausführen
      if (radioButtonAddition.Checked == true)
        ergebnis = zahl1 + zahl2;
      if (radioButtonAddition.Checked == true)
        ergebnis = zahl1 - zahl2;
      if (radioButtonMultiplikation.Checked == true)
        ergebnis = zahl1 * zahl2;
      if (radioButtonDivision.Checked == true)
        ergebnis = zahl1 / zahl2;

      labelAnzeige.Text = Convert.ToString(ergebnis);

    }

    private void Pruefen(TextBox TextBoxZahl1, TextBox TextBoxZahl2)
    {

      //Vereinbarung von Variablen 
      double zahl1, zahl2 = 0;

      //Prüfe ob ein Ausnahme stattfindet 
      Boolean ausnahme = false;

      //Prüfe nach eine ungültige Eingabe bzw. Konvertierungsfehler und Division durch Null  
      try
      {
        zahl2 = Convert.ToDouble(TextBoxZahl2.Text);

        //Wenn Division wird nach dem Wert geprüft 
        if (radioButtonDivision.Checked == true)

          if (zahl2 == 0)
          {
            MessageBox.Show("Zahl 2 hat einen Null Wert " + zahl2 + " Division durch Null ist nicht definiert");
            TextBoxZahl2.Select();
            ausnahme = true;
          }

        try
        {
          zahl1 = Convert.ToDouble(TextBoxZahl1.Text);

          if (radioButtonDivision.Checked == true)

            if (zahl1 == 0)
            {
              MessageBox.Show("Zahl 1 hat einen Null Wert " + zahl1 + " Division durch Null ist nicht definiert");
              TextBoxZahl1.Select();
              ausnahme = true;
            }
        }
        catch (FormatException)
        {

          //Wenn kein Zahl eingegeben wird sondern ein Buchstabe 
          MessageBox.Show("Bei der Konvertierung von Zahl 1 ist ein Fehler aufgetreten " + zahl1);
          TextBoxZahl1.Select();
          ausnahme = true;
        }
      }
      catch (FormatException)
      {

        MessageBox.Show("Bei der Konvertierung von Zahl 2 ist ein Fehler aufgetreten " + zahl2);
        TextBoxZahl2.Select();
        ausnahme = true;
      }

      //Wenn die ausnahme nicht stattgefunden hat, dann die Berechnungen aus der Berechnen Methode ausführen. 
      buttonBerechnen_Click(zahl1, zahl2);
    }
  }
}


allerdings wird bemängelt das zahl1, zahl2 nicht definiert ist.
Kann mir jemand erklären wo mein Problem liegt ?
Danke
Ritze

Moderiert von user profile iconTh69: Code- durch C#-Tags ersetzt
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4796
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Mo 09.06.14 17:40 
Hallo,

in welcher Zeile wird das bemängelt (am besten immer die ganze Fehlermeldung posten, wenn du selber nicht weiterkommst).
Ich denke aber, daß das Grundproblem ist, daß du die Button.Click-Ereignismethode umdefiniert hast, d.h. die Parameter ausgetauscht hast (und das darfst du natürlich nicht). Die Signatur muß die gleiche sein wie z.B. bei ButtonBeenden_Click, d.h.
ausblenden C#-Quelltext
1:
private void buttonBerechnen_Click(object sender, EventArgs e)					


PS: Und der Aufruf dieser Methode aus deiner Pruefen-Methode ist auch sinnfrei.
Ritzeratze Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 101



BeitragVerfasst: Mo 09.06.14 18:36 
Hallo Th69,

Bei der Eingabe von Zahlen in den Textboxen sollen die Konvertierungsfehler abgefangen werden.
Allerdings soll die Überprüfung außerhalb der Berechnungsmethode durchgeführt werden.
So muss ich doch beim Aufruf der Pruefmethode die Werte aus den Textboxen mitgeben, oder bin ich da auf dem Holzweg?

Gruss Ritze ?
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4796
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Mo 09.06.14 19:05 
Ja, gegen die Pruefen-Methode als solches habe ich ja nichts gesagt. Du solltest der aber einen boolschen Rückgabewert geben, um nach dem Aufruf aus der buttonBerechnen_Click-Methode zu überprüfen, ob weitergerechnet werden soll.

Hast du denn jetzt den Compilerfehler lösen können?
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Mo 09.06.14 20:39 
Zitat:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
try
{
    zahl1 = Convert.ToDouble(TextBoxZahl1.Text);                  
    ....
}
catch (FormatException)
{
    MessageBox.Show("Bei der Konvertierung von Zahl 1 ist ein Fehler aufgetreten " + zahl1);
    ....
}


Ich kann nicht beurteilen ob das alle Fehler in deinem Code sind aber der zitierte Code Ausschnitt wird definitiv nicht funktionieren.
Du weißt im try Block zum ersten mal der Variablen zahl1 etwas zu. Da bei der Zuweisung etwas schiefgehen kann ist am Ende des try nicht sichergestellt das zahl1 auf irgendwas zugewiesen ist. Du willst zahl1 aber im catch Block benutzen das wird der Compiler aus dem genannten Grund 'der Variablen ist möglicherweise nichts zugewiesen' verhindern und nicht kompilieren. Du solltest auch eine eigentlich verständliche entsprechende Fehlermeldung dazu bekommen 'use of unassigned local variable' oder so.
Zur Lösung solltest du da einfach den Inhalt der TextBox anzeigen. Die Exception tritt ja auf wenn der Text in der TextBox nicht konvertierbar ist. Das Ziel der nicht möglichen Konvertierung wird dir da also nie helfen, weil du die nicht hast. Wenn du die hättest hätte die Konvertierung funktioniert. Also den TextBox Inhalt anzeigen.

Alternativ zu Convert.ToDouble solltest du dir auch mal Double.TryParse ansehen. Da hier eine Fehler wahrscheinlich ist, ein User darf ja eingeben wozu auch immer er Lust hast, solltest du eine Methode ohne Exception praferieren.

Zitat:
ausblenden C#-Quelltext
1:
2:
private void Pruefen(TextBox TextBoxZahl1, TextBox TextBoxZahl2)
{


Ich vermute mal die Controls auf der Form heißen auch TextBoxZahl1 und TextBoxZahl2. Das solltest du nicht so tun, oder bist du dir jetzt in der Methode sicher ob du die übergebenen Parameter oder die auf der Form liegenden Controls benutzt?
Entscheide dich eindeutig in den Methoden ob du auf die Controls der Form zugreifen willst oder du die als Parameter übergeben willst. Mach aber nicht beides.
Ritzeratze Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 101



BeitragVerfasst: Mo 09.06.14 23:28 
Hi,

Danke für die tollen Antworten.
Jetzt bin ich aber total raus.

Gruss Ritze


Zuletzt bearbeitet von Ritzeratze am Di 10.06.14 23:54, insgesamt 1-mal bearbeitet
Ritzeratze Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 101



BeitragVerfasst: Di 10.06.14 23:09 
Moin,

habe mir das ganze nochmal in Ruhe angeschaut und überarbeitet.
Die Berechnungsmethode stimmen allerdings bei der Addition und Subtraktion nicht. Kann mir jemand einen Tipp geben, wo das Problem liegen könnte?

ausblenden volle Höhe C#-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:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Taschenrechner
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void buttonBeenden_Click(object sender, EventArgs e)
    {
      Close();
    }

    public double Berechnen(double zahl1 , double zahl2)
    {
      double ergebnis = 0;

      //die Rechenoperation ermitteln und ausführen
      if (radioButtonAddition.Checked == true)
        ergebnis = zahl1 + zahl2;
      if (radioButtonAddition.Checked == true)
        ergebnis = zahl1 - zahl2;
      if (radioButtonMultiplikation.Checked == true)
        ergebnis = zahl1 * zahl2;
      if (radioButtonDivision.Checked == true)
        ergebnis = zahl1 / zahl2;

      return (ergebnis);
  
    }

      private void Pruefen(TextBox TextBoxZahl1, TextBox TextBoxZahl2)
      {
      
      //Vereinbarung von Variablen 
      double zahl1 = 0, zahl2 = 0;

      //Prüfe ob ein Ausnahme stattfindet 
      Boolean ausnahme = false;

      //Prüfe nach eine ungültige Eingabe bzw. Konvertierungsfehler und Division durch Null  
      try
        {
        zahl2 = Convert.ToDouble(TextBoxZahl2.Text);

        //Wenn Division wird nach dem Wert geprüft 
        if (radioButtonDivision.Checked == true)

          if (zahl2 == 0)
          {
            MessageBox.Show("Zahl 2 hat einen Null Wert " + zahl2 + " Division durch Null ist nicht definiert");
            TextBoxZahl2.Select();
            ausnahme = true;
          }

            try
            {
                 zahl1 = Convert.ToDouble(TextBoxZahl1.Text);

                 if (radioButtonDivision.Checked == true)

                    if (zahl1 == 0)
                    {
                     MessageBox.Show("Zahl 1 hat einen Null Wert " + zahl1 + " Division durch Null ist nicht definiert");
                     TextBoxZahl1.Select();
                     ausnahme = true;
                    }
            }
            catch (FormatException)
             {

              //Wenn kein Zahl eingegeben wird sondern ein Buchstabe 
              MessageBox.Show("Bei der Konvertierung von Zahl 1 ist ein Fehler aufgetreten " + zahl1 );
              TextBoxZahl1.Select();
             ausnahme = true;
            }
      }
      catch (FormatException)
      {

        MessageBox.Show("Bei der Konvertierung von Zahl 2 ist ein Fehler aufgetreten " + zahl2);
        TextBoxZahl2.Select();
        ausnahme = true;
      }

      //Wenn die ausnahme nicht stattgefunden hat, dann die Berechnungen aus der Berechnen Methode ausführen. 
      if (ausnahme == false)
      {
        labelAnzeige.Text= Convert.ToString(Berechnen(zahl1,zahl2));
      }
    }

      private void buttonBerechnen_Click(object sender, EventArgs e)
      {
        Pruefen(TextBoxZahl1, TextBoxZahl2);
      }
   }
}


Moderiert von user profile iconTh69: Code- durch C#-Tags ersetzt
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Di 10.06.14 23:29 
Zitat:
ausblenden C#-Quelltext
1:
2:
3:
4:
if (radioButtonAddition.Checked == true)
    ergebnis = zahl1 + zahl2;
if (radioButtonAddition.Checked == true)
    ergebnis = zahl1 - zahl2;


Guck nochmal genau hin.
Ritzeratze Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 101



BeitragVerfasst: Di 10.06.14 23:29 
Hallo,


Fehler gefunden. Im Berechnungsmodul war zweimal Addition angegeben ,


Zuletzt bearbeitet von Ritzeratze am Di 10.06.14 23:30, insgesamt 1-mal bearbeitet
Ritzeratze Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 101



BeitragVerfasst: Di 10.06.14 23:29 
Danke Ralf,

da haben sich unsere Antworten gerade überschnitten.

Gruss Ritze