Autor Beitrag
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 04.01.09 13:21 
Hallo,

einige kleine Anmerkungen:

Die using-Anweisungen kannst Du (wie schon gesagt) fürs Forum weglassen; die sind fast immer unwichtig. (Du notierst auch zu viele, die Du gar nicht brauchst.)

Was sollen die Klammern bei den String-Zuweisungen?
ausblenden C#-Quelltext
1:
textBox3.Text = ("" + ergebnis + "");					

Diese sind überflüssig; nur bei der Gliederung komplizierter Berechnungen oder and/or-Abfragen sind sie hilfreich oder wichtig. Außerdem sollte für die Konvertierung immer ToString() verwendet werden (das hatte ich schonmal erläutert):
ausblenden C#-Quelltext
1:
textBox3.Text = ergebnis.ToString();					

Die zusätzliche Variable notpossible verwirrt: "false" heißt "nicht unmöglich", also "möglich", also possible - naja. Sie ist auch gar nicht nötig:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
string message = String.Empty;   // dies ist die saubere Vorgabe
//  dann kann so geprüft werden:
            if (! String.IsNullOrEmpty(message) )
            {
                MessageBox.Show(message);
            }

So wie Du die Prüfungen aufbaust, ist auch possible überflüssig; denn es kann auch kürzer geschrieben werden:
ausblenden C#-Quelltext
1:
if( Double.TryParse(input1, out number1) )  { ... }					

Weiterhin viel Erfolg! Jürgen
ilwaka Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 48

Win Vista
C# (VS 2008)
BeitragVerfasst: So 04.01.09 17:41 
ok glaube hab deine hinweise reingearbeitet.
das mit den using hinweisen:D ich wollte es weglassen aber hab anscheinend doch wieder strg+a
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:
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
        }

        public void button1_Click(object sender, EventArgs e)
        {
            string Message = ("");
            string input1 = textBox1.Text;
            string input2 = textBox2.Text;
            double number1 = 0;
            double number2 = 0;
            bool notpossible = false;
            //das kapital wird von string zu double gewandelt
            

            if (Double.TryParse(input1, out number1))
            {
                
                //prozent wird in double konvert,also auch komma
                 Double.TryParse(input2, out number2);
                if (Double.TryParse(input2, out number2))
                {
                   // hier wird gerechnet
                    
                    double ergebnis = (number1 * number2) / 100;
                    textBox3.Text = ergebnis.ToString();
 
                }
                else
                {
                     
                      notpossible = true;
                     Message = ("Zahl 2 ist keine gültige Zahl");

                }
            }
            else
            {
                notpossible = true;
                Message=("Zahl1 ist keine gültige Zahl");
                
            }
            if (notpossible)
            {
                MessageBox.Show(""+Message+"");
            }
            

            
            }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        }
    }
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 04.01.09 17:53 
user profile iconilwaka hat folgendes geschrieben Zum zitierten Posting springen:
ok glaube hab deine hinweise reingearbeitet.

Aber nur zum Teil: Klammern bei string-Zuweisungen, Verwendung von notpossible.

Jürgen
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19313
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 04.01.09 18:19 
Und das kannst du auch weglassen. ;-)
In beiden Zeilen wird die Umwandlung ggf. durchgeführt, nur dass du beim zweiten Mal erst den Rückgabewert tatsächlich verwendest.
user profile iconilwaka hat folgendes geschrieben Zum zitierten Posting springen:
ausblenden C#-Quelltext
1:
2:
                 Double.TryParse(input2, out number2);
                if (Double.TryParse(input2, out number2))
ilwaka Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 48

Win Vista
C# (VS 2008)
BeitragVerfasst: So 04.01.09 18:22 
so habs jetzt die verwendung von possible vermieden.
wenn jetzt zahl1 oder 2 nicht gültigs ist wird kein neues fenster mehr geöffnet
sondern der fehler wird im ergebnis fenster angezeigt.
@ jaenicke :D
das sollte natürlich weg habs nur übersehen :D
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:
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
        }

        public void button1_Click(object sender, EventArgs e)
        {
            string Message = "";
            string input1 = textBox1.Text;
            string input2 = textBox2.Text;
            double number1 = 0;
            double number2 = 0;
            bool notpossible = false;
            //das kapital wird von string zu double gewandelt
            

            if (Double.TryParse(input1, out number1))
            {
                
                //prozent wird in double konvert,also auch komma
                 
                if (Double.TryParse(input2, out number2))
                {
                   // hier wird gerechnet
                    
                    double ergebnis = (number1 * number2) / 100;
                    
                   Message = ergebnis.ToString();
 
                }
                else
                {
                     
                      
                     Message = "Zahl 2 ist keine gültige Zahl";

                }
            }
            else
            {
                
                Message="Zahl1 ist keine gültige Zahl";
                
            }
           
            textBox3.Text = Message;
            

            
            }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        }
    }